-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIAWeatherBugClient.m
More file actions
135 lines (98 loc) · 3.76 KB
/
Copy pathIAWeatherBugClient.m
File metadata and controls
135 lines (98 loc) · 3.76 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// WRWeatherBugClient.m
// Weather
//
// Created by Mark Adams on 7/7/12.
// BSD License
//
#import "IAWeatherBugClient.h"
@interface IAWeatherBugClient ()
@property (nonatomic) NSOperationQueue *operationQueue;
@property (copy, nonatomic) NSString *APIKey;
@end
#pragma mark -
static NSString *const kWRWeatherBugBaseURLString = @"http://i.wxbug.net/REST/Direct/";
static NSString *const kWRWeatherBugObservations = @"GetObs.ashx?ic=1";
static NSString *const kWRWeatherBugDailyForecasts = @"GetForecast.ashx?&ht=d&ht=t&c=US&l=en&ih=0";
static NSString *const kWRWeatherBugHourlyForecasts = @"GetForecastHourly.ashx?ht=d&ht=fl";
@implementation IAWeatherBugClient
- (id)initWithAPIKey:(NSString *)key
{
self = [super init];
if (!self)
return nil;
_operationQueue = [[NSOperationQueue alloc] init];
_APIKey = [key copy];
return self;
}
- (void)currentConditionsForLatitude:(double)latitude longitude:(double)longitude withCompletionBlock:(WRWeatherBugClientCompletionBlock)block
{
if (!block)
return;
NSString *endpoint = [kWRWeatherBugObservations stringByAppendingFormat:@"&la=%f&lo=%f", latitude, longitude];
[self requestValueForEndpoint:endpoint completionBlock:^(id result, NSError *error) {
if (!result)
{
block(nil, error);
return;
}
if (![result isKindOfClass:[NSDictionary class]])
return;
block(result, nil);
}];
}
- (void)hourlyForecastForLatitude:(double)latitude longitude:(double)longitude withCompletionBlock:(WRWeatherBugClientCompletionBlock)block
{
if (!block)
return;
NSString *endpoint = [kWRWeatherBugHourlyForecasts stringByAppendingFormat:@"&la=%f&lo=%f", latitude, longitude];
[self requestValueForEndpoint:endpoint completionBlock:^(id result, NSError *error) {
if (!result)
{
block(nil, error);
return;
}
block(result, nil);
}];
}
- (void)dailyForecastForLatitude:(double)latitude longitude:(double)longitude numberOfDays:(NSUInteger)numberOfDays withCompletionBlock:(WRWeatherBugClientCompletionBlock)block
{
if (!block)
return;
NSString *endpoint = [kWRWeatherBugDailyForecasts stringByAppendingFormat:@"&nf=%i&la=%f&lo=%f", numberOfDays, latitude, longitude];
[self requestValueForEndpoint:endpoint completionBlock:^(id result, NSError *error) {
if (!result)
{
block(nil, error);
return;
}
block(result, nil);
}];
}
- (void)requestValueForEndpoint:(NSString *)endPoint completionBlock:(WRWeatherBugClientCompletionBlock)block
{
if (!block)
return;
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@&api_key=%@", kWRWeatherBugBaseURLString, endPoint, self.APIKey]];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[NSURLConnection sendAsynchronousRequest:request queue:self.operationQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *requestError) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
id result = nil;
NSError *JSONError = nil;
switch (statusCode)
{
case 200:
result = [NSJSONSerialization JSONObjectWithData:data options:0 error:&JSONError];
break;
case 404:
// Handle this as necessary
break;
default:
break;
}
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
block(result, requestError ?: JSONError);
}];
}];
}
@end