summaryrefslogtreecommitdiff
path: root/Volta/Models/VoltaReading.m
blob: bd2d5fd74e94509a8f5f8fb07bb6d0b879e2ee47 (plain)
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
53
54
55
//
//  VoltaReading.m
//  Volta
//
//  Created by Ronny Fenrich on 2013-06-14.
//  Copyright (c) 2013 Decoder. All rights reserved.
//

#import "VoltaReading.h"

@implementation VoltaReading


+ (VoltaReading *)initFromJSON:(NSDictionary *)jsonData;
{
    VoltaReading *result = [[VoltaReading alloc] init];

    result.id = [[jsonData objectForKey:@"id"] integerValue];
    result.usage = [[jsonData objectForKey:@"usage"] integerValue];
    result.duration = [[jsonData objectForKey:@"duration"] integerValue];
    result.cost = [[jsonData objectForKey:@"cost"] integerValue];

    // Dates
    NSDateFormatter *inputDateParser = [[NSDateFormatter alloc] init];
    [inputDateParser setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
    NSString *startDateString = [jsonData objectForKey:@"start"] == [NSNull null] ? nil : [jsonData objectForKey:@"start"];
    if (!startDateString)
    {
        result.startDate = nil;
    }
    else
    {
        result.startDate = [inputDateParser dateFromString:startDateString];
    }

    return result;
}


- (NSString *)dictionaryKey
{
    if (self.startDate)
    {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateStyle:NSDateFormatterFullStyle];
        [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
        return [dateFormatter stringFromDate:self.startDate];
    }
    else
    {
        return @"unknown date";
    }
}

@end