Class: ShopifyAPI::Rest::Base
- Inherits:
-
Object
- Object
- ShopifyAPI::Rest::Base
show all
- Extended by:
- T::Helpers, T::Sig
- Defined in:
- lib/shopify_api/rest/base.rb
Direct Known Subclasses
AbandonedCheckout, AccessScope, AndroidPayKey, ApplePayCertificate, ApplicationCharge, ApplicationCredit, Article, Asset, AssignedFulfillmentOrder, Balance, Blog, CancellationRequest, CarrierService, Checkout, Collect, Collection, CollectionListing, Comment, Country, Currency, CustomCollection, Customer, CustomerAddress, CustomerSavedSearch, DeprecatedApiCall, DiscountCode, Dispute, DisputeEvidence, DisputeFileUpload, DraftOrder, Event, Fulfillment, FulfillmentEvent, FulfillmentOrder, FulfillmentRequest, FulfillmentService, GiftCard, GiftCardAdjustment, Image, InventoryItem, InventoryLevel, Location, LocationsForMove, MarketingEvent, Metafield, MobilePlatformApplication, Order, OrderRisk, Page, Payment, PaymentGateway, PaymentTransaction, Payout, Policy, PriceRule, Product, ProductListing, ProductResourceFeedback, Province, ShopifyAPI::RecurringApplicationCharge, ShopifyAPI::Redirect, ShopifyAPI::Refund, ShopifyAPI::Report, ShopifyAPI::ResourceFeedback, ScriptTag, ShippingZone, Shop, SmartCollection, StorefrontAccessToken, TenderTransaction, Theme, Transaction, UsageCharge, User, Variant, Webhook
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.api_call_limit ⇒ Object
-
.base_find(session: nil, ids: {}, params: {}) ⇒ Object
-
.class_name ⇒ Object
-
.create_instance(data:, session:, instance: nil) ⇒ Object
-
.create_instances_from_response(response:, session:) ⇒ Object
-
.get_path(http_method:, operation:, entity: nil, ids: {}) ⇒ Object
-
.get_path_ids(http_method:, operation:) ⇒ Object
-
.has_many?(attribute) ⇒ Boolean
-
.has_one?(attribute) ⇒ Boolean
-
.inherited(subclass) ⇒ Object
-
.json_body_name ⇒ Object
-
.json_response_body_names ⇒ Object
-
.next_page? ⇒ Boolean
-
.next_page_info ⇒ Object
-
.prev_page? ⇒ Boolean
-
.prev_page_info ⇒ Object
-
.primary_key ⇒ Object
-
.read_only_attributes ⇒ Object
-
.request(http_method:, operation:, session:, ids: {}, params: {}, body: nil, entity: nil) ⇒ Object
-
.retry_request_after ⇒ Object
Instance Method Summary
collapse
Constructor Details
#initialize(session: nil, from_hash: nil) ⇒ Base
Returns a new instance of Base.
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/shopify_api/rest/base.rb', line 35
def initialize(session: nil, from_hash: nil)
@original_state = T.let({}, T::Hash[Symbol, T.untyped])
@custom_prefix = T.let(nil, T.nilable(String))
@forced_nils = T.let({}, T::Hash[String, T::Boolean])
@aliased_properties = T.let({}, T::Hash[String, String])
session ||= ShopifyAPI::Context.active_session
client = ShopifyAPI::Clients::Rest::Admin.new(session: session)
@session = T.let(T.must(session), Auth::Session)
@client = T.let(client, Clients::Rest::Admin)
@errors = T.let(Rest::BaseErrors.new, Rest::BaseErrors)
from_hash&.each do |key, value|
set_property(key, value)
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth_id, val = nil) ⇒ Object
309
310
311
312
313
314
315
316
317
318
319
320
|
# File 'lib/shopify_api/rest/base.rb', line 309
def method_missing(meth_id, val = nil)
match = meth_id.id2name.match(/([^=]+)(=)?/)
var = T.must(T.must(match)[1])
if T.must(match)[2]
set_property(var, val)
@forced_nils[var] = val.nil?
else
get_property(var)
end
end
|
Class Attribute Details
.atomic_hash_attributes ⇒ Object
Returns the value of attribute atomic_hash_attributes.
67
68
69
|
# File 'lib/shopify_api/rest/base.rb', line 67
def atomic_hash_attributes
@atomic_hash_attributes
end
|
.custom_prefix ⇒ Object
Returns the value of attribute custom_prefix.
58
59
60
|
# File 'lib/shopify_api/rest/base.rb', line 58
def custom_prefix
@custom_prefix
end
|
.has_many ⇒ Object
Returns the value of attribute has_many.
61
62
63
|
# File 'lib/shopify_api/rest/base.rb', line 61
def has_many
@has_many
end
|
.has_one ⇒ Object
Returns the value of attribute has_one.
64
65
66
|
# File 'lib/shopify_api/rest/base.rb', line 64
def has_one
@has_one
end
|
Returns the value of attribute headers.
70
71
72
|
# File 'lib/shopify_api/rest/base.rb', line 70
def
@headers
end
|
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
27
28
29
|
# File 'lib/shopify_api/rest/base.rb', line 27
def errors
@errors
end
|
#original_state ⇒ Object
Returns the value of attribute original_state.
24
25
26
|
# File 'lib/shopify_api/rest/base.rb', line 24
def original_state
@original_state
end
|
Class Method Details
.api_call_limit ⇒ Object
151
152
153
|
# File 'lib/shopify_api/rest/base.rb', line 151
def api_call_limit
instance_variable_get(:"@api_call_limit").value
end
|
.base_find(session: nil, ids: {}, params: {}) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/shopify_api/rest/base.rb', line 88
def base_find(session: nil, ids: {}, params: {})
session ||= ShopifyAPI::Context.active_session
client = ShopifyAPI::Clients::Rest::Admin.new(session: session)
path = T.must(get_path(http_method: :get, operation: :get, ids: ids))
response = client.get(path: path, query: params.compact, headers: )
instance_variable_get(:"@prev_page_info").value = response.prev_page_info
instance_variable_get(:"@next_page_info").value = response.next_page_info
instance_variable_get(:"@retry_request_after").value = response.retry_request_after
instance_variable_get(:"@api_call_limit").value = response.api_call_limit
create_instances_from_response(response: response, session: T.must(session))
end
|
.class_name ⇒ Object
106
107
108
|
# File 'lib/shopify_api/rest/base.rb', line 106
def class_name
T.must(name).demodulize.underscore
end
|
.create_instance(data:, session:, instance: nil) ⇒ Object
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
# File 'lib/shopify_api/rest/base.rb', line 276
def create_instance(data:, session:, instance: nil)
instance ||= new(session: session)
instance.original_state = {}
data.each do |attribute, value|
attr_sym = attribute.to_sym
if has_many?(attr_sym) && value
instance.original_state[attr_sym] = []
attr_list = []
value.each do |element|
child = T.unsafe(@has_many[attr_sym]).create_instance(data: element, session: session)
attr_list << child
instance.original_state[attr_sym] << child.to_hash(true)
end
instance.public_send("#{attribute}=", attr_list)
elsif has_one?(attr_sym) && value
data_hash = value.is_a?(Hash) ? value : { attribute.to_s => value }
child = T.unsafe(@has_one[attr_sym]).create_instance(data: data_hash, session: session)
instance.public_send("#{attribute}=", child)
instance.original_state[attr_sym] = child.to_hash(true)
else
instance.public_send("#{attribute}=", value)
instance.original_state[attr_sym] = value
end
end
instance
end
|
.create_instances_from_response(response:, session:) ⇒ Object
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
# File 'lib/shopify_api/rest/base.rb', line 253
def create_instances_from_response(response:, session:)
objects = []
body = T.cast(response.body, T::Hash[String, T.untyped]).with_indifferent_access
response_names = json_response_body_names
response_names.each do |response_name|
if body.key?(response_name.pluralize) || (body.key?(response_name) && body[response_name].is_a?(Array))
(body[response_name.pluralize] || body[response_name]).each do |entry|
objects << create_instance(data: entry, session: session)
end
elsif body.key?(response_name)
objects << create_instance(data: body[response_name], session: session)
end
end
objects
end
|
.get_path(http_method:, operation:, entity: nil, ids: {}) ⇒ Object
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
# File 'lib/shopify_api/rest/base.rb', line 178
def get_path(http_method:, operation:, entity: nil, ids: {})
match = T.let(nil, T.nilable(String))
max_ids = T.let(-1, Integer)
@paths.each do |path|
next if http_method != path[:http_method] || operation != path[:operation]
path_ids = T.cast(path[:ids], T::Array[Symbol])
url_ids = ids.transform_keys(&:to_sym)
path_ids.each do |id|
if url_ids[id].nil? && (entity_id = entity&.public_send(id))
url_ids[id] = entity_id
end
end
url_ids.compact!
next if !(path_ids - url_ids.keys).empty? || path_ids.length <= max_ids
max_ids = path_ids.length
match = T.cast(path[:path], String).gsub(/(<([^>]+)>)/) do
url_ids[T.unsafe(Regexp.last_match)[2].to_sym]
end
end
custom_prefix ? "#{T.must(custom_prefix).sub(%r{\A/}, "")}/#{match}" : match
end
|
.get_path_ids(http_method:, operation:) ⇒ Object
213
214
215
216
217
218
219
220
|
# File 'lib/shopify_api/rest/base.rb', line 213
def get_path_ids(http_method:, operation:)
found_path = @paths.find do |path|
http_method == path[:http_method] && operation == path[:operation]
end
return nil if found_path.nil?
T.cast(found_path[:ids], T::Array[Symbol])
end
|
.has_many?(attribute) ⇒ Boolean
156
157
158
|
# File 'lib/shopify_api/rest/base.rb', line 156
def has_many?(attribute)
@has_many.include?(attribute)
end
|
.has_one?(attribute) ⇒ Boolean
161
162
163
|
# File 'lib/shopify_api/rest/base.rb', line 161
def has_one?(attribute)
@has_one.include?(attribute)
end
|
.inherited(subclass) ⇒ Object
73
74
75
76
77
78
79
|
# File 'lib/shopify_api/rest/base.rb', line 73
def inherited(subclass)
super
subclass.define_singleton_method(:headers) do
ShopifyAPI::Rest::Base.
end
end
|
.json_body_name ⇒ Object
116
117
118
|
# File 'lib/shopify_api/rest/base.rb', line 116
def json_body_name
class_name.underscore
end
|
.json_response_body_names ⇒ Object
121
122
123
|
# File 'lib/shopify_api/rest/base.rb', line 121
def json_response_body_names
[class_name]
end
|
.next_page? ⇒ Boolean
141
142
143
|
# File 'lib/shopify_api/rest/base.rb', line 141
def next_page?
!instance_variable_get(:"@next_page_info").value.nil?
end
|
.next_page_info ⇒ Object
131
132
133
|
# File 'lib/shopify_api/rest/base.rb', line 131
def next_page_info
instance_variable_get(:"@next_page_info").value
end
|
.prev_page? ⇒ Boolean
136
137
138
|
# File 'lib/shopify_api/rest/base.rb', line 136
def prev_page?
!instance_variable_get(:"@prev_page_info").value.nil?
end
|
.prev_page_info ⇒ Object
126
127
128
|
# File 'lib/shopify_api/rest/base.rb', line 126
def prev_page_info
instance_variable_get(:"@prev_page_info").value
end
|
.primary_key ⇒ Object
111
112
113
|
# File 'lib/shopify_api/rest/base.rb', line 111
def primary_key
"id"
end
|
.read_only_attributes ⇒ Object
166
167
168
|
# File 'lib/shopify_api/rest/base.rb', line 166
def read_only_attributes
@read_only_attributes&.map { |a| :"@#{a}" }
end
|
.request(http_method:, operation:, session:, ids: {}, params: {}, body: nil, entity: nil) ⇒ Object
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
# File 'lib/shopify_api/rest/base.rb', line 233
def request(http_method:, operation:, session:, ids: {}, params: {}, body: nil, entity: nil)
client = ShopifyAPI::Clients::Rest::Admin.new(session: session)
path = get_path(http_method: http_method, operation: operation.to_sym, ids: ids)
case http_method
when :get
client.get(path: T.must(path), query: params.compact, headers: )
when :post
client.post(path: T.must(path), query: params.compact, body: body || {}, headers: )
when :put
client.put(path: T.must(path), query: params.compact, body: body || {}, headers: )
when :delete
client.delete(path: T.must(path), query: params.compact, headers: )
else
raise Errors::InvalidHttpRequestError, "Invalid HTTP method: #{http_method}"
end
end
|
.retry_request_after ⇒ Object
146
147
148
|
# File 'lib/shopify_api/rest/base.rb', line 146
def retry_request_after
instance_variable_get(:"@retry_request_after").value
end
|
Instance Method Details
#delete(params: {}) ⇒ Object
372
373
374
375
376
377
378
379
380
381
|
# File 'lib/shopify_api/rest/base.rb', line 372
def delete(params: {})
@client.delete(
path: T.must(self.class.get_path(http_method: :delete, operation: :delete, entity: self)),
query: params.compact,
headers: self.class.,
)
rescue ShopifyAPI::Errors::HttpResponseError => e
@errors.errors << e
raise
end
|
#respond_to_missing?(meth_id, *args) ⇒ Boolean
323
324
325
326
327
328
|
# File 'lib/shopify_api/rest/base.rb', line 323
def respond_to_missing?(meth_id, *args)
str = meth_id.id2name
match = str.match(/([^=]+)=/)
match.nil? ? true : super
end
|
#save(update_object: false) ⇒ Object
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
# File 'lib/shopify_api/rest/base.rb', line 389
def save(update_object: false)
method = deduce_write_verb
body = {
self.class.json_body_name => attributes_to_update.merge(build_required_attributes(http_method: method)),
}
response = @client.public_send(
method,
body: body,
path: deduce_write_path(method),
headers: self.class.,
)
if update_object
response_name = self.class.json_response_body_names & response.body.keys
self.class.create_instance(data: response.body[response_name.first], session: @session, instance: self)
end
rescue ShopifyAPI::Errors::HttpResponseError => e
@errors.errors << e
raise
end
|
#save! ⇒ Object
384
385
386
|
# File 'lib/shopify_api/rest/base.rb', line 384
def save!
save(update_object: true)
end
|
#to_hash(saving = false) ⇒ Object
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
# File 'lib/shopify_api/rest/base.rb', line 331
def to_hash(saving = false)
hash = {}
instance_variables.each do |var|
next if [
:"@original_state",
:"@session",
:"@client",
:"@forced_nils",
:"@errors",
:"@aliased_properties",
].include?(var)
next if saving && self.class.read_only_attributes&.include?(var)
var = var.to_s.delete("@")
attribute = if @aliased_properties.value?(var)
T.must(@aliased_properties.key(var))
else
var
end.to_sym
if self.class.has_many?(attribute)
attribute_class = self.class.has_many[attribute]
hash[attribute.to_s] = get_property(attribute).map do |element|
get_element_hash(element, T.unsafe(attribute_class), saving)
end.to_a if get_property(attribute)
elsif self.class.has_one?(attribute)
element_hash = get_element_hash(
get_property(attribute),
T.unsafe(self.class.has_one[attribute]),
saving,
)
hash[attribute.to_s] = element_hash if element_hash || @forced_nils[attribute.to_s]
elsif !get_property(attribute).nil? || @forced_nils[attribute.to_s]
hash[attribute.to_s] =
get_property(attribute)
end
end
hash
end
|