e_plat 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +339 -0
  4. data/Rakefile +8 -0
  5. data/app/assets/config/e_plat_manifest.js +0 -0
  6. data/config/routes.rb +2 -0
  7. data/lib/active_resource/schema.rb +59 -0
  8. data/lib/e_plat/client/default_request_args.rb +33 -0
  9. data/lib/e_plat/client/platform_convenience_methods.rb +21 -0
  10. data/lib/e_plat/client.rb +95 -0
  11. data/lib/e_plat/engine.rb +4 -0
  12. data/lib/e_plat/errors/incorrect_type_error.rb +8 -0
  13. data/lib/e_plat/errors/missing_migration_key_error.rb +9 -0
  14. data/lib/e_plat/errors/missing_session_error.rb +8 -0
  15. data/lib/e_plat/mapping/base.rb +49 -0
  16. data/lib/e_plat/mapping/bigcommerce/v_3/product/image.rb +55 -0
  17. data/lib/e_plat/mapping/bigcommerce/v_3/product/variant.rb +96 -0
  18. data/lib/e_plat/mapping/bigcommerce/v_3/product.rb +129 -0
  19. data/lib/e_plat/mapping/bigcommerce/v_3/shop.rb +103 -0
  20. data/lib/e_plat/mapping/shopify/v_2022_07/product/image.rb +32 -0
  21. data/lib/e_plat/mapping/shopify/v_2022_07/product/variant.rb +30 -0
  22. data/lib/e_plat/mapping/shopify/v_2022_07/product.rb +26 -0
  23. data/lib/e_plat/mapping/shopify/v_2022_07/shop.rb +26 -0
  24. data/lib/e_plat/mapping.rb +19 -0
  25. data/lib/e_plat/resource/attribute_interface.rb +60 -0
  26. data/lib/e_plat/resource/base.rb +145 -0
  27. data/lib/e_plat/resource/concerns/aliases.rb +100 -0
  28. data/lib/e_plat/resource/concerns/overwrite_instance_methods.rb +11 -0
  29. data/lib/e_plat/resource/concerns/overwrite_request_methods.rb +50 -0
  30. data/lib/e_plat/resource/order/customer.rb +37 -0
  31. data/lib/e_plat/resource/order/fulfillment.rb +27 -0
  32. data/lib/e_plat/resource/order/line_item.rb +37 -0
  33. data/lib/e_plat/resource/order/shipping_line.rb +24 -0
  34. data/lib/e_plat/resource/order.rb +103 -0
  35. data/lib/e_plat/resource/product/image.rb +25 -0
  36. data/lib/e_plat/resource/product/option.rb +16 -0
  37. data/lib/e_plat/resource/product/variant.rb +51 -0
  38. data/lib/e_plat/resource/product.rb +35 -0
  39. data/lib/e_plat/resource/shop.rb +79 -0
  40. data/lib/e_plat/session.rb +20 -0
  41. data/lib/e_plat/session_state.rb +25 -0
  42. data/lib/e_plat/type_coercer.rb +59 -0
  43. data/lib/e_plat/types.rb +15 -0
  44. data/lib/e_plat/version.rb +3 -0
  45. data/lib/e_plat.rb +29 -0
  46. data/lib/tasks/e_plat_tasks.rake +4 -0
  47. metadata +256 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: afc8b814dc437fb360828f3452ce591e91450961c21d38d3d96e423ebbd6a85a
4
+ data.tar.gz: 7b851774151f4e8c00ac4cb3df57be9460fb77d70f54fc5d506686aaa18034c7
5
+ SHA512:
6
+ metadata.gz: fd34b9a17e29ed2171428f889ee9ec88d8224b50438083b745d15ebf71f2311578a874e1aef051bd7cb2e5c6dca1ad6b929f77b8c161a29dd9642aa3794eb23b
7
+ data.tar.gz: a81bf741bef95ecf0b9296229ebea8bc0019a4416f1b718c88bdba14ac05848bfcc909398f4e05712527c249d5b67f6a13c1f011be8489a4808d2fe31eaa4a83
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2022 oliwoodsuk
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,339 @@
1
+ # EPlat
2
+
3
+ <strong>EPlat <small>(E-commerce Platform)</small> </br></strong>
4
+ A single interface for interacting with E-commerce platform APIs.
5
+
6
+ Goals are:
7
+ <ul>
8
+ <li>
9
+ Make it easy to use and request API resources from different platforms.
10
+ </li>
11
+ <li>
12
+ Offer a universal alias interface for interacting with common natured attributes. e.g. Shopify's product.body_html and BigCommerce's product.description.
13
+ </li>
14
+ <li>
15
+ Have a simple, easy-to-understand, lightweight project structure.
16
+ </li>
17
+ <li>
18
+ The e_plat alias interface mimics Shopify's API schema names and types.
19
+ </li>
20
+ </ul>
21
+
22
+ <p align="left">
23
+ <img src="mascot.png" width="350" title="hover text" alt="EPlat Platypus">
24
+ </p>
25
+
26
+
27
+ ## Installation
28
+
29
+ Install the gem and add to the application's Gemfile by executing:
30
+
31
+ $ gem "e_plat"
32
+
33
+ To your gemfile and then run
34
+
35
+ $ bundle
36
+
37
+
38
+ ## Config
39
+ You can configure which platform APIs are used by updating the EPlat config: <br/>
40
+ ```ruby
41
+ #initializers/e_plat.rb
42
+ EPlat.config.shopify_api_version = "2022-10"
43
+ EPlat.config.bigcommerce_api_version = "v3"
44
+ EPlat.config.woocommerce_api_version = "v3"
45
+ ```
46
+
47
+ ## Relasing new version
48
+ https://dev.to/doctolib/release-a-new-gem-version-je0
49
+
50
+
51
+ ## Usage
52
+
53
+ 1. Add `include EPlat::SessionState` to the top of your `ApplicationController` class.
54
+
55
+ 2. To make requests, first initialize a session like so:
56
+ ```ruby
57
+ EPlat::Session.new(
58
+ platform: :shopify,
59
+ store_url: "test-store.myshopify.com",
60
+ api_token: "123",
61
+ # store_hash: BigCommerce_specific
62
+ )
63
+ ```
64
+
65
+ 3. You can then make requests to the platform via a singleton call on the class:
66
+ ```ruby
67
+ EPlat::Product.find(id: 123)
68
+ ```
69
+
70
+ 4. Which will return an instance of:
71
+ ```ruby
72
+ EPlat::Product
73
+ ```
74
+
75
+
76
+ ## Under The Hood
77
+
78
+ <ol>
79
+ <li>
80
+ Request/Resources follow this format: <strong>EPlat::ResourceName</strong>. Requests are called on the class, which then return an instance.
81
+ </li>
82
+ <li>
83
+ EPlat resources will return their platform's native attributes, alongside a universal alias interface for viewing/editing via the EPlat schema.
84
+ </li>
85
+ <ul>
86
+ <li>
87
+ EPlat aliases can always be safely called, but aren't always successfully mapped to underlying platform attributes.
88
+ </li>
89
+ <li>
90
+ Whilst the EPlat alias interface and methods often mean you can treat resources the same regardless of platform, you can also ignore this interface and interact with the platform native attributes like regular active resource/record object.
91
+ </li>
92
+ <li>
93
+ The EPlat alias interface is just a set of dynamically created getter/setter/predicate methods with types that control the native attributes of the resource.
94
+ </li>
95
+ </ul>
96
+ </ol>
97
+
98
+
99
+
100
+
101
+ ## EPlat Alias Interface
102
+
103
+ <details>
104
+ <summary>Shop</summary>
105
+
106
+ ### Mapped Aliases
107
+
108
+ | Alas | Type | Shopify | BigCommerce | WooCommerce |
109
+ | ------------------------------------ | --------- | ------- | -----------| ------------|
110
+ | id | integer | * | * | nil |
111
+ | name | string | * | * | nil |
112
+ | email | string | * | * | nil |
113
+ | domain | string | * | * | nil |
114
+ | province | string | * | * | nil |
115
+ | country | string | * | * | nil |
116
+ | address1 | string | * | nil | nil |
117
+ | zip | string | * | nil | nil |
118
+ | city | string | * | nil | nil |
119
+ | source | string | * | nil | nil |
120
+ | phone | string | * | nil | nil |
121
+ | latitude | float | * | nil | nil |
122
+ | longitude | float | * | nil | nil |
123
+ | primary_locale | string | * | nil | nil |
124
+ | address2 | string | * | nil | nil |
125
+ | created_at | datetime | * | nil | nil |
126
+ | updated_at | datetime | * | nil | nil |
127
+ | country_code | string | * | * | nil |
128
+ | country_name | string | * | nil | nil |
129
+ | currency | string | * | * | nil |
130
+ | customer_email | string | * | nil | nil |
131
+ | timezone | string | * | nil | nil |
132
+ | iana_timezone | string | * | nil | nil |
133
+ | shop_owner | string | * | nil | nil |
134
+ | money_format | string | * | nil | nil |
135
+ | money_with_currency_format | string | * | nil | nil |
136
+ | weight_unit | string | * | * | nil |
137
+ | province_code | string | * | nil | nil |
138
+ | taxes_included | boolean | * | nil | nil |
139
+ | auto_configure_tax_inclusivity | boolean | * | nil | nil |
140
+ | tax_shipping | boolean | * | nil | nil |
141
+ | county_taxes | boolean | * | nil | nil |
142
+ | plan_display_name | string | * | nil | nil |
143
+ | plan_name | string | * | * | nil |
144
+ | has_discounts | boolean | * | nil | nil |
145
+ | has_gift_cards | boolean | * | nil | nil |
146
+ | myshopify_domain | string | * | nil | nil |
147
+ | google_apps_domain | string | * | nil | nil |
148
+ | google_apps_login_enabled | boolean | * | nil | nil |
149
+ | money_in_emails_format | string | * | nil | nil |
150
+ | money_with_currency_in_emails_format| string | * | nil | nil |
151
+ | eligible_for_payments | boolean | * | nil | nil |
152
+ | requires_extra_payments_agreement | boolean | * | nil | nil |
153
+ | password_enabled | boolean | * | nil | nil |
154
+ | has_storefront | boolean | * | nil | nil |
155
+ | eligible_for_card_reader_giveaway | boolean | * | nil | nil |
156
+ | finances | boolean | * | nil | nil |
157
+ | primary_location_id | integer | * | nil | nil |
158
+ | cookie_consent_level | string | * | nil | nil |
159
+ | visitor_tracking_consent_preference | string | * | nil | nil |
160
+ | checkout_api_supported | boolean | * | nil | nil |
161
+ | multi_location_enabled | boolean | * | nil | nil |
162
+ | setup_required | boolean | * | nil | nil |
163
+ | pre_launch_enabled | boolean | * | nil | nil |
164
+ | enabled_presentment_currencies | array | * | nil | nil |
165
+ | transactional_sms_disabled | boolean | * | nil | nil |
166
+ | marketing_sms_consent_enabled_at_checkout | boolean | * | nil | nil |
167
+
168
+ </details>
169
+ </br>
170
+ <details>
171
+ <summary>Product</summary>
172
+
173
+ ### Mapped Aliases
174
+
175
+ | Alas | Type | Shopify | BigCommerce | WooCommerce |
176
+ | ------------------------| -------- | ------- | -----------| ------------|
177
+ | body_html | string | * | * | nil |
178
+ | created_at | datetime | * | * | nil |
179
+ | handle | string | * | * | nil |
180
+ | id | integer | * | * | nil |
181
+ | images | array | * | * | nil |
182
+ | options | array | * | [] | nil |
183
+ | product_type | string | * | * | nil |
184
+ | published_at | datetime | * | nil | nil |
185
+ | published_scope | string | * | nil | nil |
186
+ | status | string | * | nil | nil |
187
+ | tags | string | * | nil | nil |
188
+ | admin_graphql_api_id | string | * | nil | nil |
189
+ | template_suffix | string | * | nil | nil |
190
+ | title | string | * | * | nil |
191
+ | updated_at | datetime | * | * | nil |
192
+ | variants | array | * | * | nil |
193
+ | vendor | string | * | nil | nil |
194
+
195
+ </details>
196
+ </br>
197
+ <details>
198
+ <summary>Product Variant</summary>
199
+
200
+ ### Mapped Aliases
201
+
202
+ | Alas | Type | Shopify | BigCommerce | WooCommerce |
203
+ | --------------------- | -------- | ------- | -----------| ------------|
204
+ | id | integer | * | * | nil |
205
+ | product_id | integer | * | * | nil |
206
+ | title | string | * | nil | nil |
207
+ | price | string | * | * | nil |
208
+ | sku | string | * | * | nil |
209
+ | position | integer | * | nil | nil |
210
+ | inventory_policy | string | * | nil | nil |
211
+ | compare_at_price | string | * | nil | nil |
212
+ | fulfillment_service | string | * | nil | nil |
213
+ | inventory_management | string | * | nil | nil |
214
+ | option1 | string | * | * | nil |
215
+ | option2 | string | * | nil | nil |
216
+ | option3 | string | * | nil | nil |
217
+ | created_at | datetime | * | * | nil |
218
+ | updated_at | datetime | * | * | nil |
219
+ | taxable | boolean | * | nil | nil |
220
+ | barcode | string | * | nil | nil |
221
+ | grams | integer | * | nil | nil |
222
+ | image_id | integer | * | nil | nil |
223
+ | weight | float | * | * | nil |
224
+ | weight_unit | string | * | nil | nil |
225
+ | inventory_item_id | integer | * | nil | nil |
226
+ | inventory_quantity | integer | * | * | nil |
227
+ | old_inventory_quantity| integer | * | nil | nil |
228
+ | presentment_prices | array | * | nil | nil |
229
+ | tax_code | string | * | nil | nil |
230
+ | requires_shipping | boolean | * | nil | nil |
231
+ | admin_graphql_api_id | string | * | nil | nil |
232
+
233
+ </details>
234
+ </br>
235
+ <details>
236
+ <summary>Product Option</summary>
237
+
238
+ ### Mapped Aliases
239
+
240
+ | Alas | Type | Shopify | BigCommerce | WooCommerce |
241
+ | --------- | -------- | ------- | -----------| ------------|
242
+ | id | integer | * | nil | nil |
243
+ | product_id| integer | * | nil | nil |
244
+ | name | string | * | nil | nil |
245
+ | position | integer | * | nil | nil |
246
+ | values | array | * | nil | nil |
247
+
248
+ </details>
249
+ </br>
250
+ <details>
251
+ <summary>Product Image</summary>
252
+
253
+ ### Mapped Aliases
254
+
255
+ | Alas | Type | Shopify | BigCommerce | WooCommerce |
256
+ | -------------------- | -------- | ------- | -----------| ------------|
257
+ | id | integer | * | * | nil |
258
+ | product_id | integer | * | * | nil |
259
+ | position | integer | * | nil | nil |
260
+ | created_at | datetime | * | * | nil |
261
+ | updated_at | datetime | * | * | nil |
262
+ | alt | string | * | nil | nil |
263
+ | width | integer | * | nil | nil |
264
+ | height | integer | * | nil | nil |
265
+ | src | string | * | * | nil |
266
+ | variant_ids | array | * | nil | nil |
267
+ | admin_graphql_api_id | string | * | nil | nil |
268
+
269
+ </details>
270
+
271
+ <br>
272
+
273
+ ## Todos
274
+
275
+ <ul>
276
+ <li>
277
+ Get BigCommerce request of shop working
278
+ </li>
279
+ <li>
280
+ find_by and where don't seem to work for big commerce
281
+ </li>
282
+ </ul>
283
+
284
+
285
+ ## Request Syntax
286
+
287
+ ```ruby
288
+ ### create new record
289
+ product = EPlat::Product.new(:title => 't-shirt')
290
+ product.save
291
+ # or...
292
+ EPlat::Product.create(:title => 't-shirt')
293
+ new_product = product.dup #duplicates entry without saving
294
+
295
+ ### delete record
296
+ EPlat::Product.delete(params[:id])
297
+ # or...
298
+ EPlat::Product.find(my_id).destroy
299
+
300
+ ### save record
301
+ product = EPlat::Product.new(:title => 't-shirt')
302
+ product.save
303
+
304
+ ### get records
305
+ EPlat::Product.find(1) # => GET /product/1.json (or EPlat::Product.first(args)) (or EPlat::Product.last(args))
306
+ EPlat::Product.find(:all) # => GET /people.json
307
+ EPlat::Product.find(:all, :params => { :title => "tshirt" }) # => GET /product.json?title=tshirt
308
+ EPlat::Product.find(:one, :from => :leader) # => GET /product/leader.json
309
+ EPlat::Product.find(:one, :from => "/product/1/specific_url.json") # => GET /product/1/specific_url.json
310
+
311
+
312
+ #### options
313
+ #prefix_options for nested URLs
314
+ EPlat::Product::Variant.collection_path(:variant_id => 5) # => /products/5/variants.json
315
+ #returns a EPlat::Product::Variant instance
316
+
317
+ # query_options to add to params ?x=y
318
+ EPlat::Product.find(:all, query_options: {test: "is it?"})
319
+ ```
320
+
321
+
322
+ ## Resource Syntax
323
+
324
+ ```ruby
325
+ #get clients shop
326
+ shop = EPlat::Shop.current
327
+
328
+ #to return all native platform attributes
329
+ EPlat::Product.new({}).attributes
330
+
331
+ #see all of the successfully mapped aliases for a resource
332
+ EPlat::Product.new({}).mapped_attributes.entries
333
+
334
+ #check if a platforms resource is mapped to a specific EPlat alias
335
+ EPlat::Product.new({}).mapped? "body_html"
336
+
337
+ ```
338
+
339
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
File without changes
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ Rails.application.routes.draw do
2
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveResource # :nodoc:
4
+ class Schema # :nodoc:
5
+ # attributes can be known to be one of these types. They are easy to
6
+ # cast to/from.
7
+ KNOWN_ATTRIBUTE_TYPES = %w( string text integer float decimal datetime timestamp time date binary boolean array hash )
8
+
9
+ # An array of attribute definitions, representing the attributes that
10
+ # have been defined.
11
+ attr_accessor :attrs
12
+
13
+ # The internals of an Active Resource Schema are very simple -
14
+ # unlike an Active Record TableDefinition (on which it is based).
15
+ # It provides a set of convenience methods for people to define their
16
+ # schema using the syntax:
17
+ # schema do
18
+ # string :foo
19
+ # integer :bar
20
+ # end
21
+ #
22
+ # The schema stores the name and type of each attribute. That is then
23
+ # read out by the schema method to populate the schema of the actual
24
+ # resource.
25
+ def initialize
26
+ @attrs = {}
27
+ end
28
+
29
+ def attribute(name, type, options = {})
30
+ raise ArgumentError, "Unknown Attribute type: #{type.inspect} for key: #{name.inspect}" unless type.nil? || Schema::KNOWN_ATTRIBUTE_TYPES.include?(type.to_s)
31
+
32
+ the_type = type.to_s
33
+ # TODO: add defaults
34
+ # the_attr = [type.to_s]
35
+ # the_attr << options[:default] if options.has_key? :default
36
+ @attrs[name.to_s] = the_type
37
+ self
38
+ end
39
+
40
+ # The following are the attribute types supported by Active Resource
41
+ # migrations.
42
+ KNOWN_ATTRIBUTE_TYPES.each do |attr_type|
43
+ # def string(*args)
44
+ # options = args.extract_options!
45
+ # attr_names = args
46
+ #
47
+ # attr_names.each { |name| attribute(name, 'string', options) }
48
+ # end
49
+ class_eval <<-EOV, __FILE__, __LINE__ + 1
50
+ # frozen_string_literal: true
51
+ def #{attr_type}(*args)
52
+ options = args.extract_options!
53
+ attr_names = args
54
+ attr_names.each { |name| attribute(name, '#{attr_type}', options) }
55
+ end
56
+ EOV
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,33 @@
1
+
2
+ module EPlat
3
+ class Client
4
+ module DefaultRequestArgs
5
+ extend ActiveSupport::Concern
6
+
7
+ def product_default_find_args
8
+ case platform
9
+ when :shopify
10
+ {}
11
+ when :bigcommerce
12
+ {
13
+ "include" => ["variants", "images"]
14
+ }
15
+ when :woocommerce
16
+ {}
17
+ end
18
+ end
19
+
20
+ def order_default_find_args
21
+ case platform
22
+ when :shopify
23
+ {}
24
+ when :bigcommerce
25
+ {}
26
+ when :woocommerce
27
+ {}
28
+ end
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,21 @@
1
+
2
+ module EPlat
3
+ class Client
4
+ module PlatformConvenienceMethods
5
+ extend ActiveSupport::Concern
6
+
7
+ def shopify?
8
+ platform == :shopify
9
+ end
10
+
11
+ def bigcommerce?
12
+ platform == :bigcommerce
13
+ end
14
+
15
+ def woocommerce?
16
+ platform == :woocommerce
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EPlat
4
+ class Client
5
+ include Client::PlatformConvenienceMethods, Client::DefaultRequestArgs
6
+
7
+ attr_accessor :platform, :store_url, :api_token, :api_version, :store_hash
8
+
9
+ def initialize(platform: nil, store_url: nil, api_token: nil, store_hash: nil)
10
+ @platform, @store_url, @api_token, @store_hash = platform&.to_sym, store_url, api_token, store_hash
11
+ get_api_version
12
+ end
13
+
14
+ def headers
15
+ platform_headers
16
+ end
17
+
18
+ def inspect
19
+ "#<#{self.class} platform=#{platform.inspect} store_url=#{store_url.inspect} api_version=#{api_version.inspect}>"
20
+ end
21
+
22
+ def inactive?
23
+ !valid?
24
+ end
25
+
26
+ def active?
27
+ valid?
28
+ end
29
+
30
+ def valid?
31
+ platform.present? && store_url.present? && api_token.present?
32
+ end
33
+
34
+ def clear!
35
+ self.platform = nil
36
+ end
37
+
38
+ def base_url
39
+ case platform
40
+ when :shopify
41
+ "https://#{ store_url }"
42
+ when :bigcommerce
43
+ "https://api.bigcommerce.com"
44
+ when :woocommerce
45
+ "/"
46
+ end
47
+ end
48
+
49
+ def url_prefix
50
+ case platform
51
+ when :shopify
52
+ "/admin/api/#{ api_version.dasherize }/"
53
+ when :bigcommerce
54
+ "/stores/#{ store_hash }/v#{ api_version }/catalog/"
55
+ when :woocommerce
56
+ "/"
57
+ end
58
+ end
59
+
60
+ def platform_headers
61
+ case platform
62
+ when :shopify
63
+ {
64
+ "Content-Type" => "application/json",
65
+ "X-Shopify-Access-Token" => api_token
66
+ }
67
+ when :bigcommerce
68
+ {
69
+ "Content-Type" => "application/json",
70
+ "X-Auth-Token" => api_token,
71
+ "host" => "api.bigcommerce.com"
72
+ }
73
+ when :woocommerce
74
+ {
75
+ "Content-Type" => "application/json",
76
+ "Authorization" => "Bearer #{ api_token }"
77
+ }
78
+ end
79
+ end
80
+
81
+ def get_api_version
82
+ self.api_version =
83
+ case platform
84
+ when :shopify
85
+ EPlat.config.shopify_api_version
86
+ when :bigcommerce
87
+ EPlat.config.bigcommerce_api_version
88
+ when :woocommerce
89
+ EPlat.config.woocommerce_api_version
90
+ end
91
+ end
92
+
93
+
94
+ end
95
+ end
@@ -0,0 +1,4 @@
1
+ module EPlat
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,8 @@
1
+
2
+ module EPlat
3
+ class IncorrectTypeError < StandardError
4
+ def initialize(msg="Impossible to create type from argument")
5
+ super
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+
2
+ module EPlat
3
+ class MissingMappingKeyError < StandardError
4
+ def initialize(msg="Missing hash key in the platform/e-plat's response. Check the mappings
5
+ ")
6
+ super
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+
2
+ module EPlat
3
+ class MissingSessionError < StandardError
4
+ def initialize(msg="Missing session state, did you include EPlat::SessionState in your ApplicationController?")
5
+ super
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,49 @@
1
+ module EPlat
2
+ class Mapping
3
+ class Base
4
+
5
+ def native_top_key
6
+ :itself # or top key of JSON response e.g. "data", "product",
7
+ end
8
+
9
+ def native_attributes
10
+ []
11
+ end
12
+
13
+ def native_attribute_aliases
14
+ [
15
+ # {
16
+ # alias_attribute: {native_key: "description", e_plat_key: "body_html"}
17
+ # },
18
+ # {
19
+ # alias_attribute_with_custom_getter: {native_key:, e_plat_key:, via: }
20
+ # }
21
+ ]
22
+ end
23
+
24
+ def aliases
25
+ native_attribute_aliases.map do |alias_hash|
26
+ next unless alias_hash[:alias_attribute]
27
+
28
+ keys = alias_hash[:alias_attribute].values[0..1]
29
+ Hash[*keys]
30
+ end.compact_blank.inject(&:merge) || {}
31
+ end
32
+
33
+ def to_native_keys(attributes)
34
+ as = aliases.invert
35
+ attributes.transform_keys{|key| (as[key]) ? as[key] : key }
36
+ end
37
+
38
+ def to_e_plat_keys(attributes)
39
+ as = aliases
40
+ attributes.transform_keys{|key| (as[key]) ? as[key] : key }
41
+ end
42
+
43
+ def mappable_keys
44
+ native_attribute_aliases.map{|a| a.values.first[:e_plat_key]}.presence || :all
45
+ end
46
+
47
+ end
48
+ end
49
+ end