Module: Lucid::Shopify::Resource::Read
- Includes:
- Enumerable
- Defined in:
- lib/lucid/shopify/resource/read.rb
Overview
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #count(credentials, params = {}) ⇒ Integer
- #default_params ⇒ Hash abstract
-
#default_shopify_params ⇒ Hash
Defaults set by Shopify when not specified.
-
#each(credentials, params = {}) {|Hash| ... } ⇒ Enumerator
Iterate over results.
- #find(credentials, id, params = {}) ⇒ Hash
Class Method Details
.included(base) ⇒ Object
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
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_params ⇒ Hash
This method is abstract.
Use Lucid::Shopify::Resource::Read::ClassMethods#default_params to implement (optional)
43 44 45 |
# File 'lib/lucid/shopify/resource/read.rb', line 43 def default_params {} end |
#default_shopify_params ⇒ Hash
Defaults set by Shopify when not specified.
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.
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
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 |