Class: WeatherFetcher::HtmlBasedProvider

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

Constant Summary collapse

TYPE =
:html_based
USER_AGENT =
"Chrome 32.0.1667.0"

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 Provider

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

.weather_updated_everyObject

How often weather is updated



57
58
59
# File 'lib/weather_fetcher/providers/html_based_provider.rb', line 57

def self.weather_updated_every
  4*HOUR
end

Instance Method Details

#can_fetch?(p) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/weather_fetcher/providers/html_based_provider.rb', line 48

def can_fetch?(p)
  begin
    url(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
# File 'lib/weather_fetcher/providers/html_based_provider.rb', line 12

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

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

  return processed
end

#fetch_url(url) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/weather_fetcher/providers/html_based_provider.rb', line 33

def fetch_url(url)
  http = Curl::Easy.perform(url) do |curl|
    curl.headers["User-Agent"] = USER_AGENT
    curl.enable_cookies = true
    curl.follow_location = true
  end
  s = http.body_str
  return s
end

#fetch_url_old(url) ⇒ Object

Download url



27
28
29
30
31
# File 'lib/weather_fetcher/providers/html_based_provider.rb', line 27

def fetch_url_old(url)
  s = Net::HTTP.get2(URI.parse(url), { 'User-Agent' => USERAGENT })
  puts s.inspect, url
  return s
end

#url(p) ⇒ Object

Url for current provider



44
45
46
# File 'lib/weather_fetcher/providers/html_based_provider.rb', line 44

def url(p)
  provider_params(p)[:url]
end