Class: WeatherFetcher::Provider::WorldWeatherOnline
- Inherits:
-
HtmlBasedProvider
- Object
- WeatherFetcher::Provider
- HtmlBasedProvider
- WeatherFetcher::Provider::WorldWeatherOnline
- Defined in:
- lib/weather_fetcher/providers/html_based/world_weather_online.rb
Constant Summary
Constants inherited from HtmlBasedProvider
HtmlBasedProvider::TYPE, HtmlBasedProvider::USER_AGENT
Constants inherited from WeatherFetcher::Provider
Instance Attribute Summary
Attributes inherited from WeatherFetcher::Provider
Class Method Summary collapse
- .api ⇒ Object
-
.api=(_api) ⇒ Object
This provider required API key.
- .provider_name ⇒ Object
-
.weather_updated_every ⇒ Object
How often weather is updated.
Instance Method Summary collapse
- #can_fetch?(p) ⇒ Boolean
-
#initialize(*args) ⇒ WorldWeatherOnline
constructor
Create an instance, definitions can be set here.
- #process(string) ⇒ Object
-
#process_node(node) ⇒ Object
Process json node to Hash for AR.
-
#url(p) ⇒ Object
Url for current provider.
Methods inherited from HtmlBasedProvider
#fetch_and_process_single, #fetch_url, #fetch_url_old
Methods inherited from WeatherFetcher::Provider
#fetch, #fetch_and_process_single, #provider_name, #provider_params, short_class_name, #short_class_name, #store_city_definition, #store_time_costs, #unix_time_today
Constructor Details
#initialize(*args) ⇒ WorldWeatherOnline
Create an instance, definitions can be set here
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/weather_fetcher/providers/html_based/world_weather_online.rb', line 27 def initialize(*args) super(*args) # set api key if exists self.defs.each do |d| if d and d[:classes] and d[:classes][self.class.provider_name] and d[:classes][self.class.provider_name][:key] self.class.api = d[:classes][self.class.provider_name][:key] end end end |
Class Method Details
.api ⇒ Object
21 22 23 24 |
# File 'lib/weather_fetcher/providers/html_based/world_weather_online.rb', line 21 def self.api return nil unless defined?(@@api) @@api end |
.api=(_api) ⇒ Object
This provider required API key
17 18 19 |
# File 'lib/weather_fetcher/providers/html_based/world_weather_online.rb', line 17 def self.api=(_api) @@api = _api unless _api.to_s == "" end |
.provider_name ⇒ Object
7 8 9 |
# File 'lib/weather_fetcher/providers/html_based/world_weather_online.rb', line 7 def self.provider_name "WorldWeatherOnline" end |
.weather_updated_every ⇒ Object
How often weather is updated
12 13 14 |
# File 'lib/weather_fetcher/providers/html_based/world_weather_online.rb', line 12 def self.weather_updated_every 12*HOUR - 240 end |
Instance Method Details
#can_fetch?(p) ⇒ Boolean
43 44 45 46 47 48 49 50 51 |
# File 'lib/weather_fetcher/providers/html_based/world_weather_online.rb', line 43 def can_fetch?(p) return false if not defined? @@api begin url(p).nil? == false rescue false end end |
#process(string) ⇒ Object
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 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/weather_fetcher/providers/html_based/world_weather_online.rb', line 53 def process(string) result = JSON.parse(string) # weather archives as processing output weather_archives = Array.new # fix for empty response return if result.nil? or result["data"].nil? or result["data"]["current_condition"].nil? # current conditions current = result["data"]["current_condition"].first current_time = Time.create_time_from_string_12_utc(nil, current["observation_time"]) h = process_node(current) hc = h.clone.merge( { :wwo_type => :current, :time_created => Time.now, :time_from => current_time - 3600, :time_to => current_time } ) weather_archives << hc # prediction predictions = result["data"]["weather"] # example, there is no prediction on Antarctica if predictions and predictions.kind_of?(Array) predictions.each do |p| h = process_node(p) # is always 0 [:pressure, :cloud_cover, :humidity, :visibility].each do |k| h.delete(k) end # create 2 records using tempMinC and tempMaxC hl = h.clone.merge( { :wwo_type => :prediction_temp_low, :time_created => Time.now, :time_from => Time.create_time_from_string(p["date"], "0:00") - 4 * 3600, :time_to => Time.create_time_from_string(p["date"], "0:00") + 8 * 3600, :temperature => p["tempMinC"].to_i } ) weather_archives << hl # and high hh = h.clone.merge( { :wwo_type => :prediction_temp_high, :time_created => Time.now, :time_from => Time.create_time_from_string(p["date"], "0:00") + 8 * 3600, :time_to => Time.create_time_from_string(p["date"], "0:00") + 20 * 3600, :temperature => p["tempMaxC"].to_i } ) weather_archives << hh end end return WeatherData.factory(weather_archives) end |
#process_node(node) ⇒ Object
Process json node to Hash for AR
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/weather_fetcher/providers/html_based/world_weather_online.rb', line 122 def process_node(node) # http://www.worldweatheronline.com/marine-weather-api.aspx return { :temperature => node["temp_C"].to_i, :wind => node["windspeedKmph"].to_f / 3.6, :pressure => node["pressure"].to_f, :rain => node["precipMM"].to_f, :snow => nil, :provider => self.class.provider_name, :cloud_cover => node["cloudcover"].to_f, :humidity => node["humidity"].to_f, :visibility => node["visibility"].to_f, } end |
#url(p) ⇒ Object
Url for current provider
39 40 41 |
# File 'lib/weather_fetcher/providers/html_based/world_weather_online.rb', line 39 def url(p) "http://api.worldweatheronline.com/free/v1/weather.ashx?q=#{p[:coords][:lat]},#{p[:coords][:lon]}&format=json&num_of_days=5&key=#{@@api}" end |