Sha256: 6f9ee14be0547e3a04c63f1d22ea0b98baf7aff5d489c070435f7a60831077f8

Contents?: true

Size: 914 Bytes

Versions: 13

Compression:

Stored size: 914 Bytes

Contents

# frozen_string_literal: true

require_relative '../checks'
require_relative '../deep'
require_relative 'cache'
require_relative 'client'

module Github
  # Like a Github::Client but with a cache.
  class CachingClient
    include Checks

    def initialize(client, cache)
      @client = check_is_a(client: [Client, client])
      @cache = check_is_a(cache: [Cache, cache])
    end

    def get_with_caching(url, cache_for:)
      check_non_empty_string(url: url)
      check_positive_integer(cache_for: cache_for)

      if (cache_entry = @cache[url])
        return cache_entry.value.deep_dup
      end

      response = @client.get(url)
      @cache[url] = Cache::Entry.new(response, cache_for)
      response.deep_dup
    end

    def get_without_caching(url)
      check_non_empty_string(url: url)
      @client.get(url)
    end

    def post(url, request)
      @client.post(url, request)
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
sqlui-0.1.84 app/github/caching_client.rb
sqlui-0.1.83 app/github/caching_client.rb
sqlui-0.1.82 app/github/caching_client.rb
sqlui-0.1.81 app/github/caching_client.rb
sqlui-0.1.80 app/github/caching_client.rb
sqlui-0.1.79 app/github/caching_client.rb
sqlui-0.1.78 app/github/caching_client.rb
sqlui-0.1.77 app/github/caching_client.rb
sqlui-0.1.76 app/github/caching_client.rb
sqlui-0.1.75 app/github/caching_client.rb
sqlui-0.1.74 app/github/caching_client.rb
sqlui-0.1.73 app/github/caching_client.rb
sqlui-0.1.72 app/github/caching_client.rb