Class: WeatherFetcher::MetarProvider

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

Constant Summary collapse

TYPE =
:metar
MAX_METAR_TIME_THRESHOLD =

treat metars as not current when time_from is not within time range

4*3600

Constants inherited from HtmlBasedProvider

HtmlBasedProvider::USER_AGENT

Constants inherited from Provider

Provider::HOUR

Instance Attribute Summary

Attributes inherited from Provider

#defs, #logger, #weathers

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HtmlBasedProvider

#fetch_url, #fetch_url_old, #url

Methods inherited from Provider

#fetch, #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



52
53
54
# File 'lib/weather_fetcher/providers/metar_provider.rb', line 52

def self.provider_name
  "MetarProvider"
end

.weather_updated_everyObject

How often weather is updated



74
75
76
# File 'lib/weather_fetcher/providers/metar_provider.rb', line 74

def self.weather_updated_every
  10*60
end

Instance Method Details

#can_fetch?(p) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
# File 'lib/weather_fetcher/providers/metar_provider.rb', line 65

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

#fetch_and_process_single(p) ⇒ Object

Get processed weather for one definition



12
13
14
15
16
17
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
# File 'lib/weather_fetcher/providers/metar_provider.rb', line 12

def fetch_and_process_single(p)
  return nil unless can_fetch?(p)

  @pre_download = Time.now
  url = url_for_metar(metar(p))
  body = fetch_url(url)
  @pre_process = Time.now
  metars = process(body)
  metars = [metars] unless metars.kind_of?(Array)
  processed = Array.new

  metars.each do |m|
    m = SimpleMetarParser::Parser.parse(m)

    if m.valid? and (m.time_from - Time.now).abs < MAX_METAR_TIME_THRESHOLD
      processed << {
        :time_created => Time.now,
        :time_from => m.time_from,
        :time_to => m.time_to,
        :temperature => m.temperature.degrees,
        :pressure => m.pressure.hpa,
        :wind_kmh => m.wind.kmh,
        :wind => m.wind.mps,
        :snow_metar => m.specials.snow_metar,
        :rain_metar => m.specials.rain_metar,
        :provider => self.class.provider_name,
        :metar_string => m.raw
      }
    end

  end

  processed = WeatherData.factory(processed)
  @post_process = Time.now
  store_time_costs(processed)
  store_city_definition(processed, p)

  return processed
end

#metar(p) ⇒ Object

Metar



61
62
63
# File 'lib/weather_fetcher/providers/metar_provider.rb', line 61

def metar(p)
  p[:metar]
end

#url_for_metar(metar_city) ⇒ Object



56
57
58
# File 'lib/weather_fetcher/providers/metar_provider.rb', line 56

def url_for_metar(metar_city)
  raise 'Not implemented'
end