Class: IshikawaAirPollution

Inherits:
Object
  • Object
show all
Defined in:
lib/ishikawa_air_pollution.rb,
lib/ishikawa_air_pollution/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Constructor Details

#initializeIshikawaAirPollution

Returns a new instance of IshikawaAirPollution.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ishikawa_air_pollution.rb', line 7

def initialize
  @agent = Mechanize.new
  @agent.user_agent_alias = 'Mac Safari'
  @agent.request_headers = {
    'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-language' => 'ja-jp',
    'Accept-charset' => '',
    'Accept-encoding' => 'gzip, deflate',
    'Keep-alive' => nil
  }
  parse_page(fetch_page)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ishikawa_air_pollution.rb', line 45

def method_missing(name, *args)
  if @material.has_key? name.to_s
    locations = @location.inject([]) { |buf, (key, val)|
      if val.has_key? name.to_s 
        buf << {
          "name" => key,
          "value" => val[name.to_s]["value"]
        } 
      end
      buf 
    }
    @material[name.to_s].merge({
      "locations" => locations
    })
  else
    super
  end
end

Instance Method Details

#location(name) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/ishikawa_air_pollution.rb', line 24

def location(name)
  raise "unexist location" unless @location.has_key?(name)
  @location[name].inject({}) { |buf, (key, val)|
    buf[key] = val.merge(@material[key]) 
    buf
  }
end

#observationObject



32
33
34
35
36
37
38
# File 'lib/ishikawa_air_pollution.rb', line 32

def observation
  {
    "start" => @start_observation,
    "end" => @start_observation + Rational(1, 24),
    "period_sec" => 3600
  }
end

#refetch!Object



20
21
22
# File 'lib/ishikawa_air_pollution.rb', line 20

def refetch!
  parse_page(fetch_page)
end