Last active
December 20, 2015 18:29
-
-
Save aspitz/6176634 to your computer and use it in GitHub Desktop.
A simple class that makes the Ziptastic REST service available to iOS and OSX developers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
extern NSString *const ZZiptasticCityKey; | |
extern NSString *const ZZiptasticStateKey; | |
extern NSString *const ZZiptasticCountryKey; | |
extern NSString *const ZURLHTTPErrorCode; | |
typedef void (^ZZiptasticBlock)(NSDictionary *location, NSError *error); | |
@interface ZZiptastic : NSObject | |
+ (NSDictionary *)locationFromZipcode:(NSString *)zipcode error:(NSError **)error; | |
+ (void)locationFromZipcode:(NSString *)zipcode queue:(NSOperationQueue *)queue completionBlock:(ZZiptasticBlock)block; | |
+ (void)locationFromZipcode:(NSString *)zipcode completionBlock:(ZZiptasticBlock)block; | |
@end | |
#import "ZZiptastic.h" | |
NSString *const ZZiptasticURL = @"http://zip.elevenbasetwo.com/v2/US/"; | |
NSString *const ZZiptasticCountryKey = @"country"; | |
NSString *const ZZiptasticStateKey = @"state"; | |
NSString *const ZZiptasticCityKey = @"city"; | |
NSString *const ZURLHTTPErrorCode = @"ZURLHTTPErrorCode"; | |
@implementation ZZiptastic | |
+ (NSDictionary *)locationFromZipcode:(NSString *)zipcode error:(NSError **)error{ | |
NSURLRequest *urlRequest = [ZZiptastic urlRequestForZipcode:zipcode]; | |
NSHTTPURLResponse *httpResponse = nil; | |
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&httpResponse error:error]; | |
return [ZZiptastic processData:data fromResponse:httpResponse toRequest:urlRequest error:error]; | |
} | |
+ (void)locationFromZipcode:(NSString *)zipcode completionBlock:(ZZiptasticBlock)block{ | |
[ZZiptastic locationFromZipcode:zipcode queue:[[NSOperationQueue alloc]init] completionBlock:block]; | |
} | |
+ (void)locationFromZipcode:(NSString *)zipcode queue:(NSOperationQueue *)queue completionBlock:(ZZiptasticBlock)block{ | |
NSURLRequest *urlRequest = [ZZiptastic urlRequestForZipcode:zipcode]; | |
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { | |
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; | |
NSDictionary *locationDictionary = [ZZiptastic processData:data fromResponse:httpResponse toRequest:urlRequest error:&connectionError]; | |
block(locationDictionary, connectionError); | |
}]; | |
} | |
+ (NSURLRequest *)urlRequestForZipcode:(NSString *)zipcode{ | |
// build Ziptastic url request | |
NSString *urlString = [NSString stringWithFormat:@"%@%@", ZZiptasticURL, zipcode]; | |
NSURL *url = [NSURL URLWithString:urlString]; | |
return [NSURLRequest requestWithURL:url]; | |
} | |
+ (NSDictionary *)processData:(NSData *)data fromResponse:(NSHTTPURLResponse *)httpResponse toRequest:(NSURLRequest *)urlRequest error:(NSError **)error{ | |
NSDictionary *locationDictionary = nil; | |
if ((error != nil) && (*error == nil) && (httpResponse.statusCode != 200)){ | |
NSDictionary *userInfo = @{NSLocalizedDescriptionKey : [NSHTTPURLResponse localizedStringForStatusCode:httpResponse.statusCode], | |
ZURLHTTPErrorCode : @(httpResponse.statusCode), | |
NSURLErrorFailingURLErrorKey : urlRequest.URL}; | |
*error = [[NSError alloc] initWithDomain:NSURLErrorDomain code:NSURLErrorBadServerResponse userInfo:userInfo]; | |
} else { | |
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:error]; | |
if (((error == nil) || (*error == nil)) && [json isKindOfClass:[NSDictionary class]]){ | |
locationDictionary = [NSDictionary dictionaryWithDictionary:json]; | |
} | |
} | |
return locationDictionary; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment