Module: Lucid::Shopify::Resource::Read

Includes:
Enumerable
Defined in:
lib/lucid/shopify/resource/read.rb

Overview

Examples:

class OrderRepository
  include Lucid::Shopify::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

Parameters:

  • base (Class, Module)


35
36
37
38
# File 'lib/lucid/shopify/resource/read.rb', line 35

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)


116
117
118
119
120
121
122
# File 'lib/lucid/shopify/resource/read.rb', line 116

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)


43
44
45
# File 'lib/lucid/shopify/resource/read.rb', line 43

def default_params
  {}
end

#default_shopify_paramsHash

Defaults set by Shopify when not specified.

Returns:

  • (Hash)


50
51
52
53
54
# File 'lib/lucid/shopify/resource/read.rb', line 50

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’



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/lucid/shopify/resource/read.rb', line 83

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

  assert_fields_id!(params = finalise_params(params))

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

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

    results = client.get(credentials, resource, params.merge(since_id: since_id))[resource]
    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)


61
62
63
64
65
66
67
# File 'lib/lucid/shopify/resource/read.rb', line 61

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

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

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