shopify_api 4.9.0 → 9.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (281) hide show
  1. checksums.yaml +5 -5
  2. data/.github/CODEOWNERS +1 -0
  3. data/.github/ISSUE_TEMPLATE.md +36 -0
  4. data/.github/probots.yml +2 -0
  5. data/.github/workflows/build.yml +41 -0
  6. data/.gitignore +5 -1
  7. data/.rubocop.yml +28 -0
  8. data/.rubocop_todo.yml +75 -0
  9. data/CHANGELOG.md +491 -0
  10. data/CONTRIBUTING.md +1 -1
  11. data/Gemfile +6 -2
  12. data/Gemfile.lock +151 -0
  13. data/Gemfile_ar41 +5 -0
  14. data/Gemfile_ar50 +5 -0
  15. data/Gemfile_ar51 +5 -0
  16. data/Gemfile_ar_master +0 -1
  17. data/README.md +492 -100
  18. data/RELEASING +10 -9
  19. data/Rakefile +21 -5
  20. data/SECURITY.md +59 -0
  21. data/docker-compose.yml +13 -0
  22. data/docs/_config.yml +1 -0
  23. data/docs/_includes/footer.html +28 -0
  24. data/docs/_includes/head.html +28 -0
  25. data/docs/_layouts/index.html +57 -0
  26. data/docs/graphql.md +241 -0
  27. data/docs/index.md +639 -0
  28. data/lib/active_resource/connection_ext.rb +1 -0
  29. data/lib/active_resource/detailed_log_subscriber.rb +43 -7
  30. data/lib/active_resource/json_errors.rb +8 -2
  31. data/lib/shopify_api.rb +16 -6
  32. data/lib/shopify_api/api_access.rb +57 -0
  33. data/lib/shopify_api/api_version.rb +206 -0
  34. data/lib/shopify_api/connection.rb +7 -4
  35. data/lib/shopify_api/countable.rb +3 -2
  36. data/lib/shopify_api/disable_prefix_check.rb +31 -0
  37. data/lib/shopify_api/events.rb +2 -1
  38. data/lib/shopify_api/graphql.rb +103 -0
  39. data/lib/shopify_api/graphql/http_client.rb +22 -0
  40. data/lib/shopify_api/graphql/railtie.rb +17 -0
  41. data/lib/shopify_api/graphql/task.rake +100 -0
  42. data/lib/shopify_api/limits.rb +9 -9
  43. data/lib/shopify_api/message_enricher.rb +25 -0
  44. data/lib/shopify_api/meta.rb +14 -0
  45. data/lib/shopify_api/metafields.rb +5 -4
  46. data/lib/shopify_api/paginated_collection.rb +69 -0
  47. data/lib/shopify_api/pagination_link_headers.rb +34 -0
  48. data/lib/shopify_api/resources.rb +3 -1
  49. data/lib/shopify_api/resources/abandoned_checkout.rb +7 -0
  50. data/lib/shopify_api/resources/access_scope.rb +10 -0
  51. data/lib/shopify_api/resources/access_token.rb +1 -0
  52. data/lib/shopify_api/resources/address.rb +1 -0
  53. data/lib/shopify_api/resources/announcement.rb +2 -1
  54. data/lib/shopify_api/resources/api_permission.rb +9 -0
  55. data/lib/shopify_api/resources/application_charge.rb +1 -0
  56. data/lib/shopify_api/resources/application_credit.rb +1 -0
  57. data/lib/shopify_api/resources/array_base.rb +13 -0
  58. data/lib/shopify_api/resources/article.rb +3 -2
  59. data/lib/shopify_api/resources/asset.rb +28 -23
  60. data/lib/shopify_api/resources/assigned_fulfillment_order.rb +16 -0
  61. data/lib/shopify_api/resources/base.rb +101 -24
  62. data/lib/shopify_api/resources/billing_address.rb +2 -1
  63. data/lib/shopify_api/resources/blog.rb +2 -1
  64. data/lib/shopify_api/resources/carrier_service.rb +1 -0
  65. data/lib/shopify_api/resources/cart.rb +2 -1
  66. data/lib/shopify_api/resources/checkout.rb +27 -1
  67. data/lib/shopify_api/resources/collect.rb +2 -0
  68. data/lib/shopify_api/resources/collection.rb +14 -0
  69. data/lib/shopify_api/resources/collection_listing.rb +11 -1
  70. data/lib/shopify_api/resources/collection_publication.rb +10 -0
  71. data/lib/shopify_api/resources/comment.rb +20 -5
  72. data/lib/shopify_api/resources/country.rb +1 -0
  73. data/lib/shopify_api/resources/currency.rb +6 -0
  74. data/lib/shopify_api/resources/custom_collection.rb +7 -6
  75. data/lib/shopify_api/resources/customer.rb +2 -1
  76. data/lib/shopify_api/resources/customer_group.rb +1 -0
  77. data/lib/shopify_api/resources/{customer_invite_message.rb → customer_invite.rb} +1 -0
  78. data/lib/shopify_api/resources/customer_saved_search.rb +4 -1
  79. data/lib/shopify_api/resources/discount_code.rb +1 -0
  80. data/lib/shopify_api/resources/discount_code_batch.rb +34 -0
  81. data/lib/shopify_api/resources/draft_order.rb +1 -0
  82. data/lib/shopify_api/resources/draft_order_invoice.rb +1 -0
  83. data/lib/shopify_api/resources/event.rb +2 -0
  84. data/lib/shopify_api/resources/fulfillment.rb +46 -3
  85. data/lib/shopify_api/resources/fulfillment_event.rb +2 -1
  86. data/lib/shopify_api/resources/fulfillment_order.rb +151 -0
  87. data/lib/shopify_api/resources/fulfillment_order_locations_for_move.rb +5 -0
  88. data/lib/shopify_api/resources/fulfillment_request.rb +1 -0
  89. data/lib/shopify_api/resources/fulfillment_service.rb +1 -0
  90. data/lib/shopify_api/resources/fulfillment_v2.rb +21 -0
  91. data/lib/shopify_api/resources/gift_card.rb +1 -0
  92. data/lib/shopify_api/resources/image.rb +4 -3
  93. data/lib/shopify_api/resources/inventory_item.rb +6 -0
  94. data/lib/shopify_api/resources/inventory_level.rb +54 -0
  95. data/lib/shopify_api/resources/line_item.rb +10 -1
  96. data/lib/shopify_api/resources/location.rb +4 -0
  97. data/lib/shopify_api/resources/marketing_event.rb +3 -0
  98. data/lib/shopify_api/resources/metafield.rb +2 -0
  99. data/lib/shopify_api/resources/note_attribute.rb +1 -0
  100. data/lib/shopify_api/resources/option.rb +1 -0
  101. data/lib/shopify_api/resources/order.rb +25 -5
  102. data/lib/shopify_api/resources/order_risk.rb +1 -0
  103. data/lib/shopify_api/resources/page.rb +1 -0
  104. data/lib/shopify_api/resources/payment.rb +7 -0
  105. data/lib/shopify_api/resources/payment_details.rb +1 -0
  106. data/lib/shopify_api/resources/ping.rb +3 -0
  107. data/lib/shopify_api/resources/policy.rb +1 -0
  108. data/lib/shopify_api/resources/price_rule.rb +1 -1
  109. data/lib/shopify_api/resources/product.rb +33 -7
  110. data/lib/shopify_api/resources/product_listing.rb +9 -1
  111. data/lib/shopify_api/resources/product_publication.rb +10 -0
  112. data/lib/shopify_api/resources/province.rb +1 -0
  113. data/lib/shopify_api/resources/publication.rb +5 -0
  114. data/lib/shopify_api/resources/receipt.rb +1 -0
  115. data/lib/shopify_api/resources/recurring_application_charge.rb +4 -1
  116. data/lib/shopify_api/resources/redirect.rb +1 -0
  117. data/lib/shopify_api/resources/refund.rb +6 -4
  118. data/lib/shopify_api/resources/report.rb +1 -0
  119. data/lib/shopify_api/resources/resource_feedback.rb +1 -1
  120. data/lib/shopify_api/resources/rule.rb +1 -0
  121. data/lib/shopify_api/resources/script_tag.rb +1 -0
  122. data/lib/shopify_api/resources/shipping_address.rb +1 -0
  123. data/lib/shopify_api/resources/shipping_line.rb +2 -1
  124. data/lib/shopify_api/resources/shipping_rate.rb +7 -0
  125. data/lib/shopify_api/resources/shipping_zone.rb +1 -0
  126. data/lib/shopify_api/resources/shop.rb +10 -7
  127. data/lib/shopify_api/resources/smart_collection.rb +3 -3
  128. data/lib/shopify_api/resources/storefront_access_token.rb +1 -0
  129. data/lib/shopify_api/resources/tax_line.rb +1 -0
  130. data/lib/shopify_api/resources/tax_service.rb +1 -0
  131. data/lib/shopify_api/resources/tender_transaction.rb +6 -0
  132. data/lib/shopify_api/resources/theme.rb +1 -0
  133. data/lib/shopify_api/resources/transaction.rb +1 -0
  134. data/lib/shopify_api/resources/usage_charge.rb +1 -0
  135. data/lib/shopify_api/resources/user.rb +1 -0
  136. data/lib/shopify_api/resources/variant.rb +35 -0
  137. data/lib/shopify_api/resources/webhook.rb +1 -0
  138. data/lib/shopify_api/session.rb +109 -45
  139. data/lib/shopify_api/version.rb +2 -1
  140. data/lib/verify_docs.rb +8 -0
  141. data/service.yml +8 -0
  142. data/shopify_api.gemspec +19 -8
  143. data/test/abandoned_checkouts_test.rb +29 -0
  144. data/test/access_scope_test.rb +23 -0
  145. data/test/access_token_test.rb +6 -5
  146. data/test/active_resource/json_errors_test.rb +6 -6
  147. data/test/api_access_test.rb +153 -0
  148. data/test/api_permission_test.rb +9 -0
  149. data/test/api_version_test.rb +157 -0
  150. data/test/application_charge_test.rb +30 -27
  151. data/test/application_credit_test.rb +10 -9
  152. data/test/article_test.rb +34 -35
  153. data/test/asset_test.rb +14 -6
  154. data/test/assigned_fulfillment_order_test.rb +78 -0
  155. data/test/base_test.rb +147 -59
  156. data/test/blog_test.rb +4 -3
  157. data/test/carrier_service_test.rb +7 -6
  158. data/test/cart_test.rb +2 -1
  159. data/test/checkouts_test.rb +72 -4
  160. data/test/collect_test.rb +4 -3
  161. data/test/collection_listing_test.rb +56 -13
  162. data/test/collection_publication_test.rb +40 -0
  163. data/test/collection_test.rb +50 -0
  164. data/test/countable_test.rb +3 -2
  165. data/test/currency_test.rb +21 -0
  166. data/test/custom_collection_test.rb +4 -3
  167. data/test/customer_saved_search_test.rb +18 -8
  168. data/test/customer_test.rb +22 -14
  169. data/test/detailed_log_subscriber_test.rb +113 -19
  170. data/test/discount_code_batch_test.rb +41 -0
  171. data/test/discount_code_test.rb +22 -16
  172. data/test/draft_order_test.rb +68 -52
  173. data/test/fixtures/abandoned_checkout.json +184 -0
  174. data/test/fixtures/abandoned_checkouts.json +186 -0
  175. data/test/fixtures/access_scopes.json +10 -0
  176. data/test/fixtures/api_versions.json +38 -0
  177. data/test/fixtures/apis.json +42 -0
  178. data/test/fixtures/assigned_fulfillment_orders.json +80 -0
  179. data/test/fixtures/checkout.json +160 -0
  180. data/test/fixtures/checkouts.json +25 -49
  181. data/test/fixtures/collection.json +17 -0
  182. data/test/fixtures/collection_listing_product_ids2.json +1 -0
  183. data/test/fixtures/collection_products.json +47 -0
  184. data/test/fixtures/collection_publication.json +11 -0
  185. data/test/fixtures/collection_publications.json +13 -0
  186. data/test/fixtures/currencies.json +25 -0
  187. data/test/fixtures/discount_code_batch.json +14 -0
  188. data/test/fixtures/discount_code_batch_discount_codes.json +21 -0
  189. data/test/fixtures/fulfillment_order.json +39 -0
  190. data/test/fixtures/fulfillment_order_locations_for_move.json +18 -0
  191. data/test/fixtures/fulfillment_orders.json +80 -0
  192. data/test/fixtures/fulfillments.json +53 -0
  193. data/test/fixtures/graphql/2019-10.json +1083 -0
  194. data/test/fixtures/graphql/dummy_schema.rb +16 -0
  195. data/test/fixtures/graphql/unstable.json +1083 -0
  196. data/test/fixtures/inventory_level.json +7 -0
  197. data/test/fixtures/inventory_levels.json +24 -0
  198. data/test/fixtures/order_with_properties.json +373 -0
  199. data/test/fixtures/payment.json +7 -0
  200. data/test/fixtures/payments.json +9 -0
  201. data/test/fixtures/ping/conversation.json +1 -0
  202. data/test/fixtures/ping/failed_delivery_confirmation.json +1 -0
  203. data/test/fixtures/ping/message.json +1 -0
  204. data/test/fixtures/ping/successful_delivery_confirmation.json +1 -0
  205. data/test/fixtures/product_listing_product_ids.json +1 -1
  206. data/test/fixtures/product_listing_product_ids2.json +1 -0
  207. data/test/fixtures/product_publication.json +11 -0
  208. data/test/fixtures/product_publications.json +13 -0
  209. data/test/fixtures/publications.json +9 -0
  210. data/test/fixtures/shipping_rates.json +12 -0
  211. data/test/fixtures/smart_collection_products.json +155 -0
  212. data/test/fixtures/tender_transactions.json +52 -0
  213. data/test/fulfillment_event_test.rb +31 -26
  214. data/test/fulfillment_order_test.rb +530 -0
  215. data/test/fulfillment_order_test_helper.rb +8 -0
  216. data/test/fulfillment_request_test.rb +10 -8
  217. data/test/fulfillment_service_test.rb +13 -12
  218. data/test/fulfillment_test.rb +198 -20
  219. data/test/fulfillment_v2_test.rb +66 -0
  220. data/test/gift_card_test.rb +6 -4
  221. data/test/graphql/http_client_test.rb +26 -0
  222. data/test/graphql_test.rb +190 -0
  223. data/test/image_test.rb +19 -17
  224. data/test/inventory_level_test.rb +68 -0
  225. data/test/lib/webmock_extensions/last_request.rb +16 -0
  226. data/test/limits_test.rb +4 -3
  227. data/test/location_test.rb +15 -0
  228. data/test/marketing_event_test.rb +21 -21
  229. data/test/message_enricher_test.rb +45 -0
  230. data/test/meta_test.rb +47 -0
  231. data/test/metafield_test.rb +30 -20
  232. data/test/order_risk_test.rb +17 -16
  233. data/test/order_test.rb +110 -17
  234. data/test/pagination_test.rb +290 -0
  235. data/test/payment_test.rb +19 -0
  236. data/test/policy_test.rb +6 -5
  237. data/test/price_rule_test.rb +20 -15
  238. data/test/product_listing_test.rb +72 -15
  239. data/test/product_publication_test.rb +40 -0
  240. data/test/product_test.rb +80 -19
  241. data/test/publication_test.rb +12 -0
  242. data/test/recurring_application_charge_test.rb +105 -50
  243. data/test/redirect_test.rb +4 -3
  244. data/test/refund_test.rb +22 -17
  245. data/test/report_test.rb +12 -10
  246. data/test/resource_feedback_test.rb +14 -13
  247. data/test/script_tag_test.rb +10 -9
  248. data/test/session_test.rb +497 -111
  249. data/test/shipping_rate_test.rb +17 -0
  250. data/test/shipping_zone_test.rb +4 -3
  251. data/test/shop_test.rb +47 -33
  252. data/test/smart_collection_test.rb +5 -4
  253. data/test/storefront_access_token_test.rb +13 -15
  254. data/test/tax_service_test.rb +7 -3
  255. data/test/tender_transaction_test.rb +18 -0
  256. data/test/test_helper.rb +98 -67
  257. data/test/transaction_test.rb +4 -3
  258. data/test/usage_charge_test.rb +12 -8
  259. data/test/user_test.rb +4 -3
  260. data/test/variant_test.rb +50 -20
  261. data/test/webhook_test.rb +10 -7
  262. metadata +196 -37
  263. data/.travis.yml +0 -36
  264. data/CHANGELOG +0 -292
  265. data/Gemfile_ar30 +0 -6
  266. data/Gemfile_ar31 +0 -6
  267. data/Gemfile_ar32 +0 -6
  268. data/Gemfile_ar40 +0 -6
  269. data/bin/shopify +0 -3
  270. data/lib/active_resource/base_ext.rb +0 -21
  271. data/lib/active_resource/disable_prefix_check.rb +0 -36
  272. data/lib/active_resource/to_query.rb +0 -10
  273. data/lib/shopify_api/json_format.rb +0 -18
  274. data/lib/shopify_api/resources/discount.rb +0 -11
  275. data/lib/shopify_api/resources/o_auth.rb +0 -9
  276. data/test/discount_test.rb +0 -52
  277. data/test/fixtures/discount.json +0 -17
  278. data/test/fixtures/discount_disabled.json +0 -17
  279. data/test/fixtures/discounts.json +0 -34
  280. data/test/fixtures/o_auth_revoke.json +0 -5
  281. data/test/o_auth_test.rb +0 -8
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'shopify_api/connection'
2
3
 
3
4
  module ActiveResource
@@ -1,19 +1,55 @@
1
+ # frozen_string_literal: true
1
2
  module ActiveResource
2
3
  class DetailedLogSubscriber < ActiveSupport::LogSubscriber
4
+ VERSION_EOL_WARNING_HEADER = 'x-shopify-api-version-warning'
5
+ VERSION_DEPRECATION_HEADER = 'x-shopify-api-deprecated-reason'
6
+ SHOPIFY_ACCESS_TOKEN = 'X-Shopify-Access-Token'
7
+ FILTERED = '[FILTERED]'
8
+
3
9
  def request(event)
10
+ log_request_response_details(event)
11
+ warn_on_deprecated_header_or_version_eol_header(event)
12
+ end
13
+
14
+ def logger
15
+ ActiveResource::Base.logger
16
+ end
17
+
18
+ private
19
+
20
+ def log_request_response_details(event)
4
21
  data = event.payload[:data]
5
22
  headers = data.extract_options!
23
+ headers[SHOPIFY_ACCESS_TOKEN] = FILTERED
6
24
  request_body = data.first
7
25
 
8
- info "Request:\n#{request_body}" if request_body
9
- info "Headers: #{headers.inspect}"
10
- info "Response:\n#{event.payload[:response].body}"
26
+ info("Request:\n#{request_body}") if request_body
27
+ info("Headers: #{headers.inspect}")
28
+ info("Response:\n#{event.payload[:response].body}")
11
29
  end
12
30
 
13
- def logger
14
- ActiveResource::Base.logger
31
+ def warn_on_deprecated_header_or_version_eol_header(event)
32
+ payload = event.payload
33
+
34
+ payload[:response].each do |header_name, header_value|
35
+ case header_name.downcase
36
+ when VERSION_DEPRECATION_HEADER
37
+ warning_message = <<-MSG
38
+ [DEPRECATED] ShopifyAPI made a call to #{payload[:method].upcase} #{payload[:path]}, and this call made
39
+ use of a deprecated endpoint, behaviour, or parameter. See #{header_value} for more details.
40
+ MSG
41
+
42
+ warn(warning_message)
43
+
44
+ when VERSION_EOL_WARNING_HEADER
45
+ warning_message = <<-MSG
46
+ [API Version Warning] ShopifyAPI made a call to #{payload[:method].upcase} #{payload[:path]}, and this call used
47
+ an API version that is unsupported or will become unsupported within 30 days. See #{header_value} for more details.
48
+ MSG
49
+
50
+ warn(warning_message)
51
+ end
52
+ end
15
53
  end
16
54
  end
17
55
  end
18
-
19
- ActiveResource::DetailedLogSubscriber.attach_to :active_resource_detailed
@@ -1,9 +1,15 @@
1
+ # frozen_string_literal: true
1
2
  require 'active_resource/base'
2
3
 
3
4
  module ActiveResource
4
5
  class Errors < ActiveModel::Errors
5
6
  def from_json(json, save_cache = false)
6
- data = ActiveSupport::JSON.decode(json)['errors'] || {} rescue {}
7
+ data =
8
+ begin
9
+ ActiveSupport::JSON.decode(json)['errors'] || {}
10
+ rescue
11
+ {}
12
+ end
7
13
  case data
8
14
  when String
9
15
  from_string(data, save_cache)
@@ -15,7 +21,7 @@ module ActiveResource
15
21
  def from_hash(messages, save_cache = false)
16
22
  clear unless save_cache
17
23
 
18
- messages.each do |key,errors|
24
+ messages.each do |key, errors|
19
25
  errors.each do |error|
20
26
  add(key, error)
21
27
  end
data/lib/shopify_api.rb CHANGED
@@ -1,16 +1,16 @@
1
- $:.unshift File.dirname(__FILE__)
2
-
1
+ # frozen_string_literal: true
2
+ $:.unshift(File.dirname(__FILE__))
3
3
  require 'active_resource'
4
4
  require 'active_support/core_ext/class/attribute_accessors'
5
5
  require 'digest/md5'
6
6
  require 'base64'
7
7
  require 'active_resource/detailed_log_subscriber'
8
8
  require 'shopify_api/limits'
9
- require 'shopify_api/json_format'
9
+ require 'shopify_api/api_version'
10
+ require 'shopify_api/meta'
10
11
  require 'active_resource/json_errors'
11
- require 'active_resource/disable_prefix_check'
12
- require 'active_resource/base_ext'
13
- require 'active_resource/to_query'
12
+ require 'shopify_api/paginated_collection'
13
+ require 'shopify_api/disable_prefix_check'
14
14
 
15
15
  module ShopifyAPI
16
16
  include Limits
@@ -21,10 +21,20 @@ require 'shopify_api/metafields'
21
21
  require 'shopify_api/countable'
22
22
  require 'shopify_api/resources'
23
23
  require 'shopify_api/session'
24
+ require 'shopify_api/api_access'
25
+ require 'shopify_api/message_enricher'
24
26
  require 'shopify_api/connection'
27
+ require 'shopify_api/pagination_link_headers'
28
+ require 'shopify_api/graphql'
29
+ require 'shopify_api/graphql/railtie' if defined?(Rails)
25
30
 
26
31
  if ShopifyAPI::Base.respond_to?(:connection_class)
27
32
  ShopifyAPI::Base.connection_class = ShopifyAPI::Connection
28
33
  else
29
34
  require 'active_resource/connection_ext'
30
35
  end
36
+
37
+ if ENV["SHOPIFY_LOG_PATH"]
38
+ ActiveResource::Base.logger = Logger.new(ENV["SHOPIFY_LOG_PATH"])
39
+ ActiveResource::DetailedLogSubscriber.attach_to(:active_resource_detailed)
40
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShopifyAPI
4
+ class ApiAccess
5
+ SCOPE_DELIMITER = ','
6
+
7
+ def initialize(scope_names)
8
+ if scope_names.is_a?(String)
9
+ scope_names = scope_names.to_s.split(SCOPE_DELIMITER)
10
+ end
11
+
12
+ store_scopes(scope_names)
13
+ end
14
+
15
+ def covers?(scopes)
16
+ scopes.compressed_scopes <= expanded_scopes
17
+ end
18
+
19
+ def to_s
20
+ to_a.join(SCOPE_DELIMITER)
21
+ end
22
+
23
+ def to_a
24
+ compressed_scopes.to_a
25
+ end
26
+
27
+ def ==(other)
28
+ other.class == self.class &&
29
+ compressed_scopes == other.compressed_scopes
30
+ end
31
+
32
+ alias :eql? :==
33
+
34
+ def hash
35
+ compressed_scopes.hash
36
+ end
37
+
38
+ protected
39
+
40
+ attr_reader :compressed_scopes, :expanded_scopes
41
+
42
+ private
43
+
44
+ def store_scopes(scope_names)
45
+ scopes = scope_names.map(&:strip).reject(&:empty?).to_set
46
+ implied_scopes = scopes.map { |scope| implied_scope(scope) }.compact
47
+
48
+ @compressed_scopes = scopes - implied_scopes
49
+ @expanded_scopes = scopes + implied_scopes
50
+ end
51
+
52
+ def implied_scope(scope)
53
+ is_write_scope = scope =~ /\A(unauthenticated_)?write_(.*)\z/
54
+ "#{$1}read_#{$2}" if is_write_scope
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,206 @@
1
+ # frozen_string_literal: true
2
+ module ShopifyAPI
3
+ class ApiVersion
4
+ class UnknownVersion < StandardError; end
5
+ class ApiVersionNotSetError < StandardError; end
6
+ include Comparable
7
+
8
+ UNSTABLE_HANDLE = 'unstable'
9
+ HANDLE_FORMAT = /((\d{4}-\d{2})|#{UNSTABLE_HANDLE})/.freeze
10
+ UNSTABLE_AS_DATE = Time.utc(3000, 1, 1)
11
+ API_PREFIX = '/admin/api/'
12
+ LOOKUP_MODES = [:raise_on_unknown, :define_on_unknown].freeze
13
+
14
+ class << self
15
+ attr_reader :versions
16
+
17
+ def version_lookup_mode
18
+ @version_lookup_mode ||= :define_on_unknown
19
+ end
20
+
21
+ def version_lookup_mode=(mode)
22
+ raise ArgumentError, "Mode must be one of #{LOOKUP_MODES}" unless LOOKUP_MODES.include?(mode)
23
+ sanitize_known_versions if mode == :raise_on_unknown
24
+ @version_lookup_mode = mode
25
+ end
26
+
27
+ def find_version(version_or_handle)
28
+ raise ArgumentError, "NullVersion is not a valid version or version handle." if version_or_handle == NullVersion
29
+ return version_or_handle if version_or_handle.is_a?(ApiVersion)
30
+ handle = version_or_handle.to_s
31
+ @versions ||= {}
32
+ @versions.fetch(handle) do
33
+ if @version_lookup_mode == :raise_on_unknown
34
+ raise UnknownVersion, unknown_version_error_message(handle)
35
+ else
36
+ add_to_known_versions(ApiVersion.new(handle: handle))
37
+ end
38
+ end
39
+ end
40
+
41
+ def coerce_to_version(version_or_handle)
42
+ warn(
43
+ '[DEPRECATED] ShopifyAPI::ApiVersion.coerce_to_version be removed in a future version. ' \
44
+ 'Use `find_version` instead.'
45
+ )
46
+ find_version(version_or_handle)
47
+ end
48
+
49
+ def fetch_known_versions
50
+ @versions = Meta.admin_versions.map do |version|
51
+ [version.handle, ApiVersion.new(version.attributes.merge(verified: version.persisted?))]
52
+ end.to_h
53
+ end
54
+
55
+ def define_known_versions
56
+ warn(
57
+ '[DEPRECATED] ShopifyAPI::ApiVersion.define_known_versions is deprecated and will be ' \
58
+ 'removed in a future version. Use `fetch_known_versions` instead.'
59
+ )
60
+ fetch_known_versions
61
+ end
62
+
63
+ def add_to_known_versions(version)
64
+ @versions[version.handle] = version
65
+ end
66
+
67
+ def clear_known_versions
68
+ @versions = {}
69
+ end
70
+
71
+ def clear_defined_versions
72
+ warn(
73
+ '[DEPRECATED] ShopifyAPI::ApiVersion.clear_defined_versions is deprecated and will be ' \
74
+ 'removed in a future version. Use `clear_known_versions` instead.'
75
+ )
76
+ clear_known_versions
77
+ end
78
+
79
+ def latest_stable_version
80
+ warn(
81
+ '[DEPRECATED] ShopifyAPI::ApiVersion.latest_stable_version is deprecated and will be ' \
82
+ 'removed in a future version.'
83
+ )
84
+ versions.values.find(&:latest_supported?)
85
+ end
86
+
87
+ private
88
+
89
+ def sanitize_known_versions
90
+ return if @versions.nil?
91
+ @versions = @versions.keys.map do |handle|
92
+ next unless @versions[handle].verified?
93
+ [handle, @versions[handle]]
94
+ end.compact.to_h
95
+ end
96
+
97
+ def unknown_version_error_message(handle)
98
+ msg = "ApiVersion.version_lookup_mode is set to `:raise_on_unknown`. \n"
99
+ return msg + "No versions defined. You must call `ApiVersion.fetch_known_versions` first." if @versions.empty?
100
+ msg + "`#{handle}` is not in the defined version set. Available versions: #{@versions.keys}"
101
+ end
102
+ end
103
+
104
+ attr_reader :handle, :display_name, :supported, :latest_supported, :verified
105
+
106
+ def initialize(attributes)
107
+ attributes = ActiveSupport::HashWithIndifferentAccess.new(attributes)
108
+ @handle = attributes[:handle].to_s
109
+ @display_name = attributes.fetch(:display_name, attributes[:handle].to_s)
110
+ @supported = attributes.fetch(:supported, false)
111
+ @latest_supported = attributes.fetch(:latest_supported, false)
112
+ @verified = attributes.fetch(:verified, false)
113
+ end
114
+
115
+ def to_s
116
+ handle
117
+ end
118
+
119
+ def latest_supported?
120
+ latest_supported
121
+ end
122
+
123
+ def supported?
124
+ supported
125
+ end
126
+
127
+ def verified?
128
+ verified
129
+ end
130
+
131
+ def <=>(other)
132
+ handle_as_date <=> other.handle_as_date
133
+ end
134
+
135
+ def ==(other)
136
+ other.class == self.class && handle == other.handle
137
+ end
138
+
139
+ def hash
140
+ handle.hash
141
+ end
142
+
143
+ def construct_api_path(path)
144
+ "#{API_PREFIX}#{handle}/#{path}"
145
+ end
146
+
147
+ def construct_graphql_path
148
+ construct_api_path('graphql.json')
149
+ end
150
+
151
+ def name
152
+ warn(
153
+ '[DEPRECATED] ShopifyAPI::ApiVersion#name is deprecated and will be removed in a future version. ' \
154
+ 'Use `handle` instead.'
155
+ )
156
+ handle
157
+ end
158
+
159
+ def stable?
160
+ warn(
161
+ '[DEPRECATED] ShopifyAPI::ApiVersion#stable? is deprecated and will be removed in a future version. ' \
162
+ 'Use `supported?` instead.'
163
+ )
164
+ supported?
165
+ end
166
+
167
+ def unstable?
168
+ handle == UNSTABLE_HANDLE
169
+ end
170
+
171
+ def handle_as_date
172
+ return UNSTABLE_AS_DATE if unstable?
173
+ year, month, day = handle.split('-')
174
+ Time.utc(year, month, day)
175
+ end
176
+
177
+ class NullVersion
178
+ class << self
179
+ def new(*_args)
180
+ raise NoMethodError, "NullVersion is an abstract class and cannot be instantiated."
181
+ end
182
+
183
+ def matches?(version)
184
+ version.nil? || version == self
185
+ end
186
+
187
+ def raise_not_set_error(*_args)
188
+ raise ApiVersionNotSetError, "You must set ShopifyAPI::Base.api_version before making a request."
189
+ end
190
+ alias_method :stable?, :raise_not_set_error
191
+ alias_method :construct_api_path, :raise_not_set_error
192
+ alias_method :construct_graphql_path, :raise_not_set_error
193
+ alias_method :latest_supported?, :raise_not_set_error
194
+ alias_method :supported?, :raise_not_set_error
195
+ alias_method :verified?, :raise_not_set_error
196
+ alias_method :unstable?, :raise_not_set_error
197
+ alias_method :handle, :raise_not_set_error
198
+ alias_method :display_name, :raise_not_set_error
199
+ alias_method :supported, :raise_not_set_error
200
+ alias_method :verified, :raise_not_set_error
201
+ alias_method :latest_supported, :raise_not_set_error
202
+ alias_method :name, :raise_not_set_error
203
+ end
204
+ end
205
+ end
206
+ end
@@ -1,10 +1,11 @@
1
+ # frozen_string_literal: true
1
2
  module ShopifyAPI
2
3
  class Connection < ActiveResource::Connection
3
4
  attr_reader :response
4
5
 
5
6
  module ResponseCapture
6
7
  def handle_response(response)
7
- @response = super
8
+ @response = super(ShopifyAPI::MessageEnricher.new(response))
8
9
  end
9
10
  end
10
11
 
@@ -13,15 +14,17 @@ module ShopifyAPI
13
14
  module RequestNotification
14
15
  def request(method, path, *arguments)
15
16
  super.tap do |response|
16
- notify_about_request(response, arguments)
17
+ notify_about_request(method, path, response, arguments)
17
18
  end
18
19
  rescue => e
19
- notify_about_request(e.response, arguments) if e.respond_to?(:response)
20
+ notify_about_request(method, path, e.response, arguments) if e.respond_to?(:response)
20
21
  raise
21
22
  end
22
23
 
23
- def notify_about_request(response, arguments)
24
+ def notify_about_request(method, path, response, arguments)
24
25
  ActiveSupport::Notifications.instrument("request.active_resource_detailed") do |payload|
26
+ payload[:method] = method
27
+ payload[:path] = path
25
28
  payload[:response] = response
26
29
  payload[:data] = arguments
27
30
  end
@@ -1,11 +1,12 @@
1
+ # frozen_string_literal: true
1
2
  module ShopifyAPI
2
3
  module Countable
3
4
  def count(options = {})
4
5
  data = get(:count, options)
5
6
 
6
7
  count = case data
7
- when Hash then data["count"]
8
- else data
8
+ when Hash then data["count"]
9
+ else data
9
10
  end
10
11
 
11
12
  Integer(count)