-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCYahooWeatherParser.m
138 lines (114 loc) · 4.59 KB
/
SCYahooWeatherParser.m
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
136
137
138
//
// SCYahooWeatherParser.m
// SCYahooWeather
//
// Version History
// ----
// * 0.1 (2011-06-25): created by sweetchili
// * 0.2 (2013-03-18): upgraded to support ARC by josh-fuggle
// * 0.3 (2013-03-18): added support for delegation by josh-fuggle
//
// This file is part of SCYahooWeather.
//
// SCYahooWeather is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SCYahooWeather is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SCYahooWeather. If not, see <http://www.gnu.org/licenses/>.
//
#import "SCYahooWeatherParser.h"
#define kSCYahooWeatherRequestURL @"http://weather.yahooapis.com/forecastrss?w=%d&u=%@"
#define kSCYahooWeatherXMLKeyConditionTag @"yweather:condition"
#define kSCYahooWeatherXMLKeyTemp @"temp"
#define kSCYahooWeatherXMLKeyText @"text"
#define kSCYahooWeatherXMLKeyCondition @"code"
@interface SCYahooWeatherParser () <NSXMLParserDelegate>
@property (weak, readwrite) id <SCYahooWeatherParserDelegate> delegate;
@property (copy, readwrite) SCYahooWeatherInfoBlock block;
@property (strong) NSDictionary *data;
@property (readwrite) NSInteger WOEID;
@property (readwrite) SCWeatherUnit unit;
// keep a strong reference to self while parsing is underway
@property (strong) SCYahooWeatherParser *selfReference;
@end
@implementation SCYahooWeatherParser
#pragma mark - Initialization
#pragma mark Initialize with Delegates
- (id)initWithWOEID:(NSInteger)WOEID weatherUnit:(SCWeatherUnit)unit delegate:(id <SCYahooWeatherParserDelegate>)delegate
{
if (self = [super init]) {
self.WOEID = WOEID;
self.unit = unit;
self.delegate = delegate;
self.selfReference = self;
}
return self;
}
+ (id)weatherParserWithWOEID:(NSInteger)WOEID weatherUnit:(SCWeatherUnit)unit delegate:(id <SCYahooWeatherParserDelegate>)delegate
{
return [[SCYahooWeatherParser alloc] initWithWOEID:WOEID weatherUnit:unit delegate:delegate];
}
#pragma mark Initialize with Blocks
- (id)initWithWOEID:(NSInteger)WOEID weatherUnit:(SCWeatherUnit)unit block:(SCYahooWeatherInfoBlock)block
{
if (self = [super init]) {
self.WOEID = WOEID;
self.unit = unit;
self.block = block;
self.selfReference = self;
}
return self;
}
+ (id)weatherParserWithWOEID:(NSInteger)WOEID weatherUnit:(SCWeatherUnit)unit block:(SCYahooWeatherInfoBlock)block
{
return [[SCYahooWeatherParser alloc] initWithWOEID:WOEID weatherUnit:unit block:block];
}
#pragma mark - Parsing
- (void)parse
{
NSString *URLString = [NSString stringWithFormat:kSCYahooWeatherRequestURL, self.WOEID, [self weatherUniString]];
NSURL *URL = [[NSURL alloc] initWithString:URLString];
// Begin parsing in a background thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
xmlParser.delegate = self;
[xmlParser parse];
});
}
#pragma mark NSXMLParser
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if(![elementName isEqualToString:kSCYahooWeatherXMLKeyConditionTag]) return;
SCWeather *weather = [SCWeather new];
weather.weatherString = attributeDict[kSCYahooWeatherXMLKeyText];
weather.temperature = [attributeDict[kSCYahooWeatherXMLKeyTemp] intValue];
weather.condition = [attributeDict[kSCYahooWeatherXMLKeyCondition] intValue];
dispatch_async(dispatch_get_main_queue(), ^{
// If the client chose delegates as the primary callback
if (self.delegate) {
[self.delegate yahooWeatherParser:self recievedWeatherInformation:weather];
}
// If the client chose blocks as the callback
if (self.block) {
self.block(self, weather);
}
});
}
-(void)parserDidEndDocument:(NSXMLParser *)parser
{
// Weather parse is no longer required
self.selfReference = nil;
}
#pragma mark - Helper Methods
- (NSString *)weatherUniString
{
return (self.unit == SCWeatherUnitCelcius ? @"c" : @"f");
}
@end