Ruby
api
RSS

ETag等の比較で更新をチェックしてリソースを取得したい(injectで書く)

More than 1 year has passed since last update.

ETag等の比較で更新をチェックしてリソースを取得したい』をinjectメソッドで書き直した。依然としてモヤモヤは晴れないけども。

require "logger"
require "open-uri"

$logger = Logger.new($stderr)
$logger.level = Logger::DEBUG

class Crawler
  def initialize(interval)
    @interval = interval
  end

  def start
    loop.inject("") do |etag|
      connection = Connection.new(@interval, etag)
      yield response = connection.start
      sleep @interval
      response.meta["etag"]
    end
    self
  end
end

class Connection
  ENDPOINT = URI("https://example.com/index.rss")
  OPTION = {
    read_timeout: 5,
  }

  def initialize(interval, etag = "")
    @interval = interval
    @etag = etag
  end

  def start
    open(ENDPOINT, OPTION.merge("If-None-Match" => @etag))
  rescue OpenURI::HTTPError
    sleep @interval
    retry
  rescue => e
    $logger.warn("%s: %s" % [e.class, e.message])
    sleep @interval
    retry
  end
end

c = Crawler.new(3)

c.start do |response|
  $logger.info(response.meta["etag"])
end