Sha256: 55efb12cc88e160aa52493cdfd9fd71cc8dcc42588532c4ecc860cf90539bf11

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

require 'nokogiri'
require 'open-uri'

module Kisyo
  class Daily
    CACHE_SIZE = 100

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

    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

    private

    attr_reader :cache, :location
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kisyo-0.1.1 lib/kisyo/daily.rb
kisyo-0.1.0 lib/kisyo/daily.rb