previsao-clima-tempo 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +71 -9
- data/lib/previsao-clima-tempo.rb +145 -48
- data/lib/previsao-clima-tempo/previsao_dia.rb +7 -6
- data/lib/previsao-clima-tempo/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -18,22 +18,84 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
#From Webservice
|
22
|
+
|
23
|
+
Instantiating a object.The city code should be informed.
|
22
24
|
PrevisaoClimaTempo.new(:codCity => '3156')
|
23
25
|
|
24
|
-
|
25
|
-
Devolve um objeto PrevisaoDia com as informações do dia atual.
|
26
|
+
Returns an object PrevisaoDia with information from the current day.
|
26
27
|
PrevisaoClimaTempo.new(:codCity => '3156').now
|
27
28
|
|
28
|
-
|
29
|
+
Returns an object PrevisaoDia with information the next day.
|
29
30
|
PrevisaoClimaTempo.new(:codCity => '3156').tomorrow
|
30
31
|
|
31
|
-
|
32
|
-
PrevisaoClimaTempo.new(:codCity => '3156').days(13)
|
32
|
+
Returns a collection of objects PrevisaoDia with information of days referenced.
|
33
|
+
PrevisaoClimaTempo.new(:codCity => '3156').days(13) maximum of 13 days from the current day
|
34
|
+
|
35
|
+
Returns an object PrevisaoDia with information the day referenced.
|
36
|
+
PrevisaoClimaTempo.new(:codCity => '3156').day(date)
|
33
37
|
|
34
|
-
|
35
|
-
|
36
|
-
|
38
|
+
#From Page(contains more information than is extracted from the webservice)
|
39
|
+
|
40
|
+
Returns a hash of condtions of weather from page
|
41
|
+
PrevisaoClimaTempo.new(:codCity => '314').nowFromPage
|
42
|
+
return:
|
43
|
+
|
44
|
+
{
|
45
|
+
:last_update: 18:00
|
46
|
+
:wind:
|
47
|
+
:velocity: 3 Km/h
|
48
|
+
:direction: Su-sudeste
|
49
|
+
:moisture: 91%
|
50
|
+
:condition: Poucas nuvens
|
51
|
+
:pression: 1022 hPa
|
52
|
+
:temperature: 13ºC
|
53
|
+
}
|
54
|
+
|
55
|
+
Returns a hash of condtions of weather whith 5 days from page.
|
56
|
+
PrevisaoClimaTempo.new(:codCity => '314').fullFromPage
|
57
|
+
return:
|
58
|
+
|
59
|
+
{
|
60
|
+
1:
|
61
|
+
:last_update: 19:00
|
62
|
+
:date: Sábado, 17/08/2013
|
63
|
+
:condition: Sol com muitas nuvens durante o dia e períodos de céu nublado. Noite
|
64
|
+
com muitas nuvens.
|
65
|
+
:wind:
|
66
|
+
:velocity: 6km/h
|
67
|
+
:direction: Su-sudeste
|
68
|
+
:probability_of_precipitation:
|
69
|
+
:volume: 0mm
|
70
|
+
:percentage: 0%
|
71
|
+
:moisture_relative_complete:
|
72
|
+
:max: 100%
|
73
|
+
:min: 49%
|
74
|
+
:temperature:
|
75
|
+
:max: 16º
|
76
|
+
:min: 7º
|
77
|
+
:uv: Alto
|
78
|
+
:sunrise: 06h13
|
79
|
+
:sunset: 17h36
|
80
|
+
2: ...
|
81
|
+
}
|
82
|
+
|
83
|
+
Returns a hash of condtions of weather whith 5 days from page.
|
84
|
+
If today is 16-12-2013 returns 21,22,23,24,25 trends.
|
85
|
+
PrevisaoClimaTempo.new(:codCity => '314').trendsFromPage
|
86
|
+
return:
|
87
|
+
{
|
88
|
+
1:
|
89
|
+
:date: Quinta-Feira, 22/08/2013
|
90
|
+
:condition: Sol com algumas nuvens. Não chove.
|
91
|
+
:probability_of_precipitation:
|
92
|
+
:volume: 0mm
|
93
|
+
:percentage: 0%
|
94
|
+
:temperature:
|
95
|
+
:max: 23º
|
96
|
+
:min: 7º
|
97
|
+
2:
|
98
|
+
}
|
37
99
|
|
38
100
|
## Dependencies
|
39
101
|
|
data/lib/previsao-clima-tempo.rb
CHANGED
@@ -12,7 +12,7 @@ class PrevisaoClimaTempo
|
|
12
12
|
attr_reader :codCity
|
13
13
|
|
14
14
|
def initialize(option)
|
15
|
-
raise TypeError
|
15
|
+
raise TypeError unless option.kind_of? Hash
|
16
16
|
raise ArgumentError unless option.has_key? :codCity
|
17
17
|
|
18
18
|
@codCity = option[:codCity]
|
@@ -20,11 +20,11 @@ class PrevisaoClimaTempo
|
|
20
20
|
|
21
21
|
|
22
22
|
|
23
|
-
#
|
24
|
-
#
|
23
|
+
# Returns the forecast for the date informed,
|
24
|
+
# maximum current date + 13 days from webservice
|
25
25
|
#
|
26
26
|
# Example:
|
27
|
-
# >> PrevisaoClimaTempo.day("02-03-2012".to_date)
|
27
|
+
# >> PrevisaoClimaTempo.new(:codCity => '314').day("02-03-2012".to_date)
|
28
28
|
#
|
29
29
|
# Arguments:
|
30
30
|
# date: (Date)
|
@@ -44,11 +44,10 @@ class PrevisaoClimaTempo
|
|
44
44
|
end
|
45
45
|
|
46
46
|
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
47
|
+
# Returns a collection of objects PrevisaoDia,
|
48
|
+
# the maximum current date + 13 days from webservice
|
50
49
|
# Example:
|
51
|
-
# >> PrevisaoClimaTempo.days(4)
|
50
|
+
# >> PrevisaoClimaTempo.new(:codCity => '314').days(4)
|
52
51
|
#
|
53
52
|
# Arguments:
|
54
53
|
# qtdDays: (Integer)
|
@@ -74,12 +73,12 @@ class PrevisaoClimaTempo
|
|
74
73
|
end
|
75
74
|
|
76
75
|
|
77
|
-
#
|
78
|
-
#
|
79
|
-
#
|
76
|
+
# Returns an object with
|
77
|
+
# the conditions of weather
|
78
|
+
# next day from webservice
|
80
79
|
#
|
81
80
|
# Example:
|
82
|
-
# >> PrevisaoClimaTempo.
|
81
|
+
# >> PrevisaoClimaTempo.new(:codCity => '314').tomorrow
|
83
82
|
#
|
84
83
|
# Arguments:
|
85
84
|
#
|
@@ -90,25 +89,116 @@ class PrevisaoClimaTempo
|
|
90
89
|
end
|
91
90
|
|
92
91
|
|
93
|
-
#
|
94
|
-
#
|
95
|
-
#
|
92
|
+
# Returns a hash of condtions of weather from page.
|
93
|
+
# It's more complete than the webservice
|
94
|
+
#
|
95
|
+
# Example:
|
96
|
+
# >> PrevisaoClimaTempo.new(:codCity => '314').nowFromPage
|
97
|
+
#
|
98
|
+
# Arguments:
|
99
|
+
#
|
100
|
+
def nowFromPage
|
101
|
+
request ||= requestHtml
|
102
|
+
{
|
103
|
+
:last_update => request.xpath("//span[@class='paragrafo-padrao']").text,
|
104
|
+
:wind => { :velocity => request.xpath("//li[@class='dados-momento-li list-style-none'][4]/span[2]").text,
|
105
|
+
:direction => wind[request.xpath("//li[@class='dados-momento-li list-style-none'][1]/span[2]").text]
|
106
|
+
},
|
107
|
+
:moisture => request.xpath("//li[@class='dados-momento-li list-style-none'][5]/span[2]").text,
|
108
|
+
:condition => request.xpath("//li[@class='dados-momento-li list-style-none'][2]/span[2]").text,
|
109
|
+
:pression => request.xpath("//li[@class='dados-momento-li list-style-none'][3]/span[2]").text,
|
110
|
+
:temperature => request.xpath("//span[@class='left temp-momento top10']").text
|
111
|
+
}
|
112
|
+
end
|
113
|
+
|
114
|
+
# Returns a hash of condtions of weather whith 5 days from page.
|
115
|
+
#
|
116
|
+
# It's more complete than the webservice
|
96
117
|
#
|
97
118
|
# Example:
|
98
|
-
# >> PrevisaoClimaTempo.
|
119
|
+
# >> PrevisaoClimaTempo.new(:codCity => '314').fullFromPage
|
99
120
|
#
|
100
121
|
# Arguments:
|
101
122
|
#
|
102
|
-
def
|
123
|
+
def fullFromPage
|
124
|
+
|
125
|
+
request ||= requestHtml
|
126
|
+
|
127
|
+
@previsoes = Hash.new
|
128
|
+
count = 1
|
129
|
+
|
130
|
+
until count > 5 do
|
131
|
+
@previsoes[count] = {
|
132
|
+
:last_update => request.xpath("//p[@class='clear left paragrafo-padrao']").text[/(\d+)/]+":00",
|
133
|
+
:date => request.xpath("//div[@class='box-prev-completa'][#{count}]//span[@class='data-prev']").text.strip+"/"+Time.now().year.to_s,
|
134
|
+
:condition => request.xpath("//div[@class='box-prev-completa'][#{count}]/span[@class='left left5 paragrafo-padrao top10 fraseologia-prev']").text.strip,
|
135
|
+
:wind => request.xpath("//div[@class='box-prev-completa'][#{count}]//li[@class='velocidade-vento-prev-completa list-style-none']/span[2]").text.strip,
|
136
|
+
:probability_of_precipitation => request.xpath("//div[@class='box-prev-completa'][#{count}]//li[@class='prob-chuva-prev-completa list-style-none']/span[2]").text.strip,
|
137
|
+
:moisture_relative_complete => { :max => request.xpath("//div[@class='box-prev-completa'][#{count}]//li[@class='umidade-relativa-prev-completa list-style-none']/span[2]").text.strip,
|
138
|
+
:min => request.xpath("//div[@class='box-prev-completa'][#{count}]//li[@class='umidade-relativa-prev-completa list-style-none']/span[3]").text.strip},
|
139
|
+
:temperature => { :max => request.xpath("//div[@class='box-prev-completa'][#{count}]//span[@class='max']").text.strip,
|
140
|
+
:min => request.xpath("//div[@class='box-prev-completa'][#{count}]//span[@class='min']").text.strip},
|
141
|
+
:uv => request.xpath("//div[@class='box-prev-completa'][#{count}]//p[@class='left left10 top10 paragrafo-padrao uv-size']/span[1]").text.strip,
|
142
|
+
:sunrise => request.xpath("//div[@class='box-prev-completa'][#{count}]//div[@class='por-do-sol']/span[3]").text.strip,
|
143
|
+
:sunset => request.xpath("//div[@class='box-prev-completa'][#{count}]//div[@class='por-do-sol']/span[6]").text.strip
|
144
|
+
}
|
145
|
+
|
146
|
+
@previsoes[count][:wind] = { :velocity => @previsoes[count][:wind][/(\d+)/]+"km/h" ,
|
147
|
+
:direction => wind[@previsoes[count][:wind].gsub("#{@previsoes[count][:wind][/(\d+)/]}km/h","")]
|
148
|
+
}
|
149
|
+
@probability_of_precipitation = @previsoes[count][:probability_of_precipitation].split('mm')
|
150
|
+
|
151
|
+
@previsoes[count][:probability_of_precipitation] = { :volume => @probability_of_precipitation[0]+"mm" ,
|
152
|
+
:percentage => @probability_of_precipitation[1]
|
153
|
+
}
|
154
|
+
|
155
|
+
count+=1
|
156
|
+
end
|
103
157
|
|
104
|
-
|
158
|
+
@previsoes
|
105
159
|
|
106
160
|
end
|
161
|
+
|
162
|
+
# Returns a hash of condtions of weather whith 5 days from page.
|
163
|
+
# If today is 16-12-2013 returns 21,22,23,24,25 trends.
|
164
|
+
# It's more complete than the webservice
|
165
|
+
#
|
166
|
+
# Example:
|
167
|
+
# >> PrevisaoClimaTempo.new(:codCity => '314').trendsFromPage
|
168
|
+
#
|
169
|
+
# Arguments:
|
170
|
+
#
|
171
|
+
def trendsFromPage
|
172
|
+
|
173
|
+
request ||= requestHtml
|
174
|
+
|
175
|
+
@previsoes = Hash.new
|
176
|
+
count = 1
|
177
|
+
|
178
|
+
until count > 5 do
|
179
|
+
@previsoes[count] = {
|
180
|
+
:date => request.xpath("//div[@class='box-prev-tendencia'][#{count}]//span[@class='data-prev']").text.strip+"/"+Time.now().year.to_s,
|
181
|
+
:condition => request.xpath("//div[@class='box-prev-tendencia'][#{count}]//div[@class='frase-previsao-prev-tendencia']").text.strip,
|
182
|
+
:probability_of_precipitation => request.xpath("//div[@class='box-prev-completa'][#{count}]//li[@class='prob-chuva-prev-completa list-style-none']/span[2]").text.strip,
|
183
|
+
:temperature => { :max => request.xpath("//div[@class='box-prev-tendencia'][#{count}]//span[@class='max']").text.strip,
|
184
|
+
:min => request.xpath("//div[@class='box-prev-tendencia'][#{count}]//span[@class='min']").text.strip}
|
185
|
+
}
|
186
|
+
|
187
|
+
@probability_of_precipitation = @previsoes[count][:probability_of_precipitation].split('mm')
|
188
|
+
|
189
|
+
@previsoes[count][:probability_of_precipitation] = { :volume => @probability_of_precipitation[0]+"mm" ,
|
190
|
+
:percentage => @probability_of_precipitation[1]
|
191
|
+
}
|
192
|
+
count+=1
|
193
|
+
end
|
107
194
|
|
195
|
+
@previsoes
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
private
|
108
200
|
|
109
|
-
#
|
110
|
-
# com as cidades compreendidas
|
111
|
-
# pelo clima tempo
|
201
|
+
# Returns a hash of cities with code, name and abbreviation of the state
|
112
202
|
#
|
113
203
|
# Example:
|
114
204
|
# >> PrevisaoClimaTempo.cities()
|
@@ -121,15 +211,15 @@ class PrevisaoClimaTempo
|
|
121
211
|
|
122
212
|
cities = Array.new
|
123
213
|
|
124
|
-
request ||=
|
214
|
+
request ||= requestXml(url)
|
125
215
|
|
126
216
|
request.xpath('//cidades/cidade').each do |cidadePath|
|
127
217
|
|
128
218
|
cidade = Hash.new
|
129
219
|
|
130
|
-
cidade[:id] = cidadePath.at_xpath('id').text
|
131
|
-
cidade[:uf] = cidadePath.at_xpath('uf').text
|
132
|
-
cidade[:nome] = cidadePath.at_xpath('nome').text
|
220
|
+
cidade[:id] = cidadePath.at_xpath('id').text.strip
|
221
|
+
cidade[:uf] = cidadePath.at_xpath('uf').text.strip
|
222
|
+
cidade[:nome] = cidadePath.at_xpath('nome').text.strip
|
133
223
|
|
134
224
|
cities << cidade
|
135
225
|
|
@@ -138,17 +228,33 @@ class PrevisaoClimaTempo
|
|
138
228
|
cities
|
139
229
|
|
140
230
|
end
|
231
|
+
|
232
|
+
def wind
|
233
|
+
{
|
234
|
+
"N" => "Norte",
|
235
|
+
"S" => "Sul",
|
236
|
+
"E" => "Leste",
|
237
|
+
"W" => "Oeste",
|
238
|
+
"NE" => "Nordeste",
|
239
|
+
"NW" => "Noroeste",
|
240
|
+
"SE" => "Sudeste",
|
241
|
+
"SW" => "Sudoeste",
|
242
|
+
"ENE" => "Lés-nordeste",
|
243
|
+
"ESE" => "Lés-sudeste",
|
244
|
+
"SSE" => "Su-sudeste",
|
245
|
+
"NNE" => "Nor-nordeste",
|
246
|
+
"NNW" => "Nor-noroeste",
|
247
|
+
"SSW" => "Su-sudoeste",
|
248
|
+
"WSW" => "Oés-sudoeste",
|
249
|
+
"WNW" => "Oés-noroeste"
|
250
|
+
}
|
251
|
+
end
|
141
252
|
|
142
|
-
|
143
|
-
# Devolve o doc xml
|
144
|
-
#
|
145
|
-
# Example:
|
146
|
-
# >> PrevisaoClimaTempo.request(url)
|
147
|
-
#
|
253
|
+
|
148
254
|
# Arguments:
|
149
255
|
# url: (String)
|
150
256
|
#
|
151
|
-
def
|
257
|
+
def requestXml(url)
|
152
258
|
begin
|
153
259
|
|
154
260
|
Nokogiri::XML(open(url))
|
@@ -158,13 +264,13 @@ class PrevisaoClimaTempo
|
|
158
264
|
end
|
159
265
|
end
|
160
266
|
|
161
|
-
|
267
|
+
|
268
|
+
def requestHtml
|
269
|
+
request = Net::HTTP.get URI.parse("http://www.climatempo.com.br/previsao-do-tempo/cidade/#{@codCity}/empty")
|
270
|
+
request = Nokogiri::HTML request
|
271
|
+
end
|
162
272
|
|
163
|
-
|
164
|
-
# no máximo a data atual + 13 dias
|
165
|
-
#
|
166
|
-
#
|
167
|
-
#
|
273
|
+
|
168
274
|
# Arguments:
|
169
275
|
# url: (String)
|
170
276
|
# days: (Integer)
|
@@ -172,7 +278,7 @@ class PrevisaoClimaTempo
|
|
172
278
|
#
|
173
279
|
def loadDays(url,days,limit)
|
174
280
|
|
175
|
-
previsoes ||=
|
281
|
+
previsoes ||= requestXml(url)
|
176
282
|
|
177
283
|
previsoes.xpath('//cidade/previsao').each do |previsao|
|
178
284
|
|
@@ -185,15 +291,6 @@ class PrevisaoClimaTempo
|
|
185
291
|
end
|
186
292
|
|
187
293
|
|
188
|
-
|
189
|
-
# Efetua o assign do objeto PrevisaoDia
|
190
|
-
#
|
191
|
-
#
|
192
|
-
# Example:
|
193
|
-
# >> PrevisaoClimaTempo.assign(previsao)
|
194
|
-
#
|
195
|
-
# Arguments:
|
196
|
-
#
|
197
294
|
def assign(previsao)
|
198
295
|
|
199
296
|
PrevisaoDia.new(previsao.at_xpath('dia').text,
|
@@ -6,12 +6,13 @@ class PrevisaoDia
|
|
6
6
|
attr_reader :dia,:tempo,:maxima,:minima,:iuv
|
7
7
|
attr_writer :dia,:tempo,:maxima,:minima,:iuv
|
8
8
|
|
9
|
-
def initialize(dia,tempo,maxima,minima,iuv)
|
10
|
-
@dia
|
11
|
-
@tempo
|
12
|
-
@maxima
|
13
|
-
@minima
|
14
|
-
@iuv
|
9
|
+
def initialize(dia,tempo,maxima,minima,iuv, temperatura = "not found")
|
10
|
+
@dia = dayFormat(dia)
|
11
|
+
@tempo = tempo.length > 3 ? tempo : tempoLabel[tempo]
|
12
|
+
@maxima = maxima
|
13
|
+
@minima = minima
|
14
|
+
@iuv = iuv
|
15
|
+
@temperatura = temperatura
|
15
16
|
end
|
16
17
|
|
17
18
|
def dayFormat(dia)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: previsao-clima-tempo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-08-
|
12
|
+
date: 2013-08-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|