Module: LucidShopify::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
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
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_params ⇒ Hash
This method is abstract.
Use LucidShopify::Resource::Read::ClassMethods#default_params to implement (optional)
47 48 49 |
# File 'lib/lucid_shopify/resource/read.rb', line 47 def default_params {} end |
#default_shopify_params ⇒ Hash
Defaults set by Shopify when not specified.
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.
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))[resource] results.each do |result| yield result end break if results.empty? since_id = results.last['id'] end end |
#find(credentials, id, params = {}) ⇒ 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)[resource_singular] end |