Class: LucidShopify::Resource::Read Abstract

Inherits:
Object
  • Object
show all
Includes:
Enumerable, LucidShopify
Defined in:
lib/lucid_shopify/resource/read.rb

Overview

This class is abstract.

Examples:

class Orders < LucidShopify::Resource::Read
  resource :orders

  default_params fields: %w(id tags)
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: Client.new(send_request: SendThrottledRequest.new)) ⇒ Read

Returns a new instance of Read.

Parameters:

  • client (LucidShopify::Client) (defaults to: Client.new(send_request: SendThrottledRequest.new))


22
23
24
# File 'lib/lucid_shopify/resource/read.rb', line 22

def initialize(client: Client.new(send_request: SendThrottledRequest.new))
  @client = client
end

Instance Attribute Details

#clientLucidShopify::Client (readonly)

Returns:

  • (LucidShopify::Client)


27
28
29
# File 'lib/lucid_shopify/resource/read.rb', line 27

def client
  @client
end

Class Method Details

.default_params(params) ⇒ Object

Set the default query params. Note that ‘fields’ may be passed as an array of strings.

Examples:

default_params fields: %w(id tags)

Parameters:

  • params (Hash)


59
60
61
# File 'lib/lucid_shopify/resource/read.rb', line 59

def self.default_params(params)
  define_method(:default_params) { params }
end

.resource(resource_name) ⇒ Object

Set the remote API resource name for the subclass.

Examples:

resource :orders

Parameters:

  • resource_name (String, #to_s)


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

def self.resource(resource_name)
  define_method(:resource) { resource_name.to_s }
end

Instance Method Details

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

Parameters:

  • client (LucidShopify::AuthorizedClient)
  • params (Hash) (defaults to: {})

Returns:

  • (Integer)


147
148
149
150
151
# File 'lib/lucid_shopify/resource/read.rb', line 147

def count(client, params = {})
  params = finalize_params(params)

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

#default_paramsHash

This method is abstract.

Returns:

  • (Hash)


68
69
70
# File 'lib/lucid_shopify/resource/read.rb', line 68

def default_params
  {}
end

#default_shopify_paramsHash

Defaults set by Shopify when not specified.

Returns:

  • (Hash)


77
78
79
80
81
# File 'lib/lucid_shopify/resource/read.rb', line 77

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 (LucidShopify::RequestCredentials)
  • params (Hash) (defaults to: {})

Yields:

  • (Hash)

Returns:

  • (Enumerator)

Raises:

  • (ArgumentError)

    if ‘fields’ does not include ‘id’



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/lucid_shopify/resource/read.rb', line 112

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

  assert_fields_id!(params = finalized_params(params))

  since_id = 1

  loop do
    results = client.get(credentials, resource, params.merge(since_id: since_id))
    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 (LucidShopify::RequestCredentials)
  • id (Integer)
  • params (Hash) (defaults to: {})

Returns:

  • (Hash)


90
91
92
93
94
# File 'lib/lucid_shopify/resource/read.rb', line 90

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

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

#resourceString

This method is abstract.

Returns:

  • (String)

Raises:

  • (NotImplementedError)


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

def resource
  raise NotImplementedError
end