Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple stock details lookup in YFStockDetailsLoader #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions BSYahooFinance.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
C70B94721589B3D6003CC371 /* libz.1.2.5.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = C70B94711589B3D6003CC371 /* libz.1.2.5.dylib */; };
F414981313521987008AD497 /* NSObject+JSON.m in Sources */ = {isa = PBXBuildFile; fileRef = F414981213521987008AD497 /* NSObject+JSON.m */; };
F414982813521AA4008AD497 /* SBJsonStreamParser.m in Sources */ = {isa = PBXBuildFile; fileRef = F414981D13521AA3008AD497 /* SBJsonStreamParser.m */; };
F414982913521AA4008AD497 /* SBJsonStreamParserAdapter.m in Sources */ = {isa = PBXBuildFile; fileRef = F414981F13521AA3008AD497 /* SBJsonStreamParserAdapter.m */; };
Expand All @@ -26,7 +27,6 @@
F45C1E31133F4E1B00B3D443 /* ASINetworkQueue.m in Sources */ = {isa = PBXBuildFile; fileRef = F45C1E19133F4E1B00B3D443 /* ASINetworkQueue.m */; };
F45C1E36133F4E1B00B3D443 /* SBJsonParser.m in Sources */ = {isa = PBXBuildFile; fileRef = F45C1E27133F4E1B00B3D443 /* SBJsonParser.m */; };
F45C1E37133F4E1B00B3D443 /* SBJsonWriter.m in Sources */ = {isa = PBXBuildFile; fileRef = F45C1E29133F4E1B00B3D443 /* SBJsonWriter.m */; };
F45C1E39133F4E3100B3D443 /* libz.1.2.3.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = F45C1E38133F4E3100B3D443 /* libz.1.2.3.dylib */; };
F45C1E41133F4E5500B3D443 /* CFNetwork.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F45C1E40133F4E5500B3D443 /* CFNetwork.framework */; };
F45C1E43133F4E6000B3D443 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F45C1E42133F4E6000B3D443 /* SystemConfiguration.framework */; };
F45C1E45133F4E6D00B3D443 /* MobileCoreServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F45C1E44133F4E6D00B3D443 /* MobileCoreServices.framework */; };
Expand Down Expand Up @@ -70,6 +70,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
C70B94711589B3D6003CC371 /* libz.1.2.5.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libz.1.2.5.dylib; path = usr/lib/libz.1.2.5.dylib; sourceTree = SDKROOT; };
F414981113521987008AD497 /* NSObject+JSON.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+JSON.h"; sourceTree = "<group>"; };
F414981213521987008AD497 /* NSObject+JSON.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+JSON.m"; sourceTree = "<group>"; };
F414981C13521AA3008AD497 /* SBJsonStreamParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SBJsonStreamParser.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -166,10 +167,10 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
C70B94721589B3D6003CC371 /* libz.1.2.5.dylib in Frameworks */,
F45C1E45133F4E6D00B3D443 /* MobileCoreServices.framework in Frameworks */,
F45C1E43133F4E6000B3D443 /* SystemConfiguration.framework in Frameworks */,
F45C1E41133F4E5500B3D443 /* CFNetwork.framework in Frameworks */,
F45C1E39133F4E3100B3D443 /* libz.1.2.3.dylib in Frameworks */,
F4EAE16E133F4CF700A543DB /* UIKit.framework in Frameworks */,
F4EAE170133F4CF700A543DB /* Foundation.framework in Frameworks */,
F4EAE172133F4CF700A543DB /* CoreGraphics.framework in Frameworks */,
Expand Down Expand Up @@ -307,6 +308,7 @@
F4EAE15E133F4CF700A543DB = {
isa = PBXGroup;
children = (
C70B94711589B3D6003CC371 /* libz.1.2.5.dylib */,
F45C1DFD133F4D9000B3D443 /* BSYahooFinance */,
F4EAE173133F4CF700A543DB /* BSYahooFinanceDemo */,
F4EAE193133F4CF800A543DB /* BSYahooFinanceTests */,
Expand Down
8 changes: 4 additions & 4 deletions BSYahooFinance/YFStockDetailsLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
id<YFStockDetailsLoaderDelegate> delegate;
ASIHTTPRequest *internalStockRequest;
NSError *internalError;
YFStockDetails *internalStockDetails;
NSString *internalSymbolToLoad;
NSMutableArray *internalStockDetails;
NSArray *internalSymbolsToLoad;
BOOL synchronousLoad;
}

@property (nonatomic, assign) id<YFStockDetailsLoaderDelegate> delegate;
@property (nonatomic, readonly) NSError *error;
@property (nonatomic, readonly) YFStockDetails *stockDetails;
@property (nonatomic, readonly) NSArray *stockDetails;
@property (nonatomic, assign) BOOL synchronousLoad;

+ (YFStockDetailsLoader *)loaderWithDelegate:(id<YFStockDetailsLoaderDelegate>)delegate;
- (id)initWithDelegate:(id<YFStockDetailsLoaderDelegate>)delegate;
- (void)loadDetails:(NSString *)stockSymbol;
- (void)loadDetails:(NSArray *)stockSymbols;
- (void)cancel;

@end
40 changes: 26 additions & 14 deletions BSYahooFinance/YFStockDetailsLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
@interface YFStockDetailsLoader()
@property (nonatomic, retain) ASIHTTPRequest *internalStockRequest;
@property (nonatomic, retain) NSError *internalError;
@property (nonatomic, retain) YFStockDetails *internalStockDetails;
@property (nonatomic, retain) NSString *internalSymbolToLoad;
@property (nonatomic, retain) NSMutableArray *internalStockDetails;
@property (nonatomic, retain) NSArray *internalSymbolsToLoad;
@end

@implementation YFStockDetailsLoader
Expand All @@ -26,11 +26,11 @@ @implementation YFStockDetailsLoader
@synthesize internalStockRequest;
@synthesize internalError;
@synthesize internalStockDetails;
@synthesize internalSymbolToLoad;
@synthesize internalSymbolsToLoad;
@dynamic error;
@dynamic stockDetails;

static NSString *yahooLoadStockDetailsURLString = @"http://query.yahooapis.com/v1/public/yql?q=select%%20*%%20from%%20yahoo.finance.quotes%%20where%%20symbol%%20%%3D%%20%%22%@%%22&format=json&env=store%%3A%%2F%%2Fdatatables.org%%2Falltableswithkeys&callback=cbfunc";
static NSString *yahooLoadStockDetailsURLString = @"http://query.yahooapis.com/v1/public/yql?q=select%%20*%%20from%%20yahoo.finance.quotes%%20where%%20symbol%%20in%%20(%@)&format=json&env=store%%3A%%2F%%2Fdatatables.org%%2Falltableswithkeys&callback=cbfunc";


#pragma mark - Initializers
Expand All @@ -51,12 +51,18 @@ - (id)initWithDelegate:(id<YFStockDetailsLoaderDelegate>)aDelegate

#pragma mark - Implementation

- (void)loadDetails:(NSString *)stockSymbol
- (void)loadDetails:(NSArray *)stockSymbols
{
[self cancel];
self.internalSymbolToLoad = stockSymbol;
self.internalSymbolsToLoad = stockSymbols;

NSURL *requestUrl = [NSURL URLWithString:[NSString stringWithFormat:yahooLoadStockDetailsURLString, [stockSymbol stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]];
NSMutableArray *symbolParams = [NSMutableArray arrayWithCapacity:[stockSymbols count]];
for (NSString *stockSymbol in stockSymbols) {
[symbolParams addObject:[NSString stringWithFormat:@"'%@'", [stockSymbol stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]];
}

NSString *urlString = [NSString stringWithFormat:yahooLoadStockDetailsURLString, [symbolParams componentsJoinedByString:@","]];
NSURL *requestUrl = [NSURL URLWithString:urlString];

self.internalStockRequest = [ASIHTTPRequest requestWithURL:requestUrl];
[self.internalStockRequest setCachePolicy:ASIDoNotReadFromCacheCachePolicy];
Expand All @@ -79,7 +85,7 @@ - (void)cancel
self.internalStockRequest = nil;
}

self.internalSymbolToLoad = nil;
self.internalSymbolsToLoad = nil;
self.internalStockDetails = nil;
self.internalError = nil;
}
Expand All @@ -101,12 +107,19 @@ - (void)requestDidFinish:(ASIHTTPRequest *)request
else {
int resultCount = [[[parsedDictionary objectForKey:@"query"] objectForKey:@"count"] intValue];

if (resultCount == 1) {
NSDictionary *stockInfo = [[[parsedDictionary objectForKey:@"query"] objectForKey:@"results"] objectForKey:@"quote"];
if(stockInfo != nil) {
self.internalStockDetails = [YFStockDetails stockDetailsWithDetails:stockInfo];
if (resultCount >0) {
id quotes = [[[parsedDictionary objectForKey:@"query"] objectForKey:@"results"] objectForKey:@"quote"];

if ([quotes isKindOfClass:[NSDictionary class]]) {
self.internalStockDetails = [NSMutableArray arrayWithObject:[YFStockDetails stockDetailsWithDetails:quotes]];
} else if ([quotes isKindOfClass:[NSArray class]]) {
self.internalStockDetails = [NSMutableArray arrayWithCapacity:[quotes count]];
for (NSDictionary *quote in quotes) {
[self.internalStockDetails addObject:[YFStockDetails stockDetailsWithDetails:quote]];
}
}
}


if (self.internalStockDetails == nil) {
self.internalError = [NSError errorWithDomain:@"YFStockDetailsLoader" code:0 userInfo:
Expand Down Expand Up @@ -137,8 +150,7 @@ - (NSError *)error
return self.internalError;
}

- (YFStockDetails *)stockDetails
{
- (NSArray *)stockDetails {
return self.internalStockDetails;
}

Expand Down
10 changes: 7 additions & 3 deletions BSYahooFinanceDemo/DetailsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ - (void)viewDidAppear:(BOOL)animated
{
self.title = self.stockSymbol.name;
self.detailsLoader = [YFStockDetailsLoader loaderWithDelegate:self];
[self.detailsLoader loadDetails:self.stockSymbol.symbol];
[self.detailsLoader loadDetails:[NSArray arrayWithObject:self.stockSymbol.symbol]];
}

#pragma mark - YFStockDetailsLoader delegate methods

- (void)stockDetailsDidLoad:(YFStockDetailsLoader *)aDetailsLoader
{
self.detailKeys = [aDetailsLoader.stockDetails.detailsDictionary allKeys];
NSArray *details = aDetailsLoader.stockDetails;
YFStockDetails *stockDetails = [details objectAtIndex:0];
self.detailKeys = [stockDetails.detailsDictionary allKeys];
[self.stockDetails reloadData];
}

Expand Down Expand Up @@ -90,7 +92,9 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
}
else if ([self.detailKeys count] > 0) {
cell.textLabel.textColor = [UIColor blackColor];
NSString *str = [self.detailsLoader.stockDetails.detailsDictionary objectForKey:[self.detailKeys objectAtIndex:indexPath.row]];
NSArray *details = self.detailsLoader.stockDetails;
YFStockDetails *stockDetails = [details objectAtIndex:0];
NSString *str = [stockDetails.detailsDictionary objectForKey:[self.detailKeys objectAtIndex:indexPath.row]];
if (![[NSNull null] isEqual:str]) {
cell.detailTextLabel.text = str;
}
Expand Down