Category prefixing and string constants #1

Merged
merged 4 commits into from Feb 26, 2014
@@ -1,9 +1,9 @@
@interface NSDictionary (XNGOAuth1Additions)
-+ (id)dictionaryFromQueryString:(NSString *)queryString;
++ (id)xng_dictionaryFromQueryString:(NSString *)queryString;
-- (id)initWithQueryString:(NSString *)queryString;
+- (id)xng_initWithQueryString:(NSString *)queryString;
-- (NSString *)queryStringRepresentation;
+- (NSString *)xng_queryStringRepresentation;
@end
@@ -3,29 +3,29 @@
@implementation NSDictionary (XNGOAuth1Additions)
-+ (id)dictionaryFromQueryString:(NSString *)queryString {
- return [[NSDictionary alloc] initWithQueryString:queryString];
++ (id)xng_dictionaryFromQueryString:(NSString *)queryString {
+ return [[NSDictionary alloc] xng_initWithQueryString:queryString];
}
-- (id)initWithQueryString:(NSString *)queryString {
+- (id)xng_initWithQueryString:(NSString *)queryString {
NSArray *components = [queryString componentsSeparatedByString:@"&"];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
for (NSString *component in components) {
NSArray *keyValue = [component componentsSeparatedByString:@"="];
- NSString *key = [keyValue[0] URLDecode];
- NSString *value = [keyValue[1] URLDecode];
+ NSString *key = [keyValue[0] xng_URLDecode];
+ NSString *value = [keyValue[1] xng_URLDecode];
[dictionary setObject:value forKey:key];
}
return dictionary;
}
-- (NSString *)queryStringRepresentation {
+- (NSString *)xng_queryStringRepresentation {
NSMutableArray *paramArray = [NSMutableArray array];
[self enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
- NSString *param = [NSString stringWithFormat:@"%@=%@", [key URLEncode], [value URLEncode]];
+ NSString *param = [NSString stringWithFormat:@"%@=%@", [key xng_URLEncode], [value xng_URLEncode]];
[paramArray addObject:param];
}];
@@ -1,7 +1,7 @@
@interface NSString (XNGOAuth1Additions)
-- (NSString *)URLEncode;
+- (NSString *)xng_URLEncode;
-- (NSString *)URLDecode;
+- (NSString *)xng_URLDecode;
@end
@@ -2,7 +2,7 @@
@implementation NSString (XNGOAuth1Additions)
-- (NSString *)URLEncode {
+- (NSString *)xng_URLEncode {
return (__bridge_transfer NSString *)
CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(__bridge CFStringRef)self,
@@ -11,7 +11,7 @@ - (NSString *)URLEncode {
kCFStringEncodingUTF8);
}
-- (NSString *)URLDecode {
+- (NSString *)xng_URLDecode {
return (__bridge_transfer NSString *)
CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,
(__bridge CFStringRef)self,
@@ -1,6 +1,11 @@
#import "XNGOAuthToken.h"
#import "NSDictionary+XNGOAuth1Additions.h"
+static NSString *const XNGOAuthTokenTokenKey = @"oauth_token";
+static NSString *const XNGOAuthTokenSecretKey = @"oauth_token_secret";
+static NSString *const XNGOAuthTokenVerifierKey = @"oauth_verifier";
+static NSString *const XNGOAuthTokenDurationKey = @"oauth_token_duration";
+
@interface XNGOAuthToken ()
@property (nonatomic) NSDate *expiration;
@@ -27,19 +32,19 @@ - (id)initWithQueryString:(NSString *)queryString {
return nil;
}
- NSDictionary *attributes = [NSDictionary dictionaryFromQueryString:queryString];
+ NSDictionary *attributes = [NSDictionary xng_dictionaryFromQueryString:queryString];
if (attributes.allKeys.count == 0) {
return nil;
}
- NSString *token = attributes[@"oauth_token"];
- NSString *secret = attributes[@"oauth_token_secret"];
- NSString *verifier = attributes[@"oauth_verifier"];
+ NSString *token = attributes[XNGOAuthTokenTokenKey];
+ NSString *secret = attributes[XNGOAuthTokenSecretKey];
+ NSString *verifier = attributes[XNGOAuthTokenVerifierKey];
NSDate *expiration;
- if (attributes[@"oauth_token_duration"]) {
- expiration = [NSDate dateWithTimeIntervalSinceNow:[attributes[@"oauth_token_duration"] doubleValue]];
+ if (attributes[XNGOAuthTokenDurationKey]) {
+ expiration = [NSDate dateWithTimeIntervalSinceNow:[attributes[XNGOAuthTokenDurationKey] doubleValue]];
}
self = [self initWithToken:token secret:secret expiration:expiration];
@@ -48,7 +53,7 @@ - (id)initWithQueryString:(NSString *)queryString {
_verifier = verifier;
NSMutableDictionary *mutableUserInfo = [attributes mutableCopy];
- [mutableUserInfo removeObjectsForKeys:@[@"oauth_token", @"oauth_token_secret", @"oauth_verifier", @"oauth_token_duration"]];
+ [mutableUserInfo removeObjectsForKeys:@[XNGOAuthTokenTokenKey, XNGOAuthTokenSecretKey, XNGOAuthTokenVerifierKey, XNGOAuthTokenDurationKey]];
if (mutableUserInfo.allKeys.count > 0) {
_userInfo = [NSDictionary dictionaryWithDictionary:mutableUserInfo];
}
@@ -12,15 +12,15 @@ @implementation NSDictionaryAdditionsTests
- (void)testInitializer {
NSString *queryString = @"param1=HELLO&param2=XING";
- NSDictionary *resultDict = [[NSDictionary alloc] initWithQueryString:queryString];
+ NSDictionary *resultDict = [[NSDictionary alloc] xng_initWithQueryString:queryString];
expect(resultDict[@"param1"]).to.equal(@"HELLO");
expect(resultDict[@"param2"]).to.equal(@"XING");
}
- (void)testClassMethod {
NSString *queryString = @"param1=HELLO&param2=XING";
- NSDictionary *resultDict = [NSDictionary dictionaryFromQueryString:queryString];
+ NSDictionary *resultDict = [NSDictionary xng_dictionaryFromQueryString:queryString];
expect(resultDict[@"param1"]).to.equal(@"HELLO");
expect(resultDict[@"param2"]).to.equal(@"XING");
@@ -29,8 +29,8 @@ - (void)testClassMethod {
- (void)testQueryStringRepresentation {
NSDictionary *dictionary = @{
@"param1": @"HELLO",
- @"param2":@"XING"};
- NSString *queryString = [dictionary queryStringRepresentation];
+ @"param2": @"XING"};
+ NSString *queryString = [dictionary xng_queryStringRepresentation];
expect(queryString).to.equal(@"param1=HELLO&param2=XING");
}