-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFetchData.m
More file actions
110 lines (75 loc) · 2.79 KB
/
FetchData.m
File metadata and controls
110 lines (75 loc) · 2.79 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// fetchData.m
// Task
//
// Created by Ahmed Azab on 1/19/16.
// Copyright © 2016 Ahmed Azab. All rights reserved.
//
#import "FetchData.h"
#import "Tweet.h"
@implementation FetchData
{
NSArray * orderedTweetsArray;
}
#pragma mark - Call API
-(void)getData
{
NSString *urlString = @"https://176.58.126.236/twitzer/";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.securityPolicy setAllowInvalidCertificates:YES];
[manager.securityPolicy setValidatesDomainName:NO];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSArray *myArray = (NSArray *)responseObject;
[self parseResponseObject:myArray];
[self.delegate fetchDataDidFinish:self];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self.delegate fetchDataFailedWithError:error];
}];
}
-(void)parseResponseObject:(NSArray *)list
{
NSLog(@"");
NSMutableArray * tweetsList = [[NSMutableArray alloc]init];
// Build an array from the dictionary for easy access to each entry
for (int i=0;i<[list count];i++)
{
Tweet * tweet = [Tweet new];
NSDictionary * dict = [list objectAtIndex:i];
NSString * text = [dict objectForKey:@"text"];
NSArray * textArray = [text componentsSeparatedByString:@">"];
if ([textArray count] == 2)
text = [textArray objectAtIndex:1];
tweet.text = text;
NSString * url = [dict objectForKey:@"url"];
tweet.url = url;
NSString * img = [dict objectForKey:@"img"];
tweet.img = img;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[dict objectForKey:@"date"]doubleValue]];
tweet.date = date;
BOOL today = [[NSCalendar currentCalendar] isDateInToday:date];
if (today)
{
tweet.dateText = @"Today";
}
else
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMM dd"];
tweet.dateText = [formatter stringFromDate:[NSDate date]];
}
[tweetsList addObject:tweet];
}
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
orderedTweetsArray = [tweetsList sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
}
#pragma mark - Helping Methods
-(int)numberOfFetchedData
{
return (int)orderedTweetsArray.count ;
}
-(Tweet *)tweetAtIndex:(NSInteger)index
{
return [orderedTweetsArray objectAtIndex:index];
}
@end