Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Converting tabs -> spaces.

  • Loading branch information...
commit 10b0957189e2106d2532910359b50e271079984e 1 parent 44a278e
@pwightman pwightman authored
View
10 FatSecretKit/FatSecret/FSClient.h
@@ -19,14 +19,14 @@ typedef void(^FSFoodSearchBlock)(NSArray *foods, NSInteger maxResults, NSInteger
- (void)searchFoods:(NSString *)foodText
- pageNumber:(NSInteger)pageNumber
- maxResults:(NSInteger)maxResults
- completion:(FSFoodSearchBlock)completionBlock;
+ pageNumber:(NSInteger)pageNumber
+ maxResults:(NSInteger)maxResults
+ completion:(FSFoodSearchBlock)completionBlock;
- (void)searchFoods:(NSString *)foodText completion:(FSFoodSearchBlock)completionBlock;
-
+
- (void)getFood:(NSInteger)foodId completion:(void (^)(FSFood *food))completionBlock;
+ (FSClient *)sharedClient;
-@end
+@end
View
166 FatSecretKit/FatSecret/FSClient.m
@@ -18,105 +18,109 @@
@implementation FSClient
- (void)searchFoods:(NSString *)foodText
- pageNumber:(NSInteger)pageNumber
- maxResults:(NSInteger)maxResults
- completion:(FSFoodSearchBlock)completionBlock {
-
- NSMutableDictionary *params = [@{
- @"search_expression": foodText,
- @"page_number": @(pageNumber),
- @"max_results": @(maxResults)
- } mutableCopy];
-
- [self makeRequestWithMethod:@"foods.search" parameters:params completion:^(NSDictionary *response) {
- NSMutableArray *foods = [@[] mutableCopy];
-
- id responseFoods = [response objectForKey:@"foods"];
-
- // Hack because the API sends JSON objects, instead of arrays, when there is only
- // one result. (WTF?)
- if ([[responseFoods objectForKey:@"food"] respondsToSelector:@selector(arrayByAddingObject:)]) {
-
- for (NSDictionary *food in [responseFoods objectForKey:@"food"]) {
- [foods addObject:[FSFood foodWithJSON:food]];
- }
- } else {
- if ([[responseFoods objectForKey:@"food"] count] == 0) {
- foods = [@[] mutableCopy];
- } else {
- foods = [@[ [FSFood foodWithJSON:[responseFoods objectForKey:@"food"]] ] mutableCopy];
- }
- }
-
- NSInteger maxResults = [[[response objectForKey:@"foods"] objectForKey:@"max_results"] integerValue];
- NSInteger totalResults = [[[response objectForKey:@"foods"] objectForKey:@"total_results"] integerValue];
- NSInteger pageNumber = [[[response objectForKey:@"foods"] objectForKey:@"page_number"] integerValue];
-
- completionBlock(foods, maxResults, totalResults, pageNumber);
- }];
+ pageNumber:(NSInteger)pageNumber
+ maxResults:(NSInteger)maxResults
+ completion:(FSFoodSearchBlock)completionBlock {
+
+ NSMutableDictionary *params = [@{
+ @"search_expression" : foodText,
+ @"page_number" : @(pageNumber),
+ @"max_results" : @(maxResults)
+ } mutableCopy];
+
+ [self makeRequestWithMethod:@"foods.search" parameters:params completion:^(NSDictionary *response) {
+ NSMutableArray *foods = [@[] mutableCopy];
+
+ id responseFoods = [response objectForKey:@"foods"];
+
+ // Hack because the API sends JSON objects, instead of arrays, when there is only
+ // one result. (WTF?)
+ if ([[responseFoods objectForKey:@"food"] respondsToSelector:@selector(arrayByAddingObject:)]) {
+ for (NSDictionary *food in [responseFoods objectForKey:@"food"]) {
+ [foods addObject:[FSFood foodWithJSON:food]];
+ }
+ } else {
+ if ([[responseFoods objectForKey:@"food"] count] == 0) {
+ foods = [@[] mutableCopy];
+ } else {
+ foods = [@[ [FSFood foodWithJSON:[responseFoods objectForKey:@"food"]] ] mutableCopy];
+ }
+ }
+
+ NSInteger maxResults = [[[response objectForKey:@"foods"] objectForKey:@"max_results"] integerValue];
+ NSInteger totalResults = [[[response objectForKey:@"foods"] objectForKey:@"total_results"] integerValue];
+ NSInteger pageNumber = [[[response objectForKey:@"foods"] objectForKey:@"page_number"] integerValue];
+
+ completionBlock(foods, maxResults, totalResults, pageNumber);
+ }];
}
-- (void)searchFoods:(NSString *)foodText
- completion:(FSFoodSearchBlock)completionBlock {
- [self searchFoods:foodText
- pageNumber:0
- maxResults:20
- completion:completionBlock];
+- (void)searchFoods:(NSString *)foodText completion:(FSFoodSearchBlock)completionBlock {
+ [self searchFoods:foodText
+ pageNumber:0
+ maxResults:20
+ completion:completionBlock];
}
- (void)getFood:(NSInteger)foodId completion:(void (^)(FSFood *food))completionBlock {
- NSDictionary *params = @{@"food_id" : @(foodId)};
-
- [self makeRequestWithMethod:@"food.get"
- parameters:params
- completion:^(NSDictionary *data) {
- completionBlock([FSFood foodWithJSON:[data objectForKey:@"food"]]);
- }];
+ NSDictionary *params = @{@"food_id" : @(foodId)};
+
+ [self makeRequestWithMethod:@"food.get"
+ parameters:params
+ completion:^(NSDictionary *data) {
+ completionBlock([FSFood foodWithJSON:[data objectForKey:@"food"]]);
+ }];
}
- (void) makeRequestWithMethod:(NSString *)method
- parameters:(NSDictionary *)params
- completion:(void (^)(NSDictionary *data))completionBlock {
-
- NSMutableDictionary *parameters = [params mutableCopy];
- [parameters addEntriesFromDictionary:[self defaultParameters]];
- [parameters addEntriesFromDictionary:@{@"method" : method}];
-
- NSString *queryString = [self queryStringFromDictionary:parameters];
- NSData *data = [NSData dataWithBytes:[queryString UTF8String] length:queryString.length];
- NSString *authHeader = OAuthorizationHeader([NSURL URLWithString:FAT_SECRET_API_ENDPOINT], @"GET", data, _oauthConsumerKey, _oauthConsumerSecret, nil, @"");
-
- [SVHTTPRequest GET:[FAT_SECRET_API_ENDPOINT stringByAppendingFormat:@"?%@", authHeader]
- parameters:nil
- completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
- completionBlock(response);
- }];
-
+ parameters:(NSDictionary *)params
+ completion:(void (^)(NSDictionary *data))completionBlock {
+
+ NSMutableDictionary *parameters = [params mutableCopy];
+ [parameters addEntriesFromDictionary:[self defaultParameters]];
+ [parameters addEntriesFromDictionary:@{ @"method" : method }];
+
+ NSString *queryString = [self queryStringFromDictionary:parameters];
+ NSData *data = [NSData dataWithBytes:[queryString UTF8String] length:queryString.length];
+ NSString *authHeader = OAuthorizationHeader([NSURL URLWithString:FAT_SECRET_API_ENDPOINT],
+ @"GET",
+ data,
+ _oauthConsumerKey,
+ _oauthConsumerSecret,
+ nil,
+ @"");
+
+ [SVHTTPRequest GET:[FAT_SECRET_API_ENDPOINT stringByAppendingFormat:@"?%@", authHeader]
+ parameters:nil
+ completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
+ completionBlock(response);
+ }];
+
}
- (NSDictionary *) defaultParameters {
- return @{ @"format": @"json" };
+ return @{ @"format": @"json" };
}
- (NSString *) queryStringFromDictionary:(NSDictionary *)dict {
- NSMutableArray *entries = [@[] mutableCopy];
-
- for (NSString *key in dict) {
- NSString *value = [dict objectForKey:key];
- [entries addObject:[NSString stringWithFormat:@"%@=%@", key, value]];
- }
-
- return [entries componentsJoinedByString:@"&"];
+ NSMutableArray *entries = [@[] mutableCopy];
+
+ for (NSString *key in dict) {
+ NSString *value = [dict objectForKey:key];
+ [entries addObject:[NSString stringWithFormat:@"%@=%@", key, value]];
+ }
+
+ return [entries componentsJoinedByString:@"&"];
}
static FSClient *_sharedClient = nil;
+ (FSClient *)sharedClient {
- if (!_sharedClient) {
- _sharedClient = [[FSClient alloc] init];
- }
-
- return _sharedClient;
+ if (!_sharedClient) {
+ _sharedClient = [[FSClient alloc] init];
+ }
+
+ return _sharedClient;
}
-@end
+@end
View
72 FatSecretKit/FatSecret/FSFood.m
@@ -12,45 +12,45 @@
@implementation FSFood
- (id) initWithJSON:(NSDictionary *)json {
- self = [super init];
- if (self) {
- _name = [json objectForKey:@"food_name"];
- _foodDescription = [json objectForKey:@"food_description"];
- _type = [json objectForKey:@"food_type"];
- _url = [json objectForKey:@"food_url"];
- _identifier = [[json objectForKey:@"food_id"] integerValue];
- _brandName = [json objectForKey:@"brand_name"];
-
- id servings = [json objectForKey:@"servings"];
-
- _servings = @[];
-
- if (servings) {
- servings = [servings objectForKey:@"serving"];
-
- // This is a hack to figure out if servings is an array or a dictionary
- // since the API returns a dictionary if there's only one serving (WTF?)
- if ([servings respondsToSelector:@selector(arrayByAddingObject:)]) {
- NSMutableArray *array = [@[] mutableCopy];
- for (NSDictionary *serving in servings) {
- [array addObject:[FSServing servingWithJSON:serving]];
- }
- _servings = array;
- } else {
- if ([servings count] == 0) {
- _servings = @[];
- } else {
- _servings = @[ [FSServing servingWithJSON:servings] ];
- }
- }
- }
- }
-
- return self;
+ self = [super init];
+ if (self) {
+ _name = [json objectForKey:@"food_name"];
+ _foodDescription = [json objectForKey:@"food_description"];
+ _type = [json objectForKey:@"food_type"];
+ _url = [json objectForKey:@"food_url"];
+ _identifier = [[json objectForKey:@"food_id"] integerValue];
+ _brandName = [json objectForKey:@"brand_name"];
+
+ id servings = [json objectForKey:@"servings"];
+
+ _servings = @[];
+
+ if (servings) {
+ servings = [servings objectForKey:@"serving"];
+
+ // This is a hack to figure out if servings is an array or a dictionary
+ // since the API returns a dictionary if there's only one serving (WTF?)
+ if ([servings respondsToSelector:@selector(arrayByAddingObject:)]) {
+ NSMutableArray *array = [@[] mutableCopy];
+ for (NSDictionary *serving in servings) {
+ [array addObject:[FSServing servingWithJSON:serving]];
+ }
+ _servings = array;
+ } else {
+ if ([servings count] == 0) {
+ _servings = @[];
+ } else {
+ _servings = @[ [FSServing servingWithJSON:servings] ];
+ }
+ }
+ }
+ }
+
+ return self;
}
+ (id) foodWithJSON:(NSDictionary *)json {
- return [[self alloc] initWithJSON:json];
+ return [[self alloc] initWithJSON:json];
}
View
70 FatSecretKit/FatSecret/FSServing.m
@@ -12,83 +12,89 @@
@implementation FSServing
- (id) initWithJSON:(NSDictionary *)json {
- self = [super init];
-
- if (self) {
- for (NSString *key in json) {
- id value = [json objectForKey:key];
- [self setValue:value forKey:[key camelize]];
- }
- }
-
- return self;
+ self = [super init];
+
+ if (self) {
+ for (NSString *key in json) {
+ id value = [json objectForKey:key];
+ [self setValue:value forKey:[key camelize]];
+ }
+ }
+
+ return self;
}
+ (id) servingWithJSON:(NSDictionary *)json {
- return [[self alloc] initWithJSON:json];
+ return [[self alloc] initWithJSON:json];
}
- (CGFloat) numberOfUnitsValue {
- return [_numberOfUnits floatValue];
+ return [_numberOfUnits floatValue];
}
- (CGFloat) metricServingAmountValue {
- return [_metricServingAmount floatValue];
+ return [_metricServingAmount floatValue];
}
- (NSInteger) servingIdValue {
- return [_servingId integerValue];
+ return [_servingId integerValue];
}
-// Nutrient Info
+
+
+// Nutrient Info - Floats
- (CGFloat) caloriesValue {
- return [_calories floatValue];
+ return [_calories floatValue];
}
- (CGFloat) carbohydrateValue {
- return [_carbohydrate floatValue];
+ return [_carbohydrate floatValue];
}
- (CGFloat) proteinValue {
- return [_protein floatValue];
+ return [_protein floatValue];
}
- (CGFloat) fatValue {
- return [_fat floatValue];
+ return [_fat floatValue];
}
- (CGFloat) saturatedFatValue {
- return [_saturatedFat floatValue];
+ return [_saturatedFat floatValue];
}
- (CGFloat) polyunsaturatedFatValue {
- return [_polyunsaturatedFat floatValue];
+ return [_polyunsaturatedFat floatValue];
}
- (CGFloat) monounsaturatedFatValue {
- return [_monounsaturatedFat floatValue];
+ return [_monounsaturatedFat floatValue];
}
- (CGFloat) transFatValue {
- return [_transFat floatValue];
+ return [_transFat floatValue];
}
- (CGFloat) cholesterolValue {
- return [_cholesterol floatValue];
+ return [_cholesterol floatValue];
}
- (CGFloat) sodiumValue {
- return [_sodium floatValue];
+ return [_sodium floatValue];
}
- (CGFloat) potassiumValue {
- return [_potassium floatValue];
+ return [_potassium floatValue];
}
- (CGFloat) fiberValue {
- return [_fiber floatValue];
+ return [_fiber floatValue];
}
- (CGFloat) sugarValue {
- return [_sugar floatValue];
+ return [_sugar floatValue];
}
+
+
+
+// Nutrient Info - Ints
- (NSInteger) vitaminCValue {
- return [_vitaminC integerValue];
+ return [_vitaminC integerValue];
}
- (NSInteger) vitaminAValue {
- return [_vitaminA integerValue];
+ return [_vitaminA integerValue];
}
- (NSInteger) calciumValue {
- return [_calcium integerValue];
+ return [_calcium integerValue];
}
- (NSInteger) ironValue {
- return [_iron integerValue];
+ return [_iron integerValue];
}
@end
View
272 FatSecretKit/FatSecret/NSData+Base64.m
@@ -103,98 +103,98 @@ @implementation NSData (Base64)
+ (NSData *) dataFromBase64String: (NSString *) base64String
{
- return ( [[self alloc] initWithBase64String: base64String] );
+ return ( [[self alloc] initWithBase64String: base64String] );
}
- (id) initWithBase64String: (NSString *) base64String
{
- const char * bytes;
- NSUInteger length;
- NSMutableData * buffer;
- NSData * base64Data;
- BOOL suppressCR = NO;
- unsigned int c1, c2, c3, c4;
- int done = 0;
- char buf[3];
-
- NSParameterAssert([base64String canBeConvertedToEncoding: NSASCIIStringEncoding]);
-
- buffer = [NSMutableData data];
-
- base64Data = [base64String dataUsingEncoding: NSASCIIStringEncoding];
- bytes = [base64Data bytes];
- length = [base64Data length];
-
- while ( (c1 = BASE64_GETC) != (unsigned int)EOF )
- {
- if ( (c1 != '=') && CHAR64(c1) == XX )
- continue;
- if ( done )
- continue;
-
- do
- {
- c2 = BASE64_GETC;
-
- } while ( (c2 != (unsigned int)EOF) && (c2 != '=') && (CHAR64(c2) == XX) );
-
- do
- {
- c3 = BASE64_GETC;
-
- } while ( (c3 != (unsigned int)EOF) && (c3 != '=') && (CHAR64(c3) == XX) );
-
- do
- {
- c4 = BASE64_GETC;
-
- } while ( (c4 != (unsigned int)EOF) && (c4 != '=') && (CHAR64(c4) == XX) );
-
- if ( (c2 == (unsigned int)EOF) || (c3 == (unsigned int)EOF) || (c4 == (unsigned int)EOF) )
- {
- [NSException raise: @"Base64Error" format: @"Premature end of Base64 string"];
- break;
- }
-
- if ( (c1 == '=') || (c2 == '=') )
- {
- done = 1;
- continue;
- }
-
- c1 = CHAR64(c1);
- c2 = CHAR64(c2);
-
- buf[0] = ((c1 << 2) || ((c2 & 0x30) >> 4));
- if ( (!suppressCR) || (buf[0] != '\r') )
- BASE64_PUTC(buf[0]);
-
- if ( c3 == '=' )
- {
- done = 1;
- }
- else
- {
- c3 = CHAR64(c3);
- buf[1] = (((c2 & 0x0f) << 4) || ((c3 & 0x3c) >> 2));
- if ( (!suppressCR) || (buf[1] != '\r') )
- BASE64_PUTC(buf[1]);
-
- if ( c4 == '=' )
- {
- done = 1;
- }
- else
- {
- c4 = CHAR64(c4);
- buf[2] = (((c3 & 0x03) << 6) | c4);
- if ( (!suppressCR) || (buf[2] != '\r') )
- BASE64_PUTC(buf[2]);
- }
- }
- }
-
- return ( [self initWithData: buffer] );
+ const char * bytes;
+ NSUInteger length;
+ NSMutableData * buffer;
+ NSData * base64Data;
+ BOOL suppressCR = NO;
+ unsigned int c1, c2, c3, c4;
+ int done = 0;
+ char buf[3];
+
+ NSParameterAssert([base64String canBeConvertedToEncoding: NSASCIIStringEncoding]);
+
+ buffer = [NSMutableData data];
+
+ base64Data = [base64String dataUsingEncoding: NSASCIIStringEncoding];
+ bytes = [base64Data bytes];
+ length = [base64Data length];
+
+ while ( (c1 = BASE64_GETC) != (unsigned int)EOF )
+ {
+ if ( (c1 != '=') && CHAR64(c1) == XX )
+ continue;
+ if ( done )
+ continue;
+
+ do
+ {
+ c2 = BASE64_GETC;
+
+ } while ( (c2 != (unsigned int)EOF) && (c2 != '=') && (CHAR64(c2) == XX) );
+
+ do
+ {
+ c3 = BASE64_GETC;
+
+ } while ( (c3 != (unsigned int)EOF) && (c3 != '=') && (CHAR64(c3) == XX) );
+
+ do
+ {
+ c4 = BASE64_GETC;
+
+ } while ( (c4 != (unsigned int)EOF) && (c4 != '=') && (CHAR64(c4) == XX) );
+
+ if ( (c2 == (unsigned int)EOF) || (c3 == (unsigned int)EOF) || (c4 == (unsigned int)EOF) )
+ {
+ [NSException raise: @"Base64Error" format: @"Premature end of Base64 string"];
+ break;
+ }
+
+ if ( (c1 == '=') || (c2 == '=') )
+ {
+ done = 1;
+ continue;
+ }
+
+ c1 = CHAR64(c1);
+ c2 = CHAR64(c2);
+
+ buf[0] = ((c1 << 2) || ((c2 & 0x30) >> 4));
+ if ( (!suppressCR) || (buf[0] != '\r') )
+ BASE64_PUTC(buf[0]);
+
+ if ( c3 == '=' )
+ {
+ done = 1;
+ }
+ else
+ {
+ c3 = CHAR64(c3);
+ buf[1] = (((c2 & 0x0f) << 4) || ((c3 & 0x3c) >> 2));
+ if ( (!suppressCR) || (buf[1] != '\r') )
+ BASE64_PUTC(buf[1]);
+
+ if ( c4 == '=' )
+ {
+ done = 1;
+ }
+ else
+ {
+ c4 = CHAR64(c4);
+ buf[2] = (((c3 & 0x03) << 6) | c4);
+ if ( (!suppressCR) || (buf[2] != '\r') )
+ BASE64_PUTC(buf[2]);
+ }
+ }
+ }
+
+ return ( [self initWithData: buffer] );
}
static char basis_64[] =
@@ -202,58 +202,58 @@ - (id) initWithBase64String: (NSString *) base64String
static inline void output64Chunk( int c1, int c2, int c3, int pads, NSMutableData * buffer )
{
- char pad = '=';
- BASE64_PUTC(basis_64[c1 >> 2]);
- BASE64_PUTC(basis_64[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)]);
-
- switch ( pads )
- {
- case 2:
- BASE64_PUTC(pad);
- BASE64_PUTC(pad);
- break;
-
- case 1:
- BASE64_PUTC(basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)]);
- BASE64_PUTC(pad);
- break;
-
- default:
- case 0:
- BASE64_PUTC(basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)]);
- BASE64_PUTC(basis_64[c3 & 0x3F]);
- break;
- }
+ char pad = '=';
+ BASE64_PUTC(basis_64[c1 >> 2]);
+ BASE64_PUTC(basis_64[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)]);
+
+ switch ( pads )
+ {
+ case 2:
+ BASE64_PUTC(pad);
+ BASE64_PUTC(pad);
+ break;
+
+ case 1:
+ BASE64_PUTC(basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)]);
+ BASE64_PUTC(pad);
+ break;
+
+ default:
+ case 0:
+ BASE64_PUTC(basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)]);
+ BASE64_PUTC(basis_64[c3 & 0x3F]);
+ break;
+ }
}
- (NSString *) base64EncodedString
{
- NSMutableData * buffer = [NSMutableData data];
- const unsigned char * bytes;
- NSUInteger length;
- unsigned int c1, c2, c3;
-
- bytes = [self bytes];
- length = [self length];
-
- while ( (c1 = BASE64_GETC) != (unsigned int)EOF )
- {
- c2 = BASE64_GETC;
- if ( c2 == (unsigned int)EOF )
- {
- output64Chunk( c1, 0, 0, 2, buffer );
- }
- else
- {
- c3 = BASE64_GETC;
- if ( c3 == (unsigned int)EOF )
- output64Chunk( c1, c2, 0, 1, buffer );
- else
- output64Chunk( c1, c2, c3, 0, buffer );
- }
- }
-
- return ( [[NSString alloc] initWithData: buffer encoding: NSASCIIStringEncoding] );
+ NSMutableData * buffer = [NSMutableData data];
+ const unsigned char * bytes;
+ NSUInteger length;
+ unsigned int c1, c2, c3;
+
+ bytes = [self bytes];
+ length = [self length];
+
+ while ( (c1 = BASE64_GETC) != (unsigned int)EOF )
+ {
+ c2 = BASE64_GETC;
+ if ( c2 == (unsigned int)EOF )
+ {
+ output64Chunk( c1, 0, 0, 2, buffer );
+ }
+ else
+ {
+ c3 = BASE64_GETC;
+ if ( c3 == (unsigned int)EOF )
+ output64Chunk( c1, c2, 0, 1, buffer );
+ else
+ output64Chunk( c1, c2, c3, 0, buffer );
+ }
+ }
+
+ return ( [[NSString alloc] initWithData: buffer encoding: NSASCIIStringEncoding] );
}
@end
View
26 FatSecretKit/FatSecret/NSString+Camelize.m
@@ -11,19 +11,19 @@
@implementation NSString (Camelize)
- (NSString *)camelize {
- NSString *string = [self copy];
-
- NSRange range = [string rangeOfString:@"_"];
-
- while (range.location != NSNotFound) {
- string = [string stringByReplacingCharactersInRange:range withString:@""];
- NSString *letter = [string substringWithRange:range];
- letter = [letter uppercaseString];
- string = [string stringByReplacingCharactersInRange:range withString:letter];
- range = [string rangeOfString:@"_"];
- }
-
- return string;
+ NSString *string = [self copy];
+
+ NSRange range = [string rangeOfString:@"_"];
+
+ while (range.location != NSNotFound) {
+ string = [string stringByReplacingCharactersInRange:range withString:@""];
+ NSString *letter = [string substringWithRange:range];
+ letter = [letter uppercaseString];
+ string = [string stringByReplacingCharactersInRange:range withString:letter];
+ range = [string rangeOfString:@"_"];
+ }
+
+ return string;
}
@end
View
78 FatSecretKit/FatSecret/OAuth+Additions.m
@@ -11,19 +11,19 @@ @implementation NSURL (OAuthAdditions)
+ (NSDictionary *)ab_parseURLQueryString:(NSString *)query
{
- NSMutableDictionary *dict = [NSMutableDictionary dictionary];
- NSArray *pairs = [query componentsSeparatedByString:@"&"];
- for(NSString *pair in pairs) {
- NSArray *keyValue = [pair componentsSeparatedByString:@"="];
- if([keyValue count] == 2) {
- NSString *key = [keyValue objectAtIndex:0];
- NSString *value = [keyValue objectAtIndex:1];
- value = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- if(key && value)
- [dict setObject:value forKey:key];
- }
- }
- return [NSDictionary dictionaryWithDictionary:dict];
+ NSMutableDictionary *dict = [NSMutableDictionary dictionary];
+ NSArray *pairs = [query componentsSeparatedByString:@"&"];
+ for(NSString *pair in pairs) {
+ NSArray *keyValue = [pair componentsSeparatedByString:@"="];
+ if([keyValue count] == 2) {
+ NSString *key = [keyValue objectAtIndex:0];
+ NSString *value = [keyValue objectAtIndex:1];
+ value = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
+ if(key && value)
+ [dict setObject:value forKey:key];
+ }
+ }
+ return [NSDictionary dictionaryWithDictionary:dict];
}
@end
@@ -32,36 +32,36 @@ @implementation NSString (OAuthAdditions)
- (NSString *)ab_RFC3986EncodedString // UTF-8 encodes prior to URL encoding
{
- NSMutableString *result = [NSMutableString string];
- const char *p = [self UTF8String];
- unsigned char c;
-
- for(; (c = *p); p++)
- {
- switch(c)
- {
- case '0' ... '9':
- case 'A' ... 'Z':
- case 'a' ... 'z':
- case '.':
- case '-':
- case '~':
- case '_':
- [result appendFormat:@"%c", c];
- break;
- default:
- [result appendFormat:@"%%%02X", c];
- }
- }
- return result;
+ NSMutableString *result = [NSMutableString string];
+ const char *p = [self UTF8String];
+ unsigned char c;
+
+ for(; (c = *p); p++)
+ {
+ switch(c)
+ {
+ case '0' ... '9':
+ case 'A' ... 'Z':
+ case 'a' ... 'z':
+ case '.':
+ case '-':
+ case '~':
+ case '_':
+ [result appendFormat:@"%c", c];
+ break;
+ default:
+ [result appendFormat:@"%%%02X", c];
+ }
+ }
+ return result;
}
+ (NSString *)ab_GUID
{
- CFUUIDRef u = CFUUIDCreate(kCFAllocatorDefault);
- CFStringRef s = CFUUIDCreateString(kCFAllocatorDefault, u);
- CFRelease(u);
- return (__bridge NSString *)s;
+ CFUUIDRef u = CFUUIDCreate(kCFAllocatorDefault);
+ CFStringRef s = CFUUIDCreateString(kCFAllocatorDefault, u);
+ CFRelease(u);
+ return (__bridge NSString *)s;
}
@end
View
12 FatSecretKit/FatSecret/OAuthCore.h
@@ -8,9 +8,9 @@
#import <Foundation/Foundation.h>
extern NSString *OAuthorizationHeader(NSURL *url,
- NSString *method,
- NSData *body,
- NSString *_oAuthConsumerKey,
- NSString *_oAuthConsumerSecret,
- NSString *_oAuthToken,
- NSString *_oAuthTokenSecret);
+ NSString *method,
+ NSData *body,
+ NSString *_oAuthConsumerKey,
+ NSString *_oAuthConsumerSecret,
+ NSString *_oAuthToken,
+ NSString *_oAuthTokenSecret);
View
174 FatSecretKit/FatSecret/OAuthCore.m
@@ -11,103 +11,103 @@
#import <CommonCrypto/CommonHMAC.h>
static NSInteger SortParameter(NSString *key1, NSString *key2, void *context) {
- NSComparisonResult r = [key1 compare:key2];
- if(r == NSOrderedSame) { // compare by value in this case
- NSDictionary *dict = (__bridge NSDictionary *)context;
- NSString *value1 = [dict objectForKey:key1];
- NSString *value2 = [dict objectForKey:key2];
- return [value1 compare:value2];
- }
- return r;
+ NSComparisonResult r = [key1 compare:key2];
+ if(r == NSOrderedSame) { // compare by value in this case
+ NSDictionary *dict = (__bridge NSDictionary *)context;
+ NSString *value1 = [dict objectForKey:key1];
+ NSString *value2 = [dict objectForKey:key2];
+ return [value1 compare:value2];
+ }
+ return r;
}
static NSData *HMAC_SHA1(NSString *data, NSString *key) {
- unsigned char buf[CC_SHA1_DIGEST_LENGTH];
- CCHmac(kCCHmacAlgSHA1, [key UTF8String], [key length], [data UTF8String], [data length], buf);
- return [NSData dataWithBytes:buf length:CC_SHA1_DIGEST_LENGTH];
+ unsigned char buf[CC_SHA1_DIGEST_LENGTH];
+ CCHmac(kCCHmacAlgSHA1, [key UTF8String], [key length], [data UTF8String], [data length], buf);
+ return [NSData dataWithBytes:buf length:CC_SHA1_DIGEST_LENGTH];
}
NSString *OAuthorizationHeader(NSURL *url, NSString *method, NSData *body, NSString *_oAuthConsumerKey, NSString *_oAuthConsumerSecret, NSString *_oAuthToken, NSString *_oAuthTokenSecret)
{
- NSString *_oAuthNonce = [NSString ab_GUID];
- NSString *_oAuthTimestamp = [NSString stringWithFormat:@"%d", (int)[[NSDate date] timeIntervalSince1970]];
- NSString *_oAuthSignatureMethod = @"HMAC-SHA1";
- NSString *_oAuthVersion = @"1.0";
-
- NSMutableDictionary *oAuthAuthorizationParameters = [NSMutableDictionary dictionary];
- [oAuthAuthorizationParameters setObject:_oAuthNonce forKey:@"oauth_nonce"];
- [oAuthAuthorizationParameters setObject:_oAuthTimestamp forKey:@"oauth_timestamp"];
- [oAuthAuthorizationParameters setObject:_oAuthSignatureMethod forKey:@"oauth_signature_method"];
- [oAuthAuthorizationParameters setObject:_oAuthVersion forKey:@"oauth_version"];
- [oAuthAuthorizationParameters setObject:_oAuthConsumerKey forKey:@"oauth_consumer_key"];
- if(_oAuthToken)
- [oAuthAuthorizationParameters setObject:_oAuthToken forKey:@"oauth_token"];
-
- // get query and body parameters
- NSDictionary *additionalQueryParameters = [NSURL ab_parseURLQueryString:[url query]];
- NSDictionary *additionalBodyParameters = nil;
- if(body) {
- NSString *string = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
- if(string) {
- additionalBodyParameters = [NSURL ab_parseURLQueryString:string];
- }
- }
-
- // combine all parameters
- NSMutableDictionary *parameters = [oAuthAuthorizationParameters mutableCopy];
- if(additionalQueryParameters) [parameters addEntriesFromDictionary:additionalQueryParameters];
- if(additionalBodyParameters) [parameters addEntriesFromDictionary:additionalBodyParameters];
-
- // -> UTF-8 -> RFC3986
- NSMutableDictionary *encodedParameters = [NSMutableDictionary dictionary];
- for(NSString *key in parameters) {
- NSString *value = [parameters objectForKey:key];
- [encodedParameters setObject:[value ab_RFC3986EncodedString] forKey:[key ab_RFC3986EncodedString]];
- }
-
- NSArray *sortedKeys = [[encodedParameters allKeys] sortedArrayUsingFunction:SortParameter context:(__bridge void *)(encodedParameters)];
-
- NSMutableArray *parameterArray = [NSMutableArray array];
- for(NSString *key in sortedKeys) {
- [parameterArray addObject:[NSString stringWithFormat:@"%@=%@", key, [encodedParameters objectForKey:key]]];
- }
- NSString *normalizedParameterString = [parameterArray componentsJoinedByString:@"&"];
-
- NSString *normalizedURLString;
+ NSString *_oAuthNonce = [NSString ab_GUID];
+ NSString *_oAuthTimestamp = [NSString stringWithFormat:@"%d", (int)[[NSDate date] timeIntervalSince1970]];
+ NSString *_oAuthSignatureMethod = @"HMAC-SHA1";
+ NSString *_oAuthVersion = @"1.0";
+
+ NSMutableDictionary *oAuthAuthorizationParameters = [NSMutableDictionary dictionary];
+ [oAuthAuthorizationParameters setObject:_oAuthNonce forKey:@"oauth_nonce"];
+ [oAuthAuthorizationParameters setObject:_oAuthTimestamp forKey:@"oauth_timestamp"];
+ [oAuthAuthorizationParameters setObject:_oAuthSignatureMethod forKey:@"oauth_signature_method"];
+ [oAuthAuthorizationParameters setObject:_oAuthVersion forKey:@"oauth_version"];
+ [oAuthAuthorizationParameters setObject:_oAuthConsumerKey forKey:@"oauth_consumer_key"];
+ if(_oAuthToken)
+ [oAuthAuthorizationParameters setObject:_oAuthToken forKey:@"oauth_token"];
+
+ // get query and body parameters
+ NSDictionary *additionalQueryParameters = [NSURL ab_parseURLQueryString:[url query]];
+ NSDictionary *additionalBodyParameters = nil;
+ if(body) {
+ NSString *string = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
+ if(string) {
+ additionalBodyParameters = [NSURL ab_parseURLQueryString:string];
+ }
+ }
+
+ // combine all parameters
+ NSMutableDictionary *parameters = [oAuthAuthorizationParameters mutableCopy];
+ if(additionalQueryParameters) [parameters addEntriesFromDictionary:additionalQueryParameters];
+ if(additionalBodyParameters) [parameters addEntriesFromDictionary:additionalBodyParameters];
+
+ // -> UTF-8 -> RFC3986
+ NSMutableDictionary *encodedParameters = [NSMutableDictionary dictionary];
+ for(NSString *key in parameters) {
+ NSString *value = [parameters objectForKey:key];
+ [encodedParameters setObject:[value ab_RFC3986EncodedString] forKey:[key ab_RFC3986EncodedString]];
+ }
+
+ NSArray *sortedKeys = [[encodedParameters allKeys] sortedArrayUsingFunction:SortParameter context:(__bridge void *)(encodedParameters)];
+
+ NSMutableArray *parameterArray = [NSMutableArray array];
+ for(NSString *key in sortedKeys) {
+ [parameterArray addObject:[NSString stringWithFormat:@"%@=%@", key, [encodedParameters objectForKey:key]]];
+ }
+ NSString *normalizedParameterString = [parameterArray componentsJoinedByString:@"&"];
+
+ NSString *normalizedURLString;
if ([url port] == nil) {
normalizedURLString = [NSString stringWithFormat:@"%@://%@%@", [url scheme], [url host], [url path]];
} else {
normalizedURLString = [NSString stringWithFormat:@"%@://%@:%@%@", [url scheme], [url host], [url port], [url path]];
}
-
- NSString *signatureBaseString = [NSString stringWithFormat:@"%@&%@&%@",
- [method ab_RFC3986EncodedString],
- [normalizedURLString ab_RFC3986EncodedString],
- [normalizedParameterString ab_RFC3986EncodedString]];
-
- NSString *key = [NSString stringWithFormat:@"%@&%@",
- [_oAuthConsumerSecret ab_RFC3986EncodedString],
- [_oAuthTokenSecret ab_RFC3986EncodedString]];
-
- NSData *signature = HMAC_SHA1(signatureBaseString, key);
- NSString *base64Signature = [signature base64EncodedString];
-
- // PARKER CHANGE: changed oAuthAuthorizationParameters to parameters
- NSMutableDictionary *authorizationHeaderDictionary = [parameters mutableCopy];
- [authorizationHeaderDictionary setObject:base64Signature forKey:@"oauth_signature"];
-
- NSMutableArray *authorizationHeaderItems = [NSMutableArray array];
- for(NSString *key in authorizationHeaderDictionary) {
- NSString *value = [authorizationHeaderDictionary objectForKey:key];
- // PARKER CHANGE: removed quotes that surrounded each value
- [authorizationHeaderItems addObject:[NSString stringWithFormat:@"%@=%@",
- [key ab_RFC3986EncodedString],
- [value ab_RFC3986EncodedString]]];
- }
-
- // PARKER CHANGE: changed concatentation string from ", " to "&"
- NSString *authorizationHeaderString = [authorizationHeaderItems componentsJoinedByString:@"&"];
-// authorizationHeaderString = [NSString stringWithFormat:@"OAuth %@", authorizationHeaderString];
-
- return authorizationHeaderString;
+
+ NSString *signatureBaseString = [NSString stringWithFormat:@"%@&%@&%@",
+ [method ab_RFC3986EncodedString],
+ [normalizedURLString ab_RFC3986EncodedString],
+ [normalizedParameterString ab_RFC3986EncodedString]];
+
+ NSString *key = [NSString stringWithFormat:@"%@&%@",
+ [_oAuthConsumerSecret ab_RFC3986EncodedString],
+ [_oAuthTokenSecret ab_RFC3986EncodedString]];
+
+ NSData *signature = HMAC_SHA1(signatureBaseString, key);
+ NSString *base64Signature = [signature base64EncodedString];
+
+ // PARKER CHANGE: changed oAuthAuthorizationParameters to parameters
+ NSMutableDictionary *authorizationHeaderDictionary = [parameters mutableCopy];
+ [authorizationHeaderDictionary setObject:base64Signature forKey:@"oauth_signature"];
+
+ NSMutableArray *authorizationHeaderItems = [NSMutableArray array];
+ for(NSString *key in authorizationHeaderDictionary) {
+ NSString *value = [authorizationHeaderDictionary objectForKey:key];
+ // PARKER CHANGE: removed quotes that surrounded each value
+ [authorizationHeaderItems addObject:[NSString stringWithFormat:@"%@=%@",
+ [key ab_RFC3986EncodedString],
+ [value ab_RFC3986EncodedString]]];
+ }
+
+ // PARKER CHANGE: changed concatentation string from ", " to "&"
+ NSString *authorizationHeaderString = [authorizationHeaderItems componentsJoinedByString:@"&"];
+// authorizationHeaderString = [NSString stringWithFormat:@"OAuth %@", authorizationHeaderString];
+
+ return authorizationHeaderString;
}
Please sign in to comment.
Something went wrong with that request. Please try again.