Permalink
Browse files

Factored low-level API methods out

  • Loading branch information...
1 parent 688b8ae commit 0e6d661b548e2b8e0f3140fdfa24430fb4f75112 @chriseidhof committed Feb 14, 2012
@@ -33,6 +33,8 @@
8359A97F14EA767600FBA59F /* Album.m in Sources */ = {isa = PBXBuildFile; fileRef = 8359A97E14EA767600FBA59F /* Album.m */; };
8359A98314EA767600FBA59F /* Band.m in Sources */ = {isa = PBXBuildFile; fileRef = 8359A98214EA767600FBA59F /* Band.m */; };
8359A98714EA767600FBA59F /* Track.m in Sources */ = {isa = PBXBuildFile; fileRef = 8359A98614EA767600FBA59F /* Track.m */; };
+ 8359A98C14EA97F400FBA59F /* BandCampAPI.m in Sources */ = {isa = PBXBuildFile; fileRef = 8359A98B14EA97F400FBA59F /* BandCampAPI.m */; };
+ 8359A98D14EA97F400FBA59F /* BandCampAPI.m in Sources */ = {isa = PBXBuildFile; fileRef = 8359A98B14EA97F400FBA59F /* BandCampAPI.m */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -82,6 +84,8 @@
8359A98214EA767600FBA59F /* Band.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Band.m; path = IncrementalStoreTest/Band.m; sourceTree = "<group>"; };
8359A98514EA767600FBA59F /* Track.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Track.h; path = IncrementalStoreTest/Track.h; sourceTree = "<group>"; };
8359A98614EA767600FBA59F /* Track.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Track.m; path = IncrementalStoreTest/Track.m; sourceTree = "<group>"; };
+ 8359A98A14EA97F400FBA59F /* BandCampAPI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BandCampAPI.h; sourceTree = "<group>"; };
+ 8359A98B14EA97F400FBA59F /* BandCampAPI.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BandCampAPI.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -160,6 +164,8 @@
8359A91414E9551200FBA59F /* Supporting Files */,
8359A95014E9554C00FBA59F /* BandCampIS.h */,
8359A95114E9554C00FBA59F /* BandCampIS.m */,
+ 8359A98A14EA97F400FBA59F /* BandCampAPI.h */,
+ 8359A98B14EA97F400FBA59F /* BandCampAPI.m */,
);
path = IncrementalStoreTest;
sourceTree = "<group>";
@@ -329,6 +335,7 @@
8359A97F14EA767600FBA59F /* Album.m in Sources */,
8359A98314EA767600FBA59F /* Band.m in Sources */,
8359A98714EA767600FBA59F /* Track.m in Sources */,
+ 8359A98C14EA97F400FBA59F /* BandCampAPI.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -337,6 +344,7 @@
buildActionMask = 2147483647;
files = (
8359A94714E9551200FBA59F /* IncrementalStoreTestTests.m in Sources */,
+ 8359A98D14EA97F400FBA59F /* BandCampAPI.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -0,0 +1,16 @@
+//
+// BandCampAPI.h
+// IncrementalStoreTest
+//
+// Created by Chris Eidhof on 2/14/12.
+// Copyright (c) 2012 Chris Eidhof. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface BandCampAPI : NSObject
+
++ (NSArray*)apiRequestEntitiesWithName:(NSString*)name predicate:(NSPredicate*)predicate;
++ (NSArray*)apiDiscographyForBandWithId:(NSString*)bandId;
+
+@end
@@ -0,0 +1,84 @@
+//
+// BandCampAPI.m
+// IncrementalStoreTest
+//
+// Created by Chris Eidhof on 2/14/12.
+// Copyright (c) 2012 Chris Eidhof. All rights reserved.
+//
+
+#import "BandCampAPI.h"
+
+@implementation BandCampAPI
+
++ (NSString*)generateQueryPartForPredicate:(NSPredicate*)predicate {
+ if([predicate isKindOfClass:[NSComparisonPredicate class]]) {
+ NSComparisonPredicate* cp = (NSComparisonPredicate*)predicate;
+ if(cp.predicateOperatorType == NSEqualToPredicateOperatorType &&
+ cp.leftExpression.expressionType == NSKeyPathExpressionType &&
+ cp.rightExpression.expressionType == NSConstantValueExpressionType
+ ) {
+ id rightExpression = cp.rightExpression.constantValue;
+ NSString* value = nil;
+ if([rightExpression isKindOfClass:[NSString class]]) {
+ value = [rightExpression stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
+ } else if([rightExpression isKindOfClass:[NSNumber class]]) {
+ value = [rightExpression stringValue];
+ }
+ if(value) {
+ NSString* key = cp.leftExpression.keyPath;
+ NSString* queryPart = [NSString stringWithFormat:@"&%@=%@", key, value];
+ return queryPart;
+ }
+ }
+ }
+ NSLog(@"didn't understand predicate");
+ return nil;
+}
+
++ (NSString*)apiMethodForPredicate:(NSPredicate*)predicate {
+ if([predicate isKindOfClass:[NSComparisonPredicate class]]) {
+ NSComparisonPredicate* cp = (NSComparisonPredicate*)predicate;
+ if(cp.leftExpression.expressionType == NSKeyPathExpressionType &&
+ [cp.leftExpression.keyPath isEqualToString:@"name"]) {
+ return @"search";
+ }
+ }
+ return @"info";
+}
+
++ (NSInteger)apiVersionForEntityName:(NSString*)entityName {
+ if([entityName isEqualToString:@"Band"]) return 3;
+ if([entityName isEqualToString:@"Album"]) return 2;
+ if([entityName isEqualToString:@"Track"]) return 1;
+ if([entityName isEqualToString:@"URL"]) return 1;
+
+ return 1;
+}
+
++ (NSArray*)apiRequestEntitiesWithName:(NSString*)name predicate:(NSPredicate*)predicate {
+ NSString* lowercaseName = [name lowercaseString];
+ NSInteger version = [self apiVersionForEntityName:name];
+ NSString* method = [self apiMethodForPredicate:predicate];
+ NSString* searchURLString = [NSString stringWithFormat:@"http://api.bandcamp.com/api/%@/%d/%@?key=snaefellsjokull", lowercaseName, version, method];
+ if(predicate != nil) {
+ NSString* queryPart = [self generateQueryPartForPredicate:predicate];
+ searchURLString = [searchURLString stringByAppendingString:queryPart];
+ }
+ NSURL* searchURL = [NSURL URLWithString:searchURLString];
+ NSData* data = [NSData dataWithContentsOfURL:searchURL];
+ NSDictionary* response = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
+ if([method isEqualToString:@"search"]) {
+ return [response objectForKey:@"results"];
+ }
+ return [NSArray arrayWithObject:response];
+}
+
++ (NSArray*)apiDiscographyForBandWithId:(NSString*)bandId {
+ // todo cache?
+ NSString* searchURLString = [NSString stringWithFormat:@"http://api.bandcamp.com/api/band/3/discography?key=snaefellsjokull&band_id=%@", bandId];
+ NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:searchURLString]];
+ NSDictionary* discography = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
+ return [discography objectForKey:@"discography"];
+}
+
+@end
@@ -7,6 +7,7 @@
//
#import "BandCampIS.h"
+#import "BandCampAPI.h"
NSString *BANDCAMP_STORE_TYPE = @"BandCampIS";
@@ -19,11 +20,6 @@ - (NSManagedObjectID*)objectIdForNewObjectOfEntity:(NSEntityDescription*)entityD
nativeKey:(NSString*)nativeKey
values:(NSDictionary*)values;
-- (NSString*)apiMethodForPredicate:(NSPredicate*)predicate;
-- (NSInteger)apiVersionForEntityName:(NSString*)entityName;
-- (NSArray*)apiRequestEntitiesWithName:(NSString*)name predicate:(NSPredicate*)predicate;
-- (NSArray*)apiDiscographyForBandWithId:(NSString*)bandId;
-
@end
@implementation BandCampIS
@@ -43,38 +39,13 @@ - (BOOL)loadMetadata:(NSError *__autoreleasing *)error {
return YES;
}
-- (NSString*)generateQueryPartForPredicate:(NSPredicate*)predicate {
- if([predicate isKindOfClass:[NSComparisonPredicate class]]) {
- NSComparisonPredicate* cp = (NSComparisonPredicate*)predicate;
- if(cp.predicateOperatorType == NSEqualToPredicateOperatorType &&
- cp.leftExpression.expressionType == NSKeyPathExpressionType &&
- cp.rightExpression.expressionType == NSConstantValueExpressionType
- ) {
- id rightExpression = cp.rightExpression.constantValue;
- NSString* value = nil;
- if([rightExpression isKindOfClass:[NSString class]]) {
- value = [rightExpression stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- } else if([rightExpression isKindOfClass:[NSNumber class]]) {
- value = [rightExpression stringValue];
- }
- if(value) {
- NSString* key = cp.leftExpression.keyPath;
- NSString* queryPart = [NSString stringWithFormat:@"&%@=%@", key, value];
- return queryPart;
- }
- }
- }
- NSLog(@"didn't understand predicate");
- return nil;
-}
-
- (NSString*)nativeKeyForEntityName:(NSString*)entityName {
NSString* lowercaseName = [entityName lowercaseString];
return [lowercaseName stringByAppendingString:@"_id"];
}
- (id)fetchObjects:(NSFetchRequest*)request withContext:(NSManagedObjectContext*)context {
- NSArray* items = [self apiRequestEntitiesWithName:request.entityName predicate:request.predicate];
+ NSArray* items = [BandCampAPI apiRequestEntitiesWithName:request.entityName predicate:request.predicate];
NSMutableArray* result = [NSMutableArray arrayWithCapacity:[items count]];
NSString* nativeKey = [self nativeKeyForEntityName:request.entityName];
for(NSDictionary* item in items) {
@@ -116,7 +87,7 @@ - (id)newValueForRelationship:(NSRelationshipDescription *)relationship forObjec
if ([relationship.name isEqualToString:@"discography"]) {
id bandId = [self referenceObjectForObjectID:objectID];
NSMutableArray* results = [NSMutableArray array];
- for(NSDictionary* album in [self apiDiscographyForBandWithId:bandId]) {
+ for(NSDictionary* album in [BandCampAPI apiDiscographyForBandWithId:bandId]) {
NSManagedObjectID* oid = [self objectIdForNewObjectOfEntity:relationship.destinationEntity nativeKey:@"album_id" values:album];
[results addObject:oid];
}
@@ -149,52 +120,6 @@ - (NSManagedObjectID*)objectIdForNewObjectOfEntity:(NSEntityDescription*)entityD
#pragma mark API methods
-- (NSString*)apiMethodForPredicate:(NSPredicate*)predicate {
- if([predicate isKindOfClass:[NSComparisonPredicate class]]) {
- NSComparisonPredicate* cp = (NSComparisonPredicate*)predicate;
- if(cp.leftExpression.expressionType == NSKeyPathExpressionType &&
- [cp.leftExpression.keyPath isEqualToString:@"name"]) {
- return @"search";
- }
- }
- return @"info";
-}
-
-- (NSInteger)apiVersionForEntityName:(NSString*)entityName {
- if([entityName isEqualToString:@"Band"]) return 3;
- if([entityName isEqualToString:@"Album"]) return 2;
- if([entityName isEqualToString:@"Track"]) return 1;
- if([entityName isEqualToString:@"URL"]) return 1;
-
- return 1;
-}
-
-- (NSArray*)apiRequestEntitiesWithName:(NSString*)name predicate:(NSPredicate*)predicate {
- NSString* lowercaseName = [name lowercaseString];
- NSInteger version = [self apiVersionForEntityName:name];
- NSString* method = [self apiMethodForPredicate:predicate];
- NSString* searchURLString = [NSString stringWithFormat:@"http://api.bandcamp.com/api/%@/%d/%@?key=snaefellsjokull", lowercaseName, version, method];
- if(predicate != nil) {
- NSString* queryPart = [self generateQueryPartForPredicate:predicate];
- searchURLString = [searchURLString stringByAppendingString:queryPart];
- }
- NSURL* searchURL = [NSURL URLWithString:searchURLString];
- NSData* data = [NSData dataWithContentsOfURL:searchURL];
- NSDictionary* response = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
- if([method isEqualToString:@"search"]) {
- return [response objectForKey:@"results"];
- }
- return [NSArray arrayWithObject:response];
-}
-
-- (NSArray*)apiDiscographyForBandWithId:(NSString*)bandId {
- // todo cache?
- NSString* searchURLString = [NSString stringWithFormat:@"http://api.bandcamp.com/api/band/3/discography?key=snaefellsjokull&band_id=%@", bandId];
- NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:searchURLString]];
- NSDictionary* discography = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
- return [discography objectForKey:@"discography"];
-}
-

0 comments on commit 0e6d661

Please sign in to comment.