Class: ShopifyClient::CachedRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/shopify-client/cached_request.rb

Overview

Caching for GET requests.

Examples:

get_shop = CachedRequest.new('shop', fields: 'domain,plan')
get_shop.(client) # not cached, makes API request
get_shop.(client) # cached

Instance Method Summary collapse

Constructor Details

#initialize(path, params: {}, store: default_store) ⇒ CachedRequest

Returns a new instance of CachedRequest.

Parameters:

  • path (String)
  • params (Hash) (defaults to: {})
  • store (Cache::Store) (defaults to: default_store)


16
17
18
19
20
# File 'lib/shopify-client/cached_request.rb', line 16

def initialize(path, params: {}, store: default_store)
  @path = path
  @params = params
  @store = store
end

Instance Method Details

#call(client) ⇒ Hash

Returns response data.

Parameters:

Returns:

  • (Hash)

    response data



34
35
36
37
38
# File 'lib/shopify-client/cached_request.rb', line 34

def call(client)
  @store.(build_key(client.myshopify_domain)) do
    client.get(@path, @params).data
  end
end

#clear(myshopify_domain) ⇒ Object

Clear the cached data for a given shop.

Parameters:

  • myshopify_domain (String)


65
66
67
# File 'lib/shopify-client/cached_request.rb', line 65

def clear(myshopify_domain)
  @store.clear(build_key(myshopify_domain))
end

#default_storeCache::Store

Returns:



23
24
25
26
27
28
29
# File 'lib/shopify-client/cached_request.rb', line 23

def default_store
  if defined?(Redis)
    Cache::RedisStore.new
  else
    Cache::ThreadLocalStore.new
  end
end

#set(myshopify_domain, data) ⇒ Object

Overwrite cached data for a given shop. This might be used when the data is received from a webhook.

Parameters:

  • myshopify_domain (String)
  • data (Hash)


58
59
60
# File 'lib/shopify-client/cached_request.rb', line 58

def set(myshopify_domain, data)
  @store.set(build_key(myshopify_domain), data)
end