Sha256: 10e061416d0d0dc7e197f88fec41dfe881e3d76ecb4a609d5a293e0c6b52dde3

Contents?: true

Size: 541 Bytes

Versions: 1

Compression:

Stored size: 541 Bytes

Contents

require 'open-uri'
require 'json'
require 'observer'

module HatebuWatcher
  class Counter
    include Observable

    ENDPOINT = 'http://b.hatena.ne.jp/entry/jsonlite/?url='

    attr_reader :count

    def initialize(url)
      @url = url
    end

    def reload
      @prev_count = @count
      @count      = JSON.parse(json.read)['count'].to_i

      unless @count == @prev_count
        changed
        notify_observers(@count)
      end

      self
    end

    private

    def json
      open("#{ENDPOINT}#{@url}")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hatebu_watcher-0.0.1 lib/hatebu_watcher/counter.rb