Class: Kisyo::Daily

Inherits:
Object
  • Object
show all
Defined in:
lib/kisyo/daily.rb

Constant Summary collapse

CACHE_SIZE =
100

Instance Method Summary collapse

Constructor Details

#initialize(location) ⇒ Daily

Returns a new instance of Daily.



8
9
10
11
# File 'lib/kisyo/daily.rb', line 8

def initialize(location)
  @location = location
  @cache = Cache.new
end

Instance Method Details

#at(date) ⇒ Object



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
# File 'lib/kisyo/daily.rb', line 13

def at(date)
  key = [date.year, date.month, date.day].join(',')

  if value = cache.get(key)
    return value
  end

  url =
    'http://www.data.jma.go.jp/obd/stats/etrn/view/daily_s1.php?prec_no=%s&block_no=%s&year=%i&month=%i&day=01&view=p1' % [
      location.prefecture_id,
      location.block_id,
      date.year,
      date.month
    ]

  content = URI.open(url).read
  doc = Nokogiri.HTML(content)
  days = doc.css('div.a_print')

  raise WeatherInformationNotAvailable if days.size == 0

  days.each do |el|
    tr = el.parent.parent
    values = tr.css('td').map(&:text)

    k = [date.year, date.month, values[0]].join(',')
    cache.set(k, Element::Day.new(*values[1..-1]))
  end

  cache.get(key)
end