summaryrefslogtreecommitdiff
path: root/Juick/Message.m
diff options
context:
space:
mode:
Diffstat (limited to 'Juick/Message.m')
-rw-r--r--Juick/Message.m57
1 files changed, 21 insertions, 36 deletions
diff --git a/Juick/Message.m b/Juick/Message.m
index 2c35ef4..f6fae85 100644
--- a/Juick/Message.m
+++ b/Juick/Message.m
@@ -10,43 +10,28 @@
@implementation Message
--(id)initWithDictionary:(NSDictionary *)dictionary
-{
- Message *result = [[Message alloc] init];
- result.MID = [dictionary objectForKey:@"mid"];
- result.RID = [dictionary objectForKey:@"rid"];
- NSString *text = [[dictionary objectForKey:@"body"] stringByDecodingHTMLEntities];
- NSDictionary *userDict = [dictionary objectForKey:@"user"];
- result.user = [userDict objectForKey:@"uname"];
- result.userID = [userDict objectForKey:@"uid"];
- NSArray *tagsArray = [dictionary objectForKey:@"tags"];
- if ([tagsArray count] > 0) {
- result.text = [[NSString alloc] initWithFormat:@"%@\n%@", [tagsArray componentsJoinedByString:@", "], text];
- } else {
- result.text = text;
- }
- return result;
-}
-+(NSArray *) pullNextFromURL:(NSURL *)url {
- // Set the gesture
- NSError *error;
- NSString *data = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
- if (data == nil) {
- NSLog(@"Download Error: %@", error);
- return nil;
- }
- NSArray *resultObject = [NSJSONSerialization JSONObjectWithData:[data dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
- if (resultObject == nil) {
- NSLog(@"JSON Error: %@", error);
- return nil;
- }
- NSMutableArray *result = [NSMutableArray array];
- for (NSDictionary *message in resultObject) {
- Message *msg = [[Message alloc] initWithDictionary:message];
- [result addObject:msg];
- }
- return [result copy];
++(void) pullNextFromURL:(NSURL *)url callback:(void(^)(NSArray *))callback {
+ RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Message class]];
+ [mapping addAttributeMappingsFromDictionary:@
+ {
+ @"user.uname": @"user",
+ @"user.uid" : @"userID",
+ @"body" : @"text",
+ @"mid" : @"MID",
+ @"rid" : @"RID",
+ @"tags" : @"tags"
+ }];
+ RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:nil];
+ NSURLRequest *request = [NSURLRequest requestWithURL:url];
+ RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request
+ responseDescriptors:@[responseDescriptor]];
+ [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
+ callback([mappingResult array]);
+ } failure:^(RKObjectRequestOperation *operation, NSError *error) {
+ NSLog(@"REST Error: %@", error);
+ }];
+ [operation start];
}
@end