flex_commerce_api 0.6.57

Sign up to get free protection for your applications and to get access to all the features.
Files changed (170) hide show
  1. checksums.yaml +7 -0
  2. data/.env.example +6 -0
  3. data/.gitignore +14 -0
  4. data/.rspec +4 -0
  5. data/.rubocop.yml +1065 -0
  6. data/.ruby-gemset +1 -0
  7. data/.ruby-version +1 -0
  8. data/.travis.yml +3 -0
  9. data/Gemfile +4 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +56 -0
  12. data/Rakefile +1 -0
  13. data/app/models/address.rb +41 -0
  14. data/app/models/asset_file.rb +26 -0
  15. data/app/models/asset_folder.rb +14 -0
  16. data/app/models/bundle.rb +20 -0
  17. data/app/models/bundle_group.rb +15 -0
  18. data/app/models/cart.rb +136 -0
  19. data/app/models/category.rb +70 -0
  20. data/app/models/category_tree.rb +11 -0
  21. data/app/models/component.rb +8 -0
  22. data/app/models/container_coupon.rb +12 -0
  23. data/app/models/country.rb +11 -0
  24. data/app/models/coupon.rb +18 -0
  25. data/app/models/customer_account.rb +96 -0
  26. data/app/models/customer_account_authentication.rb +5 -0
  27. data/app/models/customer_segment.rb +6 -0
  28. data/app/models/customer_segment_member.rb +6 -0
  29. data/app/models/data_attribute.rb +6 -0
  30. data/app/models/data_store_record.rb +9 -0
  31. data/app/models/data_store_type.rb +9 -0
  32. data/app/models/discount_summary.rb +12 -0
  33. data/app/models/email.rb +5 -0
  34. data/app/models/ewis_opt_in.rb +8 -0
  35. data/app/models/external_url.rb +6 -0
  36. data/app/models/free_shipping_promotion.rb +12 -0
  37. data/app/models/import.rb +6 -0
  38. data/app/models/import_entry.rb +6 -0
  39. data/app/models/line_item.rb +34 -0
  40. data/app/models/line_item_discount.rb +7 -0
  41. data/app/models/markdown_price.rb +11 -0
  42. data/app/models/menu.rb +36 -0
  43. data/app/models/menu_item.rb +7 -0
  44. data/app/models/menu_item_item.rb +5 -0
  45. data/app/models/note.rb +18 -0
  46. data/app/models/order.rb +38 -0
  47. data/app/models/password_recovery.rb +20 -0
  48. data/app/models/payment_address_verification.rb +13 -0
  49. data/app/models/payment_process.rb +13 -0
  50. data/app/models/payment_provider.rb +15 -0
  51. data/app/models/payment_transaction.rb +13 -0
  52. data/app/models/product.rb +99 -0
  53. data/app/models/product_asset_file.rb +12 -0
  54. data/app/models/promotion.rb +19 -0
  55. data/app/models/promotion_qualifying_product_exclusion.rb +8 -0
  56. data/app/models/redirect.rb +14 -0
  57. data/app/models/refund.rb +14 -0
  58. data/app/models/remote_address.rb +22 -0
  59. data/app/models/remote_line_item.rb +11 -0
  60. data/app/models/remote_order.rb +15 -0
  61. data/app/models/remote_shipping_method.rb +12 -0
  62. data/app/models/report.rb +18 -0
  63. data/app/models/report_invocation.rb +18 -0
  64. data/app/models/retail_store.rb +18 -0
  65. data/app/models/role.rb +6 -0
  66. data/app/models/search_suggestion.rb +17 -0
  67. data/app/models/section.rb +9 -0
  68. data/app/models/session.rb +8 -0
  69. data/app/models/shipping_method.rb +26 -0
  70. data/app/models/slug.rb +19 -0
  71. data/app/models/static_page.rb +60 -0
  72. data/app/models/static_page_folder.rb +8 -0
  73. data/app/models/stock_level.rb +21 -0
  74. data/app/models/tax_code.rb +6 -0
  75. data/app/models/taxonomy.rb +5 -0
  76. data/app/models/template.rb +9 -0
  77. data/app/models/template_component.rb +11 -0
  78. data/app/models/template_definition.rb +12 -0
  79. data/app/models/template_section.rb +12 -0
  80. data/app/models/user.rb +8 -0
  81. data/app/models/user_profile.rb +6 -0
  82. data/app/models/v2/create_order.rb +10 -0
  83. data/app/models/v2/deallocate_order.rb +10 -0
  84. data/app/models/v2/line_item.rb +9 -0
  85. data/app/models/v2/order.rb +9 -0
  86. data/app/models/v2/unallocate_order.rb +10 -0
  87. data/app/models/variant.rb +18 -0
  88. data/app/models/webhook.rb +17 -0
  89. data/app/services/param_to_shql.rb +72 -0
  90. data/app/services/surrogate_keys.rb +44 -0
  91. data/bin/console +14 -0
  92. data/bin/setup +7 -0
  93. data/config/locales/payments.en.yml +3 -0
  94. data/flex-commerce-api.gemspec +41 -0
  95. data/lib/flex_commerce.rb +95 -0
  96. data/lib/flex_commerce_api.rb +21 -0
  97. data/lib/flex_commerce_api/api_base.rb +11 -0
  98. data/lib/flex_commerce_api/base_resource.rb +250 -0
  99. data/lib/flex_commerce_api/config.rb +55 -0
  100. data/lib/flex_commerce_api/error/access_denied.rb +6 -0
  101. data/lib/flex_commerce_api/error/bad_request.rb +10 -0
  102. data/lib/flex_commerce_api/error/base.rb +12 -0
  103. data/lib/flex_commerce_api/error/client_error.rb +7 -0
  104. data/lib/flex_commerce_api/error/connection_error.rb +6 -0
  105. data/lib/flex_commerce_api/error/internal_server.rb +37 -0
  106. data/lib/flex_commerce_api/error/not_found.rb +13 -0
  107. data/lib/flex_commerce_api/error/record_invalid.rb +16 -0
  108. data/lib/flex_commerce_api/error/unexpected_status.rb +7 -0
  109. data/lib/flex_commerce_api/errors.rb +13 -0
  110. data/lib/flex_commerce_api/json_api_client_extension/builder.rb +28 -0
  111. data/lib/flex_commerce_api/json_api_client_extension/capture_surrogate_keys_middleware.rb +16 -0
  112. data/lib/flex_commerce_api/json_api_client_extension/flexible_connection.rb +59 -0
  113. data/lib/flex_commerce_api/json_api_client_extension/has_many_association_proxy.rb +60 -0
  114. data/lib/flex_commerce_api/json_api_client_extension/included_data.rb +27 -0
  115. data/lib/flex_commerce_api/json_api_client_extension/json_format_middleware.rb +20 -0
  116. data/lib/flex_commerce_api/json_api_client_extension/logging_middleware.rb +24 -0
  117. data/lib/flex_commerce_api/json_api_client_extension/paginator.rb +26 -0
  118. data/lib/flex_commerce_api/json_api_client_extension/parse_json.rb +23 -0
  119. data/lib/flex_commerce_api/json_api_client_extension/parsers/parser.rb +16 -0
  120. data/lib/flex_commerce_api/json_api_client_extension/previewed_request_middleware.rb +17 -0
  121. data/lib/flex_commerce_api/json_api_client_extension/remote_builder.rb +29 -0
  122. data/lib/flex_commerce_api/json_api_client_extension/requestor.rb +42 -0
  123. data/lib/flex_commerce_api/json_api_client_extension/save_request_body_middleware.rb +20 -0
  124. data/lib/flex_commerce_api/json_api_client_extension/status_middleware.rb +40 -0
  125. data/lib/flex_commerce_api/v2/api_base.rb +13 -0
  126. data/lib/flex_commerce_api/version.rb +3 -0
  127. data/lib/json_erb.rb +9 -0
  128. data/lib/json_struct.rb +73 -0
  129. data/lib/patches.rb +4 -0
  130. data/lib/patches/json_api_client/resource.rb +50 -0
  131. data/lib/paypal_express.rb +3 -0
  132. data/lib/paypal_express/additional_info.rb +45 -0
  133. data/lib/paypal_express/api.rb +86 -0
  134. data/lib/paypal_express/auth.rb +83 -0
  135. data/lib/paypal_express/cart_shipping_method.rb +38 -0
  136. data/lib/paypal_express/exception/access_denied.rb +10 -0
  137. data/lib/paypal_express/exception/connection_error.rb +10 -0
  138. data/lib/paypal_express/exception/not_authorized.rb +10 -0
  139. data/lib/paypal_express/exception/transaction.rb +15 -0
  140. data/lib/paypal_express/generate_summary.rb +118 -0
  141. data/lib/paypal_express/process/paypal_params.rb +123 -0
  142. data/lib/paypal_express/process/response_parser.rb +146 -0
  143. data/lib/paypal_express/setup.rb +94 -0
  144. data/lib/paypal_express/shipping_methods_for_cart.rb +46 -0
  145. data/lib/retry.rb +20 -0
  146. data/schemas/jsonapi/schema.json +370 -0
  147. data/schemas/shift/v1/documents/collection/address.json +45 -0
  148. data/schemas/shift/v1/documents/collection/asset_file.json +43 -0
  149. data/schemas/shift/v1/documents/collection/asset_folder.json +43 -0
  150. data/schemas/shift/v1/documents/collection/customer_account.json +50 -0
  151. data/schemas/shift/v1/documents/collection/markdown_price.json +43 -0
  152. data/schemas/shift/v1/documents/collection/product.json +43 -0
  153. data/schemas/shift/v1/documents/collection/variant.json +43 -0
  154. data/schemas/shift/v1/documents/member/address.json +39 -0
  155. data/schemas/shift/v1/documents/member/asset_file.json +37 -0
  156. data/schemas/shift/v1/documents/member/asset_folder.json +39 -0
  157. data/schemas/shift/v1/documents/member/customer_account.json +44 -0
  158. data/schemas/shift/v1/documents/member/markdown_price.json +37 -0
  159. data/schemas/shift/v1/documents/member/product.json +39 -0
  160. data/schemas/shift/v1/documents/member/variant.json +46 -0
  161. data/schemas/shift/v1/resources/address.json +130 -0
  162. data/schemas/shift/v1/resources/asset_file.json +146 -0
  163. data/schemas/shift/v1/resources/asset_folder.json +188 -0
  164. data/schemas/shift/v1/resources/customer_account.json +339 -0
  165. data/schemas/shift/v1/resources/markdown_price.json +52 -0
  166. data/schemas/shift/v1/resources/product.json +230 -0
  167. data/schemas/shift/v1/resources/variant.json +298 -0
  168. data/tasks/json_schema.thor +275 -0
  169. data/todo.md +8 -0
  170. metadata +470 -0
@@ -0,0 +1,12 @@
1
+ module FlexCommerceApi
2
+ module Error
3
+ class Base < StandardError
4
+ attr_reader :response_env, :request_env
5
+ def initialize(request_env = nil, response_env = nil)
6
+ @response_env = response_env
7
+ @request_env = request_env
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ module FlexCommerceApi
2
+ module Error
3
+ class ClientError < Base
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ module FlexCommerceApi
2
+ module Error
3
+ class ConnectionError < Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,37 @@
1
+ module FlexCommerceApi
2
+ module Error
3
+ class InternalServer < Base
4
+ def message
5
+ body = response_env.fetch(:body, {"errors" => []})
6
+ error = extract_error(body)
7
+ return "Internal server error" unless error.present?
8
+ if error.is_a?(::Enumerable)
9
+ title = error.fetch("title", "")
10
+ detail = error.fetch("detail", "")
11
+ meta = error.fetch("meta", {})
12
+ exception = meta.fetch("exception", "")
13
+ backtrace = meta.fetch("backtrace", [])
14
+ event_id = meta.fetch("event_id", "")
15
+ "Internal server error - #{title} #{detail} #{event_id} #{exception} #{backtrace}"
16
+ else
17
+ "Internal server error - #{error}"
18
+ end
19
+ end
20
+
21
+ def raven_context
22
+ {
23
+ extra: {
24
+ body: response_env[:body]
25
+ }
26
+ }
27
+ end
28
+
29
+ private
30
+
31
+ def extract_error(body)
32
+ return body if body.is_a?(::String)
33
+ body["message"] || body["errors"]&.first
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,13 @@
1
+ module FlexCommerceApi
2
+ module Error
3
+ class NotFound < InternalServer
4
+ attr_reader :uri
5
+ def initialize(uri)
6
+ @uri = uri
7
+ end
8
+ def message
9
+ "Couldn't find resource at: #{uri.to_s}"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ module FlexCommerceApi
2
+ module Error
3
+ class RecordInvalid < Base
4
+ attr_reader :record
5
+
6
+ def initialize(record)
7
+ @record = record
8
+ end
9
+
10
+ def message
11
+ "Record Invalid - #{record.errors.full_messages.join(", ")}"
12
+ end
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ module FlexCommerceApi
2
+ module Error
3
+ class UnexpectedStatus < JsonApiClient::Errors::UnexpectedStatus
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ module FlexCommerceApi
2
+ module Error
3
+ autoload :Base, File.expand_path(File.join("error", "base"), __dir__)
4
+ autoload :ClientError, File.expand_path(File.join("error", "client_error"), __dir__)
5
+ autoload :ConnectionError, File.expand_path(File.join("error", "connection_error"), __dir__)
6
+ autoload :InternalServer, File.expand_path(File.join("error", "internal_server"), __dir__)
7
+ autoload :UnexpectedStatus, File.expand_path(File.join("error", "unexpected_status"), __dir__)
8
+ autoload :NotFound, File.expand_path(File.join("error", "not_found"), __dir__)
9
+ autoload :AccessDenied, File.expand_path(File.join("error", "access_denied"), __dir__)
10
+ autoload :RecordInvalid, File.expand_path(File.join("error", "record_invalid"), __dir__)
11
+ autoload :BadRequest, File.expand_path(File.join("error", "bad_request"), __dir__)
12
+ end
13
+ end
@@ -0,0 +1,28 @@
1
+ module FlexCommerceApi
2
+ module JsonApiClientExtension
3
+ class Builder < ::JsonApiClient::Query::Builder
4
+ def initialize(*)
5
+ super
6
+ @temp_search_criteria = nil
7
+ end
8
+ def temp_search(options = {})
9
+ @temp_search_criteria = options
10
+ self
11
+ end
12
+
13
+ def find(args = {})
14
+ case args
15
+ when Hash
16
+ where(args)
17
+ else
18
+ @primary_key = args
19
+ end
20
+ if @temp_search_criteria.nil?
21
+ klass.requestor.get(params)
22
+ else
23
+ klass.requestor.custom(:search, { request_method: :get }, params.merge(filter: @temp_search_criteria))
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ # Captures surrogate keys headers and collects them for passing to the client.
2
+ module FlexCommerceApi
3
+ module JsonApiClientExtension
4
+ class CaptureSurrogateKeysMiddleware < ::Faraday::Middleware
5
+ def call(environment)
6
+ @app.call(environment).on_complete do |env|
7
+ surrogate_keys = env.response_headers['external-surrogate-key'].split(' ') if env.response_headers['external-surrogate-key']
8
+
9
+ if surrogate_keys && Thread.current[:shift_surrogate_keys]
10
+ Thread.current[:shift_surrogate_keys].concat(surrogate_keys)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,59 @@
1
+ require 'faraday-http-cache'
2
+
3
+ module FlexCommerceApi
4
+ module JsonApiClientExtension
5
+ class FlexibleConnection < JsonApiClient::Connection
6
+ attr_accessor :last_response
7
+ def initialize(options = {})
8
+ site = options.fetch(:site)
9
+ adapter_options = Array(options.fetch(:adapter, Faraday.default_adapter))
10
+ add_json_api_extension = options.fetch(:add_json_api_extension, true)
11
+ authenticate = options.fetch(:authenticate, true)
12
+ include_previewed = options.fetch :include_previewed, false
13
+ @faraday = Faraday.new(site) do |builder|
14
+ builder.request :json
15
+ builder.use JsonApiClientExtension::SaveRequestBodyMiddleware
16
+ builder.use JsonApiClientExtension::JsonFormatMiddleware if add_json_api_extension
17
+ builder.use JsonApiClientExtension::PreviewedRequestMiddleware if include_previewed
18
+ builder.use JsonApiClient::Middleware::JsonRequest
19
+ # Surrogate Key middleware should always be above HTTP caching to ensure we're reading headers
20
+ # from the original response not the 304 responses
21
+ builder.use JsonApiClientExtension::CaptureSurrogateKeysMiddleware
22
+ # disable the cache when HTTP cache is set to false
23
+ unless false == options[:http_cache]
24
+ builder.use :http_cache, cache_options(options)
25
+ end
26
+ builder.use JsonApiClientExtension::StatusMiddleware
27
+ builder.use JsonApiClient::Middleware::ParseJson
28
+ builder.use JsonApiClientExtension::LoggingMiddleware unless FlexCommerceApi.logger.nil?
29
+ builder.adapter *adapter_options
30
+ builder.options[:open_timeout] = options.fetch(:open_timeout)
31
+ builder.options[:timeout] = options.fetch(:timeout)
32
+ end
33
+ faraday.basic_auth(ApiBase.username, ApiBase.password) if authenticate
34
+
35
+ yield(self) if block_given?
36
+ end
37
+
38
+ def run(*args)
39
+ super.tap do |response|
40
+ self.last_response = response
41
+ end
42
+ end
43
+
44
+ private
45
+ def cache_options(options)
46
+ {
47
+ # treats the cache like a client, not a proxy
48
+ shared_cache: false,
49
+ # use the Rails cache, if set, otherwise default to MemoryStore
50
+ store: defined?(::Rails) ? ::Rails.cache : nil,
51
+ # serialize the data using Marshal
52
+ serializer: Marshal,
53
+ # use our configured logger
54
+ logger: FlexCommerceApi.logger
55
+ }.merge(options.fetch(:http_cache, {}))
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,60 @@
1
+ require "active_support/core_ext/module/delegation"
2
+
3
+ module FlexCommerceApi
4
+ module JsonApiClientExtension
5
+ class HasManyAssociationProxy
6
+ def initialize(real_instance, container_instance, association_name, options)
7
+ self.real_instance = real_instance
8
+ self.container_instance = container_instance
9
+ self.association_name = association_name
10
+ self.options = options
11
+ end
12
+
13
+ delegate :empty?, :each, :each_with_index, :length, :count, :map, :select, :detect, :first, :last, :[], :any?, to: :real_instance
14
+ #
15
+ # Finds an instance by primary key
16
+ #
17
+ # @param [Integer|Number|Float] pk The primary key value to be found
18
+ # @return The resource found or nil if not found
19
+ def find(pk)
20
+ method = primary_key
21
+ real_instance.detect {|i| i.send(method) == pk}
22
+ end
23
+
24
+ # Creates a new associated item - if the option "inverse_of" is specified, then
25
+ # automatically create a relationship back to the container with the name
26
+ # specified in "inverse_of"
27
+ def new(attrs)
28
+ my_attrs = attrs.dup
29
+ if options.key?(:inverse_of)
30
+ my_attrs[:relationships] ||= {}
31
+ # Dont modify the original relationship if it was present below
32
+ my_attrs[:relationships] = my_attrs[:relationships].merge(options[:inverse_of] => container_instance)
33
+ end
34
+ association.association_class.new(my_attrs)
35
+ end
36
+
37
+ def create(attrs)
38
+ new(attrs).tap do |instance|
39
+ instance.save
40
+ end
41
+ end
42
+
43
+ def to_a
44
+ real_instance
45
+ end
46
+
47
+ private
48
+
49
+ def association
50
+ container_instance.class.associations.detect {|a| a.attr_name == association_name.to_sym}
51
+ end
52
+
53
+ def primary_key
54
+ association.association_class.primary_key
55
+ end
56
+
57
+ attr_accessor :real_instance, :container_instance, :association_name, :options
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,27 @@
1
+ require 'json_api_client/included_data'
2
+
3
+ module FlexCommerceApi
4
+ module JsonApiClientExtension
5
+ class IncludedData < ::JsonApiClient::IncludedData
6
+ def initialize(result_set, *args)
7
+ super(result_set, *args)
8
+ @result_set = result_set
9
+ end
10
+
11
+ private
12
+
13
+ # attempt to load the record from included data first, failing that, look
14
+ # in the root resource(s) for the record
15
+ def record_for(link_def)
16
+ data.dig(link_def["type"], link_def["id"]) || root_record_for(link_def)
17
+ end
18
+
19
+ def root_record_for(link_def)
20
+ @result_set.find do |resource|
21
+ resource.attributes["type"] == link_def["type"] &&
22
+ resource.attributes["id"] == link_def["id"]
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,20 @@
1
+ module FlexCommerceApi
2
+ module JsonApiClientExtension
3
+ #
4
+ #
5
+ # @!visibility private
6
+ # This class is used internally to modify the URL to have .json at the end to
7
+ # suit the flex platform.
8
+ class JsonFormatMiddleware < ::Faraday::Middleware
9
+ #
10
+ # Adds .json to the URL before it gets sent
11
+ #
12
+ def call(env)
13
+ env.url.tap do |url|
14
+ url.path << ".json_api" unless url.path=~/\.json_api$/
15
+ end
16
+ @app.call(env)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,24 @@
1
+ module FlexCommerceApi
2
+ module JsonApiClientExtension
3
+ #
4
+ #
5
+ class LoggingMiddleware < ::Faraday::Middleware
6
+ attr_accessor :request_id, :logger
7
+ def initialize(*args)
8
+ super
9
+ self.request_id = 0
10
+ self.logger = FlexCommerceApi.logger
11
+ end
12
+ #
13
+ #
14
+ def call(env)
15
+ self.request_id += 1
16
+ @app.call(env).on_complete do |response_env|
17
+ logger.debug("FlexApi::Request id #{request_id} #{env.method.to_s.upcase} #{env.url} started at #{Time.now}")
18
+ logger.debug("\t >>> #{env[:request_body]}") unless env[:request_body].nil? || env[:request_body].empty?
19
+ logger.debug("\t <<< (#{env[:status]}) #{env.body}")
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,26 @@
1
+ module FlexCommerceApi
2
+ module JsonApiClientExtension
3
+ # @!visibility private
4
+ # This class overrides the default paginator to allow for
5
+ # the custom things that are required by the flex platform.
6
+ # This class is private and should not be used by end users
7
+ class Paginator < ::JsonApiClient::Paginating::Paginator
8
+
9
+ def current_page
10
+ params.fetch("page[number]", 1).to_i
11
+ end
12
+
13
+ def total_pages
14
+ result_set.meta.page_count
15
+ end
16
+
17
+ def total_entries
18
+ result_set.meta.total_entries
19
+ end
20
+
21
+
22
+ protected
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,23 @@
1
+ # This module extends json_api_client's ParseJson service
2
+ # to make sure malformed repsonse bodies is always included as context in
3
+ # the thrown error message when json parsing fails.
4
+ module FlexCommerceApi
5
+ module JsonApiClientExtension
6
+ module ParseJson
7
+ def call(environment)
8
+ super
9
+ rescue JSON::ParserError => error
10
+ error.define_singleton_method(:raven_context) do
11
+ {
12
+ extra: {
13
+ body: environment.body
14
+ }
15
+ }
16
+ end
17
+ raise
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ JsonApiClient::Middleware::ParseJson.send :prepend, FlexCommerceApi::JsonApiClientExtension::ParseJson
@@ -0,0 +1,16 @@
1
+ require_relative "../included_data"
2
+
3
+ module FlexCommerceApi
4
+ module JsonApiClientExtension
5
+ module Parsers
6
+ class Parser < ::JsonApiClient::Parsers::Parser
7
+ class << self
8
+ def handle_included(result_set, data)
9
+ result_set.included = ::FlexCommerceApi::JsonApiClientExtension::IncludedData.new(result_set, data.fetch("included", []))
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ require "rack/utils"
2
+
3
+ # Adds preview param to all requests to Flex API.
4
+ module FlexCommerceApi
5
+ module JsonApiClientExtension
6
+ class PreviewedRequestMiddleware < ::Faraday::Middleware
7
+ def call(env)
8
+ env.url.tap do |url|
9
+ parsed_query = Rack::Utils.parse_nested_query url.query
10
+ parsed_query[:preview] = true
11
+ url.query = parsed_query.to_param
12
+ end
13
+ @app.call(env)
14
+ end
15
+ end
16
+ end
17
+ end