Commit e81100f8 by Brent Gulanowski

Clean up JSON interface on BUYCustomerToken.

parent c8fd3ec9
...@@ -190,7 +190,7 @@ ...@@ -190,7 +190,7 @@
NSURL *url = [self urlForCustomersToken]; NSURL *url = [self urlForCustomersToken];
return [self postRequestForURL:url object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) { return [self postRequestForURL:url object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
if (json && !error) { if (json && !error) {
BUYCustomerToken *authenticatedResponse = [BUYCustomerToken responseWithJSON:json]; BUYCustomerToken *authenticatedResponse = [BUYCustomerToken customerTokenWithJSON:json];
self.customerToken = authenticatedResponse.accessToken; self.customerToken = authenticatedResponse.accessToken;
if (!customerJSON) { if (!customerJSON) {
......
...@@ -31,7 +31,8 @@ ...@@ -31,7 +31,8 @@
@property (nonatomic, copy, readonly) NSString *accessToken; @property (nonatomic, copy, readonly) NSString *accessToken;
@property (nonatomic, copy, readonly) NSDate *expiry; @property (nonatomic, copy, readonly) NSDate *expiry;
@property (nonatomic, copy, readonly) NSString *customerID; @property (nonatomic, copy, readonly) NSString *customerID;
@property (nonatomic, copy, readonly) NSDictionary *JSONDictionary;
+ (BUYCustomerToken *)responseWithJSON:(NSDictionary *)json; + (BUYCustomerToken *)customerTokenWithJSON:(NSDictionary *)json;
@end @end
...@@ -27,9 +27,14 @@ ...@@ -27,9 +27,14 @@
#import "BUYCustomerToken.h" #import "BUYCustomerToken.h"
#import "NSDateFormatter+BUYAdditions.h" #import "NSDateFormatter+BUYAdditions.h"
static NSString * const customerAccessTokenKey = @"customer_access_token";
static NSString * const accessTokenKey = @"access_token";
static NSString * const expiresAtKey = @"expires_at";
static NSString * const customerIDKey = @"customer_id";
@implementation BUYCustomerToken @implementation BUYCustomerToken
+ (BUYCustomerToken *)responseWithJSON:(NSDictionary *)json + (BUYCustomerToken *)customerTokenWithJSON:(NSDictionary *)json
{ {
return [[[self class] alloc] initWithJSON:json]; return [[[self class] alloc] initWithJSON:json];
} }
...@@ -39,13 +44,23 @@ ...@@ -39,13 +44,23 @@
self = [super init]; self = [super init];
if (self) { if (self) {
NSDateFormatter *formatter = [NSDateFormatter dateFormatterForPublications]; NSDateFormatter *formatter = [NSDateFormatter dateFormatterForPublications];
NSDictionary *access = json[@"customer_access_token"]; NSDictionary *access = json[customerAccessTokenKey];
_accessToken = access[@"access_token"]; _accessToken = access[accessTokenKey];
_expiry = [formatter dateFromString:access[@"expires_at"]]; _expiry = [formatter dateFromString:access[expiresAtKey]];
_customerID = [NSString stringWithFormat:@"%@", access[@"customer_id"]]; _customerID = [NSString stringWithFormat:@"%@", access[customerIDKey]];
} }
return self; return self;
} }
- (NSDictionary *)JSONDictionary
{
NSDateFormatter *formatter = [NSDateFormatter dateFormatterForPublications];
return @{
accessTokenKey : _accessToken,
expiresAtKey : [formatter stringFromDate:_expiry],
customerIDKey : _customerID
};
}
@end @end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment