Skip to content

Commit

Permalink
refactored to use multiple result classes
Browse files Browse the repository at this point in the history
  • Loading branch information
aufflick committed Mar 22, 2012
1 parent 95da64c commit 0343819
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 51 deletions.
12 changes: 12 additions & 0 deletions Sensis SAPI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
644940D5151AFAB200A64AA6 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 644940AA151AFAB200A64AA6 /* README.md */; };
645EFD3A151B1F4900D50EDE /* Sensis_SAPI_GetByListingIdTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 645EFD38151B1F2F00D50EDE /* Sensis_SAPI_GetByListingIdTests.m */; };
645EFD3D151B20E800D50EDE /* SAPIGetByListingId.m in Sources */ = {isa = PBXBuildFile; fileRef = 645EFD3C151B20E800D50EDE /* SAPIGetByListingId.m */; };
645EFD45151B271800D50EDE /* SAPIListingResult.m in Sources */ = {isa = PBXBuildFile; fileRef = 645EFD44151B271800D50EDE /* SAPIListingResult.m */; };
645EFD4B151B28D800D50EDE /* SAPISearchResult.m in Sources */ = {isa = PBXBuildFile; fileRef = 645EFD4A151B28D800D50EDE /* SAPISearchResult.m */; };
64A70DB0151ADF34005859E4 /* LICENSE in Resources */ = {isa = PBXBuildFile; fileRef = 64A70DAF151ADF34005859E4 /* LICENSE */; };
64A70DB3151AE042005859E4 /* LICENSE_ISO8601DateFormatter in Resources */ = {isa = PBXBuildFile; fileRef = 64A70DB2151AE042005859E4 /* LICENSE_ISO8601DateFormatter */; };
64A70DB6151AE076005859E4 /* SAPIISO8601DateFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = 64A70DB5151AE076005859E4 /* SAPIISO8601DateFormatter.m */; };
Expand Down Expand Up @@ -102,6 +104,10 @@
645EFD38151B1F2F00D50EDE /* Sensis_SAPI_GetByListingIdTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Sensis_SAPI_GetByListingIdTests.m; sourceTree = "<group>"; };
645EFD3B151B20E800D50EDE /* SAPIGetByListingId.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SAPIGetByListingId.h; sourceTree = "<group>"; };
645EFD3C151B20E800D50EDE /* SAPIGetByListingId.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SAPIGetByListingId.m; sourceTree = "<group>"; };
645EFD43151B271800D50EDE /* SAPIListingResult.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SAPIListingResult.h; sourceTree = "<group>"; };
645EFD44151B271800D50EDE /* SAPIListingResult.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SAPIListingResult.m; sourceTree = "<group>"; };
645EFD49151B28D800D50EDE /* SAPISearchResult.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SAPISearchResult.h; sourceTree = "<group>"; };
645EFD4A151B28D800D50EDE /* SAPISearchResult.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SAPISearchResult.m; sourceTree = "<group>"; };
64A70DAF151ADF34005859E4 /* LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = LICENSE; sourceTree = "<group>"; };
64A70DB2151AE042005859E4 /* LICENSE_ISO8601DateFormatter */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = LICENSE_ISO8601DateFormatter; sourceTree = "<group>"; };
64A70DB4151AE076005859E4 /* SAPIISO8601DateFormatter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SAPIISO8601DateFormatter.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -308,6 +314,10 @@
64493DE5151985E100A64AA6 /* SAPIEndpoint.m */,
64493DEA1519871400A64AA6 /* SAPIResult.h */,
64493DEB1519871400A64AA6 /* SAPIResult.m */,
645EFD49151B28D800D50EDE /* SAPISearchResult.h */,
645EFD4A151B28D800D50EDE /* SAPISearchResult.m */,
645EFD43151B271800D50EDE /* SAPIListingResult.h */,
645EFD44151B271800D50EDE /* SAPIListingResult.m */,
64494041151AAEDD00A64AA6 /* SAPIPrivate.h */,
64A70DB2151AE042005859E4 /* LICENSE_ISO8601DateFormatter */,
64A70DB4151AE076005859E4 /* SAPIISO8601DateFormatter.h */,
Expand Down Expand Up @@ -451,6 +461,8 @@
644940B4151AFAB200A64AA6 /* UIImageView+AFNetworking.m in Sources */,
644940D4151AFAB200A64AA6 /* JSONKit.m in Sources */,
645EFD3D151B20E800D50EDE /* SAPIGetByListingId.m in Sources */,
645EFD45151B271800D50EDE /* SAPIListingResult.m in Sources */,
645EFD4B151B28D800D50EDE /* SAPISearchResult.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
5 changes: 0 additions & 5 deletions Sensis SAPI/SAPIEndpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,4 @@

@interface SAPIEndpoint : NSObject

- (SAPIResult *)performQueryWithError:(SAPIError **)error;

- (void)performQueryAsyncSuccess:(void (^)(SAPIResult * result))successBlock
failure:(void (^)(SAPIError * error))failureBlock;

@end
15 changes: 11 additions & 4 deletions Sensis SAPI/SAPIEndpoint.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ - (NSString *)endpoint
return nil;
}

- (Class)resultClass
{
NSAssert(0, @"You can't use SAPIEndpoint directly, you must use one of its subclasses eg. SAPISearch");

return nil;
}

- (NSString *)baseURLString;
{
return [NSString stringWithFormat:@"%@://%@/%@%@/%@",
Expand Down Expand Up @@ -110,7 +117,7 @@ - (id)queryValueForKey:(NSString *)key
#define SAPIQueryWaitingCondition 1
#define SAPIQueryFinishedCondition 2

- (SAPIResult *)performQueryWithError:(SAPIError **)returnError
- (SAPIResult *)_performQueryWithError:(SAPIError **)returnError
{
__block SAPIResult * returnResult = nil;
NSConditionLock * lock = [[NSConditionLock alloc] initWithCondition:SAPIQueryInitialCondition];
Expand Down Expand Up @@ -151,8 +158,8 @@ - (SAPIResult *)performQueryWithError:(SAPIError **)returnError
return [returnResult autorelease];
}

- (void)performQueryAsyncSuccess:(void (^)(SAPIResult * result))successBlock
failure:(void (^)(SAPIError * error))failureBlock
- (void)_performQueryAsyncSuccess:(void (^)(SAPIResult * result))successBlock
failure:(void (^)(SAPIError * error))failureBlock
{
[self setupRequestForQueryAsyncSuccess:successBlock failure:failureBlock];
[self.requestOperation start];
Expand Down Expand Up @@ -190,7 +197,7 @@ - (void)setupRequestForQueryAsyncSuccess:(void (^)(SAPIResult *))successBlock fa
if (jsonCode == SAPIResultSuccess || jsonCode == SAPIResultQueryModified)
{
ok = YES;
successBlock([SAPIResult resultWithJSONDictionary:JSON]);
successBlock([[self resultClass] resultWithJSONDictionary:JSON]);
}
else if (jsonCode == SAPIResultValidationError)
{
Expand Down
7 changes: 6 additions & 1 deletion Sensis SAPI/SAPIGetByListingId.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import "SAPIEndpoint.h"

#import "SAPISearchResult.h"

@interface SAPIGetByListingId : SAPIEndpoint

// See also the endpoint reference for more detail: http://developers.sensis.com.au/docs/endpoint_reference/Get_by_Listing_ID
Expand All @@ -17,6 +19,9 @@
@property (copy) NSString * query; // the unique businessId to search for (obtained from search results)
@property (copy) NSString * businessId; // an alias for query (not KVO compliant)

// The methods to perform the query are in the baseclass header, SAPIEndpoint.h
- (SAPISearchResult *)performQueryWithError:(SAPIError **)error;

- (void)performQueryAsyncSuccess:(void (^)(SAPISearchResult * result))successBlock
failure:(void (^)(SAPIError * error))failureBlock;

@end
17 changes: 17 additions & 0 deletions Sensis SAPI/SAPIGetByListingId.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "SAPIGetByListingId.h"
#import "SAPIPrivate.h"

@implementation SAPIGetByListingId

Expand All @@ -24,6 +25,11 @@ - (NSString *)endpoint
return @"getByListingId";
}

- (Class)resultClass
{
return [SAPISearchResult class];
}

- (NSDictionary *)queryKeys
{
return [NSDictionary dictionaryWithObjectsAndKeys:
Expand All @@ -41,4 +47,15 @@ - (void)setBusinessId:(NSString *)businessId
self.query = businessId;
}

- (SAPISearchResult *)performQueryWithError:(SAPIError **)error
{
return (SAPISearchResult *)[self _performQueryWithError:error];
}

- (void)performQueryAsyncSuccess:(void (^)(SAPISearchResult * result))successBlock
failure:(void (^)(SAPIError * error))failureBlock
{
return [self _performQueryAsyncSuccess:(void (^)(SAPIResult * result))successBlock failure:failureBlock];
}

@end
13 changes: 13 additions & 0 deletions Sensis SAPI/SAPIListingResult.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// SAPIListingResult.h
// Sensis SAPI
//
// Created by Mark Aufflick on 22/03/12.
// Copyright (c) 2012 Pumptheory Pty Ltd. All rights reserved.
//

#import "SAPIResult.h"

@interface SAPIListingResult : SAPIResult

@end
13 changes: 13 additions & 0 deletions Sensis SAPI/SAPIListingResult.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// SAPIListingResult.m
// Sensis SAPI
//
// Created by Mark Aufflick on 22/03/12.
// Copyright (c) 2012 Pumptheory Pty Ltd. All rights reserved.
//

#import "SAPIListingResult.h"

@implementation SAPIListingResult

@end
17 changes: 17 additions & 0 deletions Sensis SAPI/SAPIPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

#import "AFJSONRequestOperation.h"

#import "SAPIEndpoint.h"

@class SAPIError;

extern const NSInteger SAPIResultValidationError;

@interface SAPIEndpoint ()
Expand All @@ -21,5 +25,18 @@ extern const NSInteger SAPIResultValidationError;
- (NSDictionary *)queryKeys;
- (NSString *)queryString;
- (id)queryValueForKey:(NSString *)key;
- (Class)resultClass;

- (SAPIResult *)_performQueryWithError:(SAPIError **)error;

- (void)_performQueryAsyncSuccess:(void (^)(SAPIResult * result))successBlock
failure:(void (^)(SAPIError * error))failureBlock;

@end

@interface SAPIResult ()

+ (id)resultWithJSONDictionary:(NSDictionary *)jsonDictionary;
- (id)initWithJSONDictionary:(NSDictionary *)jsonDictionary;

@end
12 changes: 0 additions & 12 deletions Sensis SAPI/SAPIResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,8 @@ extern const NSInteger SAPIResultQueryModified;

@interface SAPIResult : NSObject

@property (retain) NSArray * results; // This is currently an array of NSDictionaries.
// In the future it will become an array of custom objects (which will respond to "objectForKey:" to maintain compatibility)
@property NSUInteger count;
@property NSUInteger totalResults;
@property NSUInteger currentPage;
@property NSUInteger totalPages;
@property (retain) NSString * executedQuery;
@property (retain) NSString * originalQuery;
@property (retain) NSDate * date;
@property NSUInteger time;
@property NSInteger code;
@property (retain) NSArray * details; // This isn't documented in the API docs, it can contain an array of strings, eg: "Number of pages capped at 50, too many results found."

+ (SAPIResult *)resultWithJSONDictionary:(NSDictionary *)jsonDictionary;
- (id)initWithJSONDictionary:(NSDictionary *)jsonDictionary;

@end
20 changes: 3 additions & 17 deletions Sensis SAPI/SAPIResult.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,14 @@
//

#import "SAPIResult.h"

#import "SAPIISO8601DateFormatter.h"
#import "SAPIPrivate.h"

const NSInteger SAPIResultSuccess = 200;
const NSInteger SAPIResultQueryModified = 206;
const NSInteger SAPIResultValidationError = 400;

@implementation SAPIResult

@synthesize results;
@synthesize count;
@synthesize totalResults;
@synthesize currentPage;
@synthesize totalPages;
@synthesize executedQuery;
@synthesize originalQuery;
@synthesize date;
@synthesize time;
@synthesize code;
@synthesize details;
Expand All @@ -39,14 +30,9 @@ - (id)initWithJSONDictionary:(NSDictionary *)jsonDictionary
{
[jsonDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop)
{
if ([key isEqualToString:@"date"])
{
SAPIISO8601DateFormatter * formatter = [[SAPIISO8601DateFormatter alloc] init];
self.date = [formatter dateFromString:obj];
[formatter release];
}
else if ([self respondsToSelector:NSSelectorFromString(key)])
if ([self respondsToSelector:NSSelectorFromString(key)])
{
NSLog(@"key: %@", key);
[self setValue:obj forKey:key];
}
}];
Expand Down
7 changes: 6 additions & 1 deletion Sensis SAPI/SAPISearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import "SAPIEndpoint.h"

#import "SAPISearchResult.h"

@interface SAPISearch : SAPIEndpoint

// See also the endpoint reference for more detail: http://developers.sensis.com.au/docs/endpoint_reference/Search
Expand All @@ -28,6 +30,9 @@
@property (retain) NSArray * contentArray; // Filter results by content - NSArray of strings - see http://developers.sensis.com.au/docs/using_endpoints/Filtering_by_Content_Type
@property (retain) NSArray * productKeywordArray; // Filter results by keyword - NSArray of strings - see http://developers.sensis.com.au/docs/using_endpoints/Filtering_by_Product_Keyword

// The methods to perform the query are in the baseclass header, SAPIEndpoint.h
- (SAPISearchResult *)performQueryWithError:(SAPIError **)error;

- (void)performQueryAsyncSuccess:(void (^)(SAPISearchResult * result))successBlock
failure:(void (^)(SAPIError * error))failureBlock;

@end
16 changes: 16 additions & 0 deletions Sensis SAPI/SAPISearch.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ - (NSString *)endpoint
return @"search";
}

- (Class)resultClass
{
return [SAPISearchResult class];
}

- (NSDictionary *)queryKeys
{
return [NSDictionary dictionaryWithObjectsAndKeys:
Expand Down Expand Up @@ -89,4 +94,15 @@ - (id)queryValueForKey:(NSString *)key
return [super queryValueForKey:key];
}

- (SAPISearchResult *)performQueryWithError:(SAPIError **)error
{
return (SAPISearchResult *)[self _performQueryWithError:error];
}

- (void)performQueryAsyncSuccess:(void (^)(SAPISearchResult * result))successBlock
failure:(void (^)(SAPIError * error))failureBlock
{
return [self _performQueryAsyncSuccess:(void (^)(SAPIResult * result))successBlock failure:failureBlock];
}

@end
24 changes: 24 additions & 0 deletions Sensis SAPI/SAPISearchResult.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// SAPISearchResult.h
// Sensis SAPI
//
// Created by Mark Aufflick on 22/03/12.
// Copyright (c) 2012 Pumptheory Pty Ltd. All rights reserved.
//

#import "SAPIResult.h"

@interface SAPISearchResult : SAPIResult

@property (retain) NSArray * results; // This is currently an array of NSDictionaries.
// In the future it will become an array of custom objects (which will respond to "objectForKey:" to maintain compatibility)

@property NSUInteger count;
@property NSUInteger totalResults;
@property NSUInteger currentPage;
@property NSUInteger totalPages;
@property (retain) NSString * executedQuery;
@property (retain) NSString * originalQuery;
@property (retain) NSDate * date;

@end
38 changes: 38 additions & 0 deletions Sensis SAPI/SAPISearchResult.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// SAPISearchResult.m
// Sensis SAPI
//
// Created by Mark Aufflick on 22/03/12.
// Copyright (c) 2012 Pumptheory Pty Ltd. All rights reserved.
//

#import "SAPISearchResult.h"

#import "SAPIISO8601DateFormatter.h"

@implementation SAPISearchResult

@synthesize results;
@synthesize count;
@synthesize totalResults;
@synthesize currentPage;
@synthesize totalPages;
@synthesize executedQuery;
@synthesize originalQuery;
@synthesize date;

- (void)setValue:(id)value forKey:(NSString *)key
{
if ([key isEqualToString:@"date"] && [value isKindOfClass:[NSString class]])
{
SAPIISO8601DateFormatter * formatter = [[SAPIISO8601DateFormatter alloc] init];
self.date = [formatter dateFromString:value];
[formatter release];
}
else
{
[super setValue:value forKey:key];
}
}

@end
Loading

0 comments on commit 0343819

Please sign in to comment.