Class: WeatherFetcher::Provider::OpenWeatherMap

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

.apiObject



24
25
26
27
# File 'lib/weather_fetcher/providers/html_based/open_weather_map.rb', line 24

def self.api
  return nil unless defined?(@@api)
  @@api
end

.api=(_api) ⇒ Object

This provider could use API key but this version won’t



20
21
22
# File 'lib/weather_fetcher/providers/html_based/open_weather_map.rb', line 20

def self.api=(_api)
  @@api = _api
end

.provider_nameObject



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

def self.provider_name
  "OpenWeatherMap"
end

.weather_updated_everyObject

How often weather is updated



14
15
16
# File 'lib/weather_fetcher/providers/html_based/open_weather_map.rb', line 14

def self.weather_updated_every
  12*HOUR - 240
end

Instance Method Details

#can_fetch?(p) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/weather_fetcher/providers/html_based/open_weather_map.rb', line 34

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

#process(string) ⇒ Object



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

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

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

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

  h = {
    :temperature => result["main"]["temp"].to_f,
    :wind => result["wind"]["speed"].to_f,
    :pressure => result["main"]["pressure"].to_f,
    :rain => nil,
    :snow => nil,
    :provider => self.class.provider_name,

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

    :wwo_type => :current,
    :time_created => Time.now,
    :time_from => current_time,
    :time_to => current_time + 3600
  }

  return WeatherData.factory(h)
end

#url(p) ⇒ Object

Url for current provider



30
31
32
# File 'lib/weather_fetcher/providers/html_based/open_weather_map.rb', line 30

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