Class: Github::CachingClient

Inherits:
Object
  • Object
show all
Includes:
Checks
Defined in:
app/github/caching_client.rb

Overview

Like a Github::Client but with a cache.

Instance Method Summary collapse

Methods included from Checks

#check_boolean, #check_enumerable_of, #check_is_a, #check_non_empty_string, #check_non_nil, #check_positive_integer

Constructor Details

#initialize(client, cache) ⇒ CachingClient

Returns a new instance of CachingClient.



13
14
15
16
# File 'app/github/caching_client.rb', line 13

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

Instance Method Details

#get_with_caching(url, cache_for:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/github/caching_client.rb', line 18

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

#get_without_caching(url) ⇒ Object



31
32
33
34
# File 'app/github/caching_client.rb', line 31

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

#post(url, request) ⇒ Object



36
37
38
# File 'app/github/caching_client.rb', line 36

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