Sha256: 89bfe1678d425dc3e707e504a427cc28ebe0f1e8cc60528f88d898fb7b3c1d36

Contents?: true

Size: 844 Bytes

Versions: 3

Compression:

Stored size: 844 Bytes

Contents

require 'digest'
require 'fspath'
require 'net/http'
require 'uri'

module TravisCheckRubies
  class Fetcher
    CACHE_TIME = 24 * 60 * 60

    attr_reader :url

    def initialize(url)
      @url = url
    end

    def data
      cached_data || fetch_data
    end

  private

    def cache_path
      @cache_path ||= FSPath(ENV['XDG_CACHE_HOME'] || '~/.cache').expand_path / "travis_check_rubies.#{Digest::SHA1.hexdigest url}"
    end

    def cached_data
      return unless cache_path.size?
      return unless cache_path.mtime + CACHE_TIME > Time.now
      cache_path.read
    end

    def fetch_data
      data = Net::HTTP.get(URI(url))

      cache_path.dirname.mkpath
      FSPath.temp_file('travis_check_rubies', cache_path.dirname) do |f|
        f.write(data)
        f.path.rename(cache_path)
      end

      data
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
travis_check_rubies-0.6.0 lib/travis_check_rubies/fetcher.rb
travis_check_rubies-0.5.1 lib/travis_check_rubies/fetcher.rb
travis_check_rubies-0.5.0 lib/travis_check_rubies/fetcher.rb