summaryrefslogtreecommitdiff
path: root/Juick/Message.m
blob: 2c35ef478ac61ce6464493124ca78dc1f18c9c32 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
//  Message.m
//  Juick
//
//  Created by Vitaly Takmazov on 29.10.13.
//  Copyright (c) 2013 com.juick. All rights reserved.
//

#import "Message.h"

@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];
}

@end