Class: WeatherFetcher::Provider::OpenWeatherMapForecast

Inherits:
HtmlBasedProvider show all
Defined in:
lib/weather_fetcher/providers/html_based/open_weather_map_forecast.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

#fetch_and_process_single, #fetch_url, #fetch_url_old

Methods inherited from WeatherFetcher::Provider

#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

Constructor Details

This class inherits a constructor from WeatherFetcher::Provider

Class Method Details

.provider_nameObject



7
8
9
# File 'lib/weather_fetcher/providers/html_based/open_weather_map_forecast.rb', line 7

def self.provider_name
  "OpenWeatherMap"
end

.weather_updated_everyObject

How often weather is updated



12
13
14
# File 'lib/weather_fetcher/providers/html_based/open_weather_map_forecast.rb', line 12

def self.weather_updated_every
  12*HOUR - 240
end

Instance Method Details

#can_fetch?(p) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
# File 'lib/weather_fetcher/providers/html_based/open_weather_map_forecast.rb', line 21

def can_fetch?(p)
  begin
    url(p).nil? == false
  rescue
    false
  end
end

#process(string) ⇒ Object



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

def process(string)
  result = JSON.parse(string)

  # fix for empty response
  return if result.nil? or result["list"].nil?

  current_time = Time.mktime(
    Time.now.year,
    Time.now.month,
    Time.now.day,
    Time.now.hour
  )

  weather_data = Array.new

  result["list"].each do |w|
    h_day = {
      :temperature => w["temp"]["day"].to_f,
      :wind => w["speed"].to_f,
      :pressure => w["pressure"].to_f,
      :rain => nil,
      :snow => nil,
      :provider => self.class.provider_name,

      :cloud_cover => nil,
      :humidity => w["humidity"].to_i,
      :visibility => nil,

      :wwo_type => :forecast,
      :time_created => Time.now,
      :time_from => Time.at(w["dt"].to_i - 6*HOUR),
      :time_to => Time.at(w["dt"].to_i + 6*HOUR)
    }

    h_night = {
      :temperature => w["temp"]["night"].to_f,
      :wind => w["speed"].to_f,
      :pressure => w["pressure"].to_f,
      :rain => nil,
      :snow => nil,
      :provider => self.class.provider_name,

      :cloud_cover => nil,
      :humidity => w["humidity"].to_i,
      :visibility => nil,

      :wwo_type => :forecast,
      :time_created => Time.now,
      :time_from => Time.at(w["dt"].to_i + 6*HOUR),
      :time_to => Time.at(w["dt"].to_i + 18*HOUR)
    }

    weather_data << h_day
    weather_data << h_night
  end


  return WeatherData.factory(weather_data)
end

#url(p) ⇒ Object

Url for current provider



17
18
19
# File 'lib/weather_fetcher/providers/html_based/open_weather_map_forecast.rb', line 17

def url(p)
  "http://api.openweathermap.org/data/2.5/forecast/daily?lat=#{p[:coords][:lat]}&lon=#{p[:coords][:lon]}&cnt=15&mode=json&units=metric"
end