Created
December 1, 2014 09:09
-
-
Save mnbayan/895b5f997a7b46e74714 to your computer and use it in GitHub Desktop.
Made available to be used with RESTKit 0.23.3
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
// | |
// Embedly.m | |
// Embedly | |
// | |
// Created by Mylene Bayan 10/16/2014. | |
// | |
#import "Embedly.h" | |
@implementation Embedly | |
- (id)initWithKey:(NSString *)key delegate:(id)delegate { | |
self.key = key; | |
self.delegate = delegate; | |
return self; | |
} | |
/* API stuff */ | |
- (NSString *)callEmbed:(NSString *)url params:(NSDictionary *)params optimizeImages:(NSInteger)width { | |
NSMutableDictionary *completeParams = [NSMutableDictionary dictionaryWithDictionary:params]; | |
[completeParams setObject:self.key forKey:@"key"]; | |
[completeParams setObject:url forKey:@"url"]; | |
if (width > 0) { | |
[completeParams setObject:[NSString stringWithFormat:@"%ld", (long)width] forKey:@"image_width"]; | |
} | |
return [self fetchEmbedlyApi:@"/1/oembed" withParams:completeParams]; | |
} | |
- (NSString *)callExtract:(NSString *)url params:(NSDictionary *)params optimizeImages:(NSInteger)width { | |
NSMutableDictionary *completeParams = [NSMutableDictionary dictionaryWithDictionary:params]; | |
[completeParams setObject:self.key forKey:@"key"]; | |
[completeParams setObject:url forKey:@"url"]; | |
if (width > 0) { | |
[completeParams setObject:[NSString stringWithFormat:@"%ld", (long)width] forKey:@"image_width"]; | |
} | |
return [self fetchEmbedlyApi:@"/1/extract" withParams:completeParams]; | |
} | |
- (NSString *)callEmbedlyApi:(NSString *)endpoint withUrl:(NSString *)url params:(NSDictionary *)params { | |
NSMutableDictionary *completeParams = [NSMutableDictionary dictionaryWithDictionary:params]; | |
[completeParams setObject:self.key forKey:@"key"]; | |
[completeParams setObject:url forKey:@"url"]; | |
return [self fetchEmbedlyApi:endpoint withParams:completeParams]; | |
} | |
- (NSString *)callEmbedlyApi:(NSString *)endpoint withUrls:(NSArray *)urls params:(NSDictionary *)params { | |
NSMutableDictionary *completeParams = [NSMutableDictionary dictionaryWithDictionary:params]; | |
[completeParams setObject:self.key forKey:@"key"]; | |
// Using NSArray causes [] to be appended to param name. NSSet avoids this. | |
[completeParams setObject:[NSSet setWithArray:urls] forKey:@"urls"]; | |
return [self fetchEmbedlyApi:endpoint withParams:completeParams]; | |
} | |
/* Display stuff */ | |
- (NSString *)buildCroppedImageUrl:(NSString *)url width:(NSInteger)width height:(NSInteger)height | |
{ | |
NSMutableDictionary *completeParams = [[NSMutableDictionary alloc] init]; | |
[completeParams setObject:self.key forKey:@"key"]; | |
[completeParams setObject:url forKey:@"url"]; | |
[completeParams setObject:[NSString stringWithFormat:@"%ld", (long)width] forKey:@"width"]; | |
[completeParams setObject:[NSString stringWithFormat:@"%ld", (long)height] forKey:@"height"]; | |
return [self buildEmbedlyUrl:@"/1/display/crop" withParams:completeParams]; | |
} | |
- (NSString *)buildResizedImageUrl:(NSString *)url width:(NSInteger)width { | |
NSMutableDictionary *completeParams = [[NSMutableDictionary alloc] init]; | |
[completeParams setObject:self.key forKey:@"key"]; | |
[completeParams setObject:url forKey:@"url"]; | |
[completeParams setObject:[NSString stringWithFormat:@"%ld", (long)width] forKey:@"width"]; | |
return [self buildEmbedlyUrl:@"/1/display/resize" withParams:completeParams]; | |
} | |
- (NSString *)buildDisplayUrl:(NSString *)endpoint withUrl:(NSString *)url params:(NSDictionary *)params { | |
NSMutableDictionary *completeParams = [NSMutableDictionary dictionaryWithDictionary:params]; | |
[completeParams setObject:self.key forKey:@"key"]; | |
[completeParams setObject:url forKey:@"url"]; | |
return [self buildEmbedlyUrl:endpoint withParams:completeParams]; | |
} | |
/* Other stuff */ | |
- (NSString *)buildEmbedlyUrl:(NSString *)endpoint withParams:(NSDictionary *)params { | |
NSString *embedlyUrl; | |
if ([endpoint hasPrefix:@"/1/display"]) { | |
embedlyUrl = [NSString stringWithFormat:@"http://i.embed.ly%@", endpoint]; | |
} else { | |
embedlyUrl = [NSString stringWithFormat:@"http://api.embed.ly%@", endpoint]; | |
} | |
NSString *displayQuery = @"?"; | |
NSString *param; | |
for (id key in params) { | |
NSString *paramValue = [params[key] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; | |
if ([displayQuery length] == 1) { | |
displayQuery = [NSString stringWithFormat:@"?%@=%@", key, paramValue]; | |
} else { | |
param = [NSString stringWithFormat:@"&%@=%@", key, paramValue]; | |
displayQuery = [displayQuery stringByAppendingString:param]; | |
} | |
} | |
return [NSString stringWithFormat:@"%@%@", embedlyUrl, displayQuery]; | |
} | |
- (NSString *)fetchEmbedlyApi:(NSString *)endpoint withParams:(NSDictionary *)params { | |
NSString *embedlyUrl = [self buildEmbedlyUrl:endpoint withParams:params]; | |
AFHTTPClient *client = [[AFHTTPClient alloc]initWithBaseURL:[NSURL URLWithString:embedlyUrl]]; | |
[client setDefaultHeader:@"User-Agent" value:@"embedly-ios/1.0"]; | |
[client getPath:embedlyUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { | |
NSError* error; | |
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject | |
options:kNilOptions | |
error:&error]; | |
[[self delegate] embedlySuccess:embedlyUrl withResponse:json endpoint:endpoint operation:operation]; | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
[[self delegate] embedlyFailure:embedlyUrl withError:error endpoint:endpoint operation:operation]; | |
}]; | |
return embedlyUrl; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment