class Kisyo::Daily

Constants

CACHE_SIZE

Attributes

cache[R]
location[R]

Public Class Methods

new(location) click to toggle source
# File lib/kisyo/daily.rb, line 8
def initialize(location)
  @location = location
  @cache = Cache.new
end

Public Instance Methods

at(date) click to toggle source
# 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=%i&block_no=%i&year=%i&month=%i&day=01&view=p1' % [
    location.prefecture_id,
    location.block_id,
    date.year,
    date.month
  ]

  content = 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