diff --git a/BSYahooFinance.xcodeproj/project.pbxproj b/BSYahooFinance.xcodeproj/project.pbxproj index 75bf61a..d068a3d 100644 --- a/BSYahooFinance.xcodeproj/project.pbxproj +++ b/BSYahooFinance.xcodeproj/project.pbxproj @@ -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 */; }; @@ -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 */; }; @@ -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 = ""; }; F414981213521987008AD497 /* NSObject+JSON.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+JSON.m"; sourceTree = ""; }; F414981C13521AA3008AD497 /* SBJsonStreamParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SBJsonStreamParser.h; sourceTree = ""; }; @@ -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 */, @@ -307,6 +308,7 @@ F4EAE15E133F4CF700A543DB = { isa = PBXGroup; children = ( + C70B94711589B3D6003CC371 /* libz.1.2.5.dylib */, F45C1DFD133F4D9000B3D443 /* BSYahooFinance */, F4EAE173133F4CF700A543DB /* BSYahooFinanceDemo */, F4EAE193133F4CF800A543DB /* BSYahooFinanceTests */, diff --git a/BSYahooFinance/YFStockDetailsLoader.h b/BSYahooFinance/YFStockDetailsLoader.h index 760b0d6..86bf2c4 100644 --- a/BSYahooFinance/YFStockDetailsLoader.h +++ b/BSYahooFinance/YFStockDetailsLoader.h @@ -23,19 +23,19 @@ id delegate; ASIHTTPRequest *internalStockRequest; NSError *internalError; - YFStockDetails *internalStockDetails; - NSString *internalSymbolToLoad; + NSMutableArray *internalStockDetails; + NSArray *internalSymbolsToLoad; BOOL synchronousLoad; } @property (nonatomic, assign) id delegate; @property (nonatomic, readonly) NSError *error; -@property (nonatomic, readonly) YFStockDetails *stockDetails; +@property (nonatomic, readonly) NSArray *stockDetails; @property (nonatomic, assign) BOOL synchronousLoad; + (YFStockDetailsLoader *)loaderWithDelegate:(id)delegate; - (id)initWithDelegate:(id)delegate; -- (void)loadDetails:(NSString *)stockSymbol; +- (void)loadDetails:(NSArray *)stockSymbols; - (void)cancel; @end diff --git a/BSYahooFinance/YFStockDetailsLoader.m b/BSYahooFinance/YFStockDetailsLoader.m index e5140d2..1e8b772 100644 --- a/BSYahooFinance/YFStockDetailsLoader.m +++ b/BSYahooFinance/YFStockDetailsLoader.m @@ -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 @@ -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 @@ -51,12 +51,18 @@ - (id)initWithDelegate:(id)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]; @@ -79,7 +85,7 @@ - (void)cancel self.internalStockRequest = nil; } - self.internalSymbolToLoad = nil; + self.internalSymbolsToLoad = nil; self.internalStockDetails = nil; self.internalError = nil; } @@ -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: @@ -137,8 +150,7 @@ - (NSError *)error return self.internalError; } -- (YFStockDetails *)stockDetails -{ +- (NSArray *)stockDetails { return self.internalStockDetails; } diff --git a/BSYahooFinanceDemo/DetailsViewController.m b/BSYahooFinanceDemo/DetailsViewController.m index 6cbca68..37f2a22 100644 --- a/BSYahooFinanceDemo/DetailsViewController.m +++ b/BSYahooFinanceDemo/DetailsViewController.m @@ -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]; } @@ -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; }