Class: PrevisaoClimaTempo

Inherits:
Object
  • Object
show all
Defined in:
lib/previsao-clima-tempo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option) ⇒ PrevisaoClimaTempo

Returns a new instance of PrevisaoClimaTempo.

Raises:

  • (TypeError)


14
15
16
17
18
19
# File 'lib/previsao-clima-tempo.rb', line 14

def initialize(option)
  raise TypeError     unless option.kind_of? Hash
  raise ArgumentError unless option.has_key? :codCity

  @codCity = option[:codCity]
end

Instance Attribute Details

#codCityObject (readonly)

Returns the value of attribute codCity.



12
13
14
# File 'lib/previsao-clima-tempo.rb', line 12

def codCity
  @codCity
end

Instance Method Details

#day(date) ⇒ Object

Returns the forecast for the date informed, maximum current date + 13 days from webservice

Example:

>> PrevisaoClimaTempo.new(:codCity => '314').day("02-03-2012".to_date)

Arguments:

date: (Date)


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/previsao-clima-tempo.rb', line 32

def day(date)
  
  days = self.days(14)
  
  day = Object
  
  days.each do |dayEach|
    break day = dayEach if dayEach.dia == date.strftime("%d-%m-%Y")   
  end
  
  day
   
end

#days(qtdDays) ⇒ Object

Returns a collection of objects PrevisaoDia, the maximum current date + 13 days from webservice Example:

>> PrevisaoClimaTempo.new(:codCity => '314').days(4)

Arguments:

qtdDays: (Integer)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/previsao-clima-tempo.rb', line 55

def days(qtdDays)
  
  days = Array.new

   urlPadrao    = "http://servicos.cptec.inpe.br/XML/cidade/7dias/#{@codCity}/previsao.xml"
   
   urlEstendida = "http://servicos.cptec.inpe.br/XML/cidade/#{@codCity}/estendida.xml"
   
   loadDays(urlPadrao,days,qtdDays)
   
   if(qtdDays > 4)
     
     loadDays(urlEstendida,days,qtdDays)
     
   end
   
   days
   
end

#fullFromPageObject

Returns a hash of condtions of weather whith 5 days from page.

It’s more complete than the webservice

Example:

>> PrevisaoClimaTempo.new(:codCity => '314').fullFromPage

Arguments:



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/previsao-clima-tempo.rb', line 123

def fullFromPage
  
 request ||= requestHtml
 
 @previsoes = Hash.new
 count = 1
 
 until count > 5 do
      @previsoes[count] = {    
                               :last_update                  => request.xpath("//p[@class='clear left paragrafo-padrao']").text[/(\d+)/]+":00",
                               :date                         => request.xpath("//div[@class='box-prev-completa'][#{count}]//span[@class='data-prev']").text.strip+"/"+Time.now().year.to_s,
                               :condition                    => request.xpath("//div[@class='box-prev-completa'][#{count}]/span[@class='left left5 paragrafo-padrao top10 fraseologia-prev']").text.strip,
                               :wind                         => request.xpath("//div[@class='box-prev-completa'][#{count}]//li[@class='velocidade-vento-prev-completa list-style-none']/span[2]").text.strip,
                               :probability_of_precipitation => request.xpath("//div[@class='box-prev-completa'][#{count}]//li[@class='prob-chuva-prev-completa list-style-none']/span[2]").text.strip,
                               :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,
                                                                  :min => request.xpath("//div[@class='box-prev-completa'][#{count}]//li[@class='umidade-relativa-prev-completa list-style-none']/span[3]").text.strip},
                               :temperature                  => { :max => request.xpath("//div[@class='box-prev-completa'][#{count}]//span[@class='max']").text.strip,
                                                                  :min => request.xpath("//div[@class='box-prev-completa'][#{count}]//span[@class='min']").text.strip},
                               :uv                           => request.xpath("//div[@class='box-prev-completa'][#{count}]//p[@class='left left10 top10 paragrafo-padrao uv-size']/span[1]").text.strip,
                               :sunrise                      => request.xpath("//div[@class='box-prev-completa'][#{count}]//div[@class='por-do-sol']/span[3]").text.strip,
                               :sunset                       => request.xpath("//div[@class='box-prev-completa'][#{count}]//div[@class='por-do-sol']/span[6]").text.strip
                           }
                           
      @previsoes[count][:wind] = { :velocity  => @previsoes[count][:wind][/(\d+)/]+"km/h" ,
                                   :direction => wind[@previsoes[count][:wind].gsub("#{@previsoes[count][:wind][/(\d+)/]}km/h","")]
                                 }
      @probability_of_precipitation = @previsoes[count][:probability_of_precipitation].split('mm')                           
                                 
      @previsoes[count][:probability_of_precipitation] = { :volume     => @probability_of_precipitation[0]+"mm" ,
                                                           :percentage => @probability_of_precipitation[1]
                                                         }
       
     count+=1
   end
   
   @previsoes
   
end

#nowFromPageObject

Returns a hash of condtions of weather from page. It’s more complete than the webservice

Example:

>> PrevisaoClimaTempo.new(:codCity => '314').nowFromPage

Arguments:



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/previsao-clima-tempo.rb', line 100

def nowFromPage
  request ||= requestHtml
  {
     :last_update => request.xpath("//span[@class='paragrafo-padrao']").text,
     :wind        => { :velocity  => request.xpath("//li[@class='dados-momento-li list-style-none'][4]/span[2]").text,
                       :direction => wind[request.xpath("//li[@class='dados-momento-li list-style-none'][1]/span[2]").text]
                     },
     :moisture    => request.xpath("//li[@class='dados-momento-li list-style-none'][5]/span[2]").text,
     :condition   => request.xpath("//li[@class='dados-momento-li list-style-none'][2]/span[2]").text,
     :pression    => request.xpath("//li[@class='dados-momento-li list-style-none'][3]/span[2]").text,
     :temperature => request.xpath("//span[@class='left temp-momento top10']").text
  }
end

#tomorrowObject

Returns an object with the conditions of weather next day from webservice

Example:

>> PrevisaoClimaTempo.new(:codCity => '314').tomorrow

Arguments:



85
86
87
88
89
# File 'lib/previsao-clima-tempo.rb', line 85

def tomorrow()
   
   self.day(Time.now + 1.days)
   
end

#trendsFromPageObject

Returns a hash of condtions of weather whith 5 days from page. If today is 16-12-2013 returns 21,22,23,24,25 trends. It’s more complete than the webservice

Example:

>> PrevisaoClimaTempo.new(:codCity => '314').trendsFromPage

Arguments:



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/previsao-clima-tempo.rb', line 171

def trendsFromPage
  
  request ||= requestHtml
   
  @previsoes = Hash.new
  count = 1
     
  until count > 5 do
      @previsoes[count] = {
                               :date                         => request.xpath("//div[@class='box-prev-tendencia'][#{count}]//span[@class='data-prev']").text.strip+"/"+Time.now().year.to_s,
                               :condition                    => request.xpath("//div[@class='box-prev-tendencia'][#{count}]//div[@class='frase-previsao-prev-tendencia']").text.strip,
                               :probability_of_precipitation => request.xpath("//div[@class='box-prev-completa'][#{count}]//li[@class='prob-chuva-prev-completa list-style-none']/span[2]").text.strip,
                               :temperature                  => { :max => request.xpath("//div[@class='box-prev-tendencia'][#{count}]//span[@class='max']").text.strip,
                                                                  :min => request.xpath("//div[@class='box-prev-tendencia'][#{count}]//span[@class='min']").text.strip}
                           }
                                 
      @probability_of_precipitation = @previsoes[count][:probability_of_precipitation].split('mm')                           
                                 
      @previsoes[count][:probability_of_precipitation] = { :volume     => @probability_of_precipitation[0]+"mm" ,
                                                           :percentage => @probability_of_precipitation[1]
                                                         }
      count+=1
   end
   
   @previsoes
   
end