Class: WeatherFetcher::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/weather_fetcher/providers/provider.rb

Direct Known Subclasses

HtmlBasedProvider

Defined Under Namespace

Classes: AllMetSat, AviationWeather, InteriaPl, Noaa, OnetPl, OpenWeatherMap, OpenWeatherMapForecast, TwojaPogodaPl, WorldWeatherOnline, WpPl, Wunderground

Constant Summary collapse

TYPE =

kind of provider: standard, html (self web based), gem (other gem)

:standard
HOUR =

just a constant, seconds in 1 hour

3600

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_defs = Array.new) ⇒ Provider

Create an instance, definitions can be set here



14
15
16
17
18
# File 'lib/weather_fetcher/providers/provider.rb', line 14

def initialize(_defs = Array.new)
  @weathers = Array.new
  @logger = Logger.new(STDOUT)
  self.defs = _defs
end

Instance Attribute Details

#defsObject

Returns the value of attribute defs.



21
22
23
# File 'lib/weather_fetcher/providers/provider.rb', line 21

def defs
  @defs
end

#loggerObject

Returns the value of attribute logger.



22
23
24
# File 'lib/weather_fetcher/providers/provider.rb', line 22

def logger
  @logger
end

#weathersObject (readonly)

Returns the value of attribute weathers.



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

def weathers
  @weathers
end

Class Method Details

.provider_nameObject

Name of provider, should be overrode

Raises:

  • (NotImplementedError)


40
41
42
# File 'lib/weather_fetcher/providers/provider.rb', line 40

def self.provider_name
  raise NotImplementedError
end

.short_class_nameObject



109
110
111
# File 'lib/weather_fetcher/providers/provider.rb', line 109

def self.short_class_name
  self.to_s.gsub(/^.*::/, '')
end

Instance Method Details

#can_fetch?(p = nil) ⇒ Boolean

All parameters are available and we can fetch

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


93
94
95
# File 'lib/weather_fetcher/providers/provider.rb', line 93

def can_fetch?(p = nil)
  raise NotImplementedError
end

#fetch(ignore_errors: true) ⇒ Object

Fetch everything from definitions (defs)



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
# File 'lib/weather_fetcher/providers/provider.rb', line 45

def fetch(ignore_errors: true)
  self.logger.debug "#{self.class.to_s.blue} - #{defs.size.to_s.green} definitions to process"
  a = Array.new
  defs.each do |d|
    begin
      self.logger.debug "#{self.class.to_s.blue} - #{d[:name].to_s.yellow}"

      p = fetch_and_process_single(d)
      p = [] if p.nil?
      p.each do |pw|
        pw.just_fetched!
        pw.next_within!(self.class.weather_updated_every)
      end

    rescue => e
      if ignore_errors
        self.logger.error "#{self.class.to_s.blue} - #{d.inspect} fail"
        self.logger.error e.inspect
        self.logger.error e.backtrace
      else
        raise e
      end

    end
    a += p unless p.nil?
  end
  # add to result array
  @weathers += a

  return a
end

#fetch_and_process_single(d) ⇒ Object

Fetch single definition



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/weather_fetcher/providers/provider.rb', line 78

def fetch_and_process_single(d)
  return nil unless can_fetch?

  @pre_download = Time.now
  body = fetch_url(url(d))
  @pre_process = Time.now
  processed = process(body)
  @post_process = Time.now
  store_time_costs(processed)
  store_city_definition(processed, d)

  return processed
end

#provider_nameObject

Name of provider



35
36
37
# File 'lib/weather_fetcher/providers/provider.rb', line 35

def provider_name
  self.class.provider_name
end

#provider_params(p) ⇒ Object

Return Hash of parameters used for current provider



118
119
120
# File 'lib/weather_fetcher/providers/provider.rb', line 118

def provider_params(p)
  return p[:classes][short_class_name]
end

#short_class_nameObject



113
114
115
# File 'lib/weather_fetcher/providers/provider.rb', line 113

def short_class_name
  self.class.short_class_name
end

#store_city_definition(_processed, _city_def) ⇒ Object



130
131
132
133
134
135
# File 'lib/weather_fetcher/providers/provider.rb', line 130

def store_city_definition(_processed, _city_def)
  return unless _processed.kind_of?(Array)
  _processed.each do |p|
    p.city_hash = _city_def
  end
end

#store_time_costs(_processed) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/weather_fetcher/providers/provider.rb', line 122

def store_time_costs(_processed)
  return unless _processed.kind_of?(Array)
  _processed.each do |p|
    p.time_costs[:download_time] = @pre_process - @pre_download
    p.time_costs[:process_time] = @post_process - @pre_process
  end
end

#unix_time_todayObject



101
102
103
104
105
106
107
# File 'lib/weather_fetcher/providers/provider.rb', line 101

def unix_time_today
  Time.mktime(
    Time.now.year,
    Time.now.month,
    Time.now.day,
    0, 0, 0, 0)
end

#url(p = nil) ⇒ Object

Raises:

  • (NotImplementedError)


97
98
99
# File 'lib/weather_fetcher/providers/provider.rb', line 97

def url(p = nil)
  raise NotImplementedError
end