Module: LucidShopify::Resource::Read

Includes:
Enumerable
Defined in:
lib/lucid_shopify/resource/read.rb

Overview

Examples:

class OrderRepository
  include LucidShopify::Resource::Read

  resource :orders

  default_params fields: %w[id tags]

  # ...
end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



37
38
39
40
# File 'lib/lucid_shopify/resource/read.rb', line 37

def self.included(base)
  base.extend(ClassMethods)
  base.include(Base)
end

Instance Method Details

#count(credentials, params = {}) ⇒ Integer

Parameters:

  • credentials (Credentials)
  • params (Hash) (defaults to: {})

Returns:

  • (Integer)


132
133
134
135
136
137
138
# File 'lib/lucid_shopify/resource/read.rb', line 132

def count(credentials, params = {})
  params = finalise_params(params)

  logger.info("Fetching #{resource} count")

  client.get(credentials, "#{resource}/count", params)['count']
end

#default_paramsHash

This method is abstract.

Returns:

  • (Hash)


47
48
49
# File 'lib/lucid_shopify/resource/read.rb', line 47

def default_params
  {}
end

#default_shopify_paramsHash

Defaults set by Shopify when not specified.

Returns:

  • (Hash)


56
57
58
59
60
# File 'lib/lucid_shopify/resource/read.rb', line 56

def default_shopify_params
  {
    limit: 50,
  }
end

#each(credentials, params = {}) {|Hash| ... } ⇒ Enumerator

Iterate over results. If set, the ‘fields’ option must include ‘id’. We would not need this if we used offset pagination, but offset pagination is unreliable.

Throttling is always enabled.

Parameters:

  • credentials (Credentials)
  • params (Hash) (defaults to: {})

Yields:

  • (Hash)

Returns:

  • (Enumerator)

Raises:

  • (ArgumentError)

    if ‘fields’ does not include ‘id’



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/lucid_shopify/resource/read.rb', line 93

def each(credentials, params = {})
  return to_enum(__method__, credentials, params) unless block_given?

  assert_fields_id!(params = finalise_params(params))

  throttled_client = client.throttled

  since_id = params.delete('since_id') || 1

  loop do
    logger.info("Fetching #{resource} since_id=#{since_id}")

    results = throttled_client.get(credentials, resource, params.merge(since_id: since_id)).values.first
    results.each do |result|
      yield result
    end

    break if results.empty?

    since_id = results.last['id']
  end
end

#find(credentials, id, params = {}) ⇒ Hash

Parameters:

  • credentials (Credentials)
  • id (Integer)
  • params (Hash) (defaults to: {})

Returns:

  • (Hash)


69
70
71
72
73
74
75
# File 'lib/lucid_shopify/resource/read.rb', line 69

def find(credentials, id, params = {})
  params = finalise_params(params)

  logger.info("Fetching #{resource_singular} id=#{id}")

  client.get(credentials, "#{resource}/#{id}", params).values.first
end