Class: WeatherFetcher::Provider::TwojaPogodaPl

Inherits:
HtmlBasedProvider show all
Defined in:
lib/weather_fetcher/providers/html_based/twoja_pogoda_pl.rb

Constant Summary

Constants inherited from HtmlBasedProvider

HtmlBasedProvider::TYPE, HtmlBasedProvider::USER_AGENT

Constants inherited from WeatherFetcher::Provider

HOUR, TYPE

Instance Attribute Summary

Attributes inherited from WeatherFetcher::Provider

#defs, #logger, #weathers

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HtmlBasedProvider

#can_fetch?, #fetch_and_process_single, #fetch_url, #fetch_url_old, #url, weather_updated_every

Methods inherited from WeatherFetcher::Provider

#can_fetch?, #fetch, #fetch_and_process_single, #initialize, #provider_name, #provider_params, #short_class_name, short_class_name, #store_city_definition, #store_time_costs, #unix_time_today, #url

Constructor Details

This class inherits a constructor from WeatherFetcher::Provider

Class Method Details

.provider_nameObject



9
10
11
# File 'lib/weather_fetcher/providers/html_based/twoja_pogoda_pl.rb', line 9

def self.provider_name
  "TwojaPogoda.pl"
end

Instance Method Details

#_process(body_raw) ⇒ Object

Process response body and rip out weather data



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
# File 'lib/weather_fetcher/providers/html_based/twoja_pogoda_pl.rb', line 18

def _process(body_raw)
  data = Array.new
  b = Nokogiri::HTML(body_raw)

  dates = b.css("table.items > tr > th > small")
  dates = dates.collect { |d| Time.parse(d.children.first.to_s) }

  temperatures = Array.new
  winds = Array.new
  clouds = Array.new
  feel_temps = Array.new
  humids = Array.new
  pressures = Array.new
  precips = Array.new

  rows = b.css("table.items > tr")
  rows.each do |row|
    tags = row.css("td > div")
    tags.each_with_index do |tag, i|
      if tag.attribute("class").to_s == "info"
        temperatures << tag.css("strong").children.first.to_s.to_f
      end

      if tag.children.first.to_s == "Wiatr"
        wind = tags[i + 1].children.first.to_s.to_f
        winds << wind
      end

      if tag.children.first.to_s == "Chmury"
        cloud = tags[i + 1].children.first.to_s
        if cloud =~ /(\d{1,2})-(\d{1,2})/
          cloud = ($1.to_i + $2.to_i) / 2
        elsif cloud =~ /(\d{1,2}) /
          cloud = $1.to_i
        else
          cloud = nil
        end
        clouds << cloud
      end

      if tag.children.first.to_s == "T.odczuw."
        feel_temp = tags[i + 1].children.first.to_s.to_f
        feel_temps << feel_temp
      end

      if tag.children.first.to_s == "Wilgotność"
        humid = tags[i + 1].children.first.to_s.to_f
        humids << humid
      end

      if tag.children.first.to_s == "Ciśnienie"
        pressure = tags[i + 1].children.first.to_s.to_f
        pressures << pressure
      end

      if tag.children.first.to_s == "Opady"
        precip = tags[i + 1].children.first.to_s
        if precip =~ /(\d{1,2}\.?\d{1,2})/
          precip = $1.to_f
        else
          precip = nil
        end
        precips << precip
      end

    end
  end

  dates.each_with_index do |d, i|
    data << {
      :time_created => Time.now,
      :time_from => d,
      :time_to => d + 24*3600,
      :temperature => temperatures[i],
      :feel_temperature => feel_temps[i],
      :pressure => pressures[i],
      :wind_kmh => winds[i],
      :wind => winds[i] / 3.6,
      #  :snow => nil, #snows[0][0].to_f,
      :rain => precips[i],
      :cloud_cover => clouds[i],
      :humidity => humids[i],
      :provider => self.class.provider_name
    }
  end

  # puts data.inspect

  return data
end

#process(string) ⇒ Object



13
14
15
# File 'lib/weather_fetcher/providers/html_based/twoja_pogoda_pl.rb', line 13

def process(string)
  return WeatherData.factory(_process(string))
end