e_plat 0.5.5 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +109 -60
  3. data/lib/e_plat/client/default_request_args.rb +1 -1
  4. data/lib/e_plat/client.rb +55 -9
  5. data/lib/e_plat/initializer.rb +26 -0
  6. data/lib/e_plat/mapping/bigcommerce/v_3/product/option.rb +58 -0
  7. data/lib/e_plat/mapping/bigcommerce/v_3/product/variant/option_value.rb +42 -0
  8. data/lib/e_plat/mapping/shopify/v_2024_07/metafield.rb +26 -0
  9. data/lib/e_plat/mapping/shopify/v_2024_07/order/billing_address.rb +14 -0
  10. data/lib/e_plat/mapping/shopify/v_2024_07/order/shipping_address.rb +30 -0
  11. data/lib/e_plat/mapping/shopify/v_2024_07/order.rb +26 -0
  12. data/lib/e_plat/mapping/shopify/v_2024_07/product/image.rb +52 -0
  13. data/lib/e_plat/mapping/shopify/v_2024_07/product/option.rb +45 -0
  14. data/lib/e_plat/mapping/shopify/v_2024_07/product/variant/option_value.rb +58 -0
  15. data/lib/e_plat/mapping/shopify/v_2024_07/product/variant.rb +180 -0
  16. data/lib/e_plat/mapping/shopify/v_2024_07/product.rb +100 -0
  17. data/lib/e_plat/mapping/shopify/v_2024_07/script_tag.rb +26 -0
  18. data/lib/e_plat/mapping/shopify/v_2024_07/shop.rb +26 -0
  19. data/lib/e_plat/mapping/shopify/v_2024_07/webhook.rb +29 -0
  20. data/lib/e_plat/mapping/virtual_collection/shopify/product/variant/option_value.rb +272 -0
  21. data/lib/e_plat/mapping.rb +39 -15
  22. data/lib/e_plat/resource/base.rb +39 -5
  23. data/lib/e_plat/resource/collection.rb +45 -16
  24. data/lib/e_plat/resource/concerns/aliases.rb +39 -19
  25. data/lib/e_plat/resource/concerns/dirty.rb +12 -8
  26. data/lib/e_plat/resource/concerns/graph_q_lable.rb +253 -0
  27. data/lib/e_plat/resource/concerns/overwrite_instance_methods.rb +4 -4
  28. data/lib/e_plat/resource/concerns/overwrite_request_methods.rb +64 -21
  29. data/lib/e_plat/resource/countable.rb +37 -10
  30. data/lib/e_plat/resource/metafield.rb +2 -2
  31. data/lib/e_plat/resource/platform_specific/bigcommerce/metafield.rb +4 -0
  32. data/lib/e_plat/resource/platform_specific/bigcommerce/order/Consignment.rb +2 -0
  33. data/lib/e_plat/resource/platform_specific/bigcommerce/order/billing_address.rb +2 -0
  34. data/lib/e_plat/resource/platform_specific/bigcommerce/order/fulfillment.rb +2 -0
  35. data/lib/e_plat/resource/platform_specific/bigcommerce/order/line_item.rb +2 -0
  36. data/lib/e_plat/resource/platform_specific/bigcommerce/order/shipping_address.rb +2 -0
  37. data/lib/e_plat/resource/platform_specific/bigcommerce/order/shipping_line.rb +2 -0
  38. data/lib/e_plat/resource/platform_specific/bigcommerce/order.rb +4 -0
  39. data/lib/e_plat/resource/platform_specific/bigcommerce/product/image.rb +2 -0
  40. data/lib/e_plat/resource/platform_specific/bigcommerce/product/option.rb +2 -0
  41. data/lib/e_plat/resource/platform_specific/bigcommerce/product/variant/option_value.rb +2 -0
  42. data/lib/e_plat/resource/platform_specific/bigcommerce/product/variant.rb +2 -0
  43. data/lib/e_plat/resource/platform_specific/bigcommerce/product.rb +4 -0
  44. data/lib/e_plat/resource/platform_specific/bigcommerce/script_tag.rb +4 -0
  45. data/lib/e_plat/resource/platform_specific/bigcommerce/shop.rb +4 -0
  46. data/lib/e_plat/resource/platform_specific/bigcommerce/webhook.rb +4 -0
  47. data/lib/e_plat/resource/platform_specific/shopify/graph_q_l/v_2024_07/input/product/variant.rb +47 -0
  48. data/lib/e_plat/resource/platform_specific/shopify/graph_q_l/v_2024_07/input/product/variants_bulk.rb +47 -0
  49. data/lib/e_plat/resource/platform_specific/shopify/graph_q_l/v_2024_07/input/product.rb +23 -0
  50. data/lib/e_plat/resource/platform_specific/shopify/graph_q_l/v_2024_07/input.rb +98 -0
  51. data/lib/e_plat/resource/platform_specific/shopify/graph_q_l/v_2024_07/mutation/product/variant.rb +78 -0
  52. data/lib/e_plat/resource/platform_specific/shopify/graph_q_l/v_2024_07/mutation/product.rb +55 -0
  53. data/lib/e_plat/resource/platform_specific/shopify/graph_q_l/v_2024_07/mutation.rb +5 -0
  54. data/lib/e_plat/resource/platform_specific/shopify/graph_q_l/v_2024_07/query/product/variant.rb +44 -0
  55. data/lib/e_plat/resource/platform_specific/shopify/graph_q_l/v_2024_07/query/product.rb +58 -0
  56. data/lib/e_plat/resource/platform_specific/shopify/graph_q_l/v_2024_07/query.rb +5 -0
  57. data/lib/e_plat/resource/platform_specific/shopify/metafield.rb +4 -0
  58. data/lib/e_plat/resource/platform_specific/shopify/order/Consignment.rb +2 -0
  59. data/lib/e_plat/resource/platform_specific/shopify/order/billing_address.rb +2 -0
  60. data/lib/e_plat/resource/platform_specific/shopify/order/fulfillment.rb +2 -0
  61. data/lib/e_plat/resource/platform_specific/shopify/order/line_item.rb +2 -0
  62. data/lib/e_plat/resource/platform_specific/shopify/order/shipping_address.rb +2 -0
  63. data/lib/e_plat/resource/platform_specific/shopify/order/shipping_line.rb +2 -0
  64. data/lib/e_plat/resource/platform_specific/shopify/order.rb +4 -0
  65. data/lib/e_plat/resource/platform_specific/shopify/product/image.rb +14 -0
  66. data/lib/e_plat/resource/platform_specific/shopify/product/option.rb +14 -0
  67. data/lib/e_plat/resource/platform_specific/shopify/product/variant/option_value.rb +14 -0
  68. data/lib/e_plat/resource/platform_specific/shopify/product/variant.rb +66 -0
  69. data/lib/e_plat/resource/platform_specific/shopify/product.rb +79 -0
  70. data/lib/e_plat/resource/platform_specific/shopify/recurring_application_charge/usage_charge.rb +2 -0
  71. data/lib/e_plat/resource/platform_specific/shopify/recurring_application_charge.rb +4 -0
  72. data/lib/e_plat/resource/platform_specific/shopify/script_tag.rb +4 -0
  73. data/lib/e_plat/resource/platform_specific/shopify/shop.rb +4 -0
  74. data/lib/e_plat/resource/platform_specific/shopify/webhook.rb +4 -0
  75. data/lib/e_plat/resource/product/image.rb +8 -5
  76. data/lib/e_plat/resource/product/option.rb +2 -3
  77. data/lib/e_plat/resource/product/variant/option_value.rb +17 -0
  78. data/lib/e_plat/resource/product/variant.rb +6 -14
  79. data/lib/e_plat/resource/product.rb +23 -11
  80. data/lib/e_plat/session.rb +3 -2
  81. data/lib/e_plat/type_coercer.rb +4 -2
  82. data/lib/e_plat/version.rb +1 -1
  83. data/lib/e_plat.rb +33 -4
  84. data/lib/hash.rb +39 -0
  85. metadata +163 -27
@@ -9,15 +9,10 @@ module EPlat
9
9
 
10
10
  schema do
11
11
  integer :id
12
- integer :product_id
13
- integer :position
14
- datetime :created_at
15
- datetime :updated_at
16
12
  string :alt
17
13
  integer :width
18
14
  integer :height
19
15
  string :src
20
- array :variant_ids
21
16
  string :admin_graphql_api_id
22
17
  end
23
18
 
@@ -25,6 +20,14 @@ module EPlat
25
20
  prefix_options[:product] ||= product.id
26
21
  super
27
22
  end
23
+
24
+ def variant_ids
25
+ product.variants.map(&:id)
26
+ end
27
+
28
+ def variant_ids=(ids)
29
+ raise EPlat::Error.new("EPlat doesn't support setting variant_ids on images")
30
+ end
28
31
 
29
32
  end
30
33
  end
@@ -6,10 +6,9 @@ module EPlat
6
6
 
7
7
  schema do
8
8
  integer :id
9
- integer :product_id
10
- string :name
9
+ string :name
11
10
  integer :position
12
- array :values
11
+ array :values
13
12
  end
14
13
 
15
14
  end
@@ -0,0 +1,17 @@
1
+ module EPlat
2
+ class Product::Variant
3
+ class OptionValue < Base
4
+ belongs_to :variant, class_name: "EPlat::Product::Variant"
5
+
6
+ class << self; end
7
+
8
+ schema do
9
+ integer :id
10
+ string :admin_graphql_api_id
11
+ string :name
12
+ string :value
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -2,13 +2,12 @@ module EPlat
2
2
  class Product
3
3
  class Variant < Base
4
4
  belongs_to :product, class_name: "EPlat::Product"
5
-
6
- class << self
7
- end
5
+
6
+ class << self; end
8
7
 
9
8
  schema do
10
9
  integer :id
11
- integer :product_id
10
+ integer :product_id # nil-able as Shopify graphql doesn't use it
12
11
  string :title
13
12
  string :price
14
13
  string :sku
@@ -16,32 +15,25 @@ module EPlat
16
15
  string :inventory_policy
17
16
  string :compare_at_price
18
17
  string :fulfillment_service
19
- string :inventory_management
20
- string :option1
21
- string :option2
22
- string :option3
18
+ boolean :inventory_management
23
19
  datetime :created_at
24
20
  datetime :updated_at
25
21
  boolean :taxable
26
22
  string :barcode
27
23
  integer :grams
28
- integer :image_id
29
24
  float :weight
30
25
  string :weight_unit
31
26
  integer :inventory_item_id
32
27
  integer :inventory_quantity
33
- integer :old_inventory_quantity
34
- array :presentment_prices
35
28
  string :tax_code
36
29
  boolean :requires_shipping
37
30
  string :admin_graphql_api_id
38
31
  end
39
32
 
40
33
  def save
41
- prefix_options[:product] ||= product.id
34
+ prefix_options[:product] ||= try(:product)&.id
42
35
  super
43
36
  end
44
-
45
37
  end
46
38
  end
47
- end
39
+ end
@@ -2,16 +2,7 @@ module EPlat
2
2
  class Product < Base
3
3
  has_many :variants, class_name: "EPlat::Product::Variant"
4
4
  has_many :images, class_name: "EPlat::Product::Image"
5
-
6
- # graphql :shopify, :product do
7
- # variants do
8
- # edges { node as: :variant }
9
- # end
10
- # images do
11
- # edges { node as: :image }
12
- # end
13
- # end
14
-
5
+ has_many :options, class_name: "EPlat::Product::Option"
15
6
 
16
7
  include Concerns::Metafieldable
17
8
 
@@ -24,7 +15,6 @@ module EPlat
24
15
  array :options
25
16
  string :product_type
26
17
  datetime :published_at
27
- string :published_scope
28
18
  string :status
29
19
  string :tags
30
20
  string :admin_graphql_api_id
@@ -46,6 +36,28 @@ module EPlat
46
36
  def find_variant(id)
47
37
  variants.find { |v| v.id && v.id.to_i == id.to_i }
48
38
  end
39
+
40
+ def variants_collection
41
+ @variants_collection ||= EPlat::Product::Variant.find(:all, params: {product_id: id, limit: 200})
42
+ end
43
+
44
+ def load_all_variants # slow method, run async
45
+ return EPlat::Collection.new(variants) unless client.shopify? && client.uses_graphql_for_products?
46
+
47
+ @variants_collection = variants_collection
48
+ @saved_variants = @variants_collection.to_a
49
+ cycle = 0
50
+
51
+ while @variants_collection.next_page?
52
+ @variants_collection = @variants_collection.fetch_next_page
53
+ @saved_variants += @variants_collection.to_a
54
+
55
+ cycle += 1
56
+ break if cycle > 10
57
+ end
58
+
59
+ self.variants = EPlat::Collection.new(@saved_variants)
60
+ end
49
61
 
50
62
  end
51
63
  end
@@ -3,12 +3,13 @@ module EPlat
3
3
  class Session
4
4
  #the session is an instance of EPlat::Client made available via Rails EPlat::Current::Attributes
5
5
 
6
- def initialize(platform:, store_url:, api_token:, store_hash: nil)
6
+ def initialize(platform:, store_url:, api_token:, store_hash: nil, api_version: nil)
7
7
  EPlat::Current.e_plat_session = EPlat::Client.new(
8
8
  platform: platform,
9
9
  store_url: store_url,
10
10
  api_token: api_token,
11
- store_hash: store_hash
11
+ store_hash: store_hash,
12
+ api_version: api_version
12
13
  ).freeze
13
14
  end
14
15
 
@@ -13,7 +13,7 @@ module EPlat
13
13
  end
14
14
 
15
15
  def type_value!(key, value)
16
- type = type_schema[key]&.downcase
16
+ type = type_schema[key].try(:downcase)
17
17
 
18
18
  if type.nil?
19
19
  return value
@@ -33,7 +33,7 @@ module EPlat
33
33
  else
34
34
  value.send to_type_method(type)
35
35
  end
36
- rescue
36
+ rescue
37
37
  value
38
38
  end
39
39
 
@@ -48,6 +48,8 @@ module EPlat
48
48
  "to_h"
49
49
  when "string"
50
50
  "to_s"
51
+ when "float"
52
+ "to_f"
51
53
  when "datetime"
52
54
  "to_datetime"
53
55
  end
@@ -1,3 +1,3 @@
1
1
  module EPlat
2
- VERSION = "0.5.5"
2
+ VERSION = "0.7.0"
3
3
  end
data/lib/e_plat.rb CHANGED
@@ -7,33 +7,54 @@ require "activeresource"
7
7
  require "activeresource-response"
8
8
  require 'dry-types'
9
9
  require 'active_resource/connection_error'
10
+ require_relative "hash"
10
11
  # require "e_plat/railtie" if defined?(Rails::Railtie)
11
12
 
12
13
  Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
13
14
  loader = Zeitwerk::Loader.for_gem
14
15
  loader.collapse("#{__dir__}/e_plat/resource") # So can call EPlat::Product
15
16
  loader.collapse("#{__dir__}/e_plat/resource/shopify_only")
17
+ loader.collapse("#{__dir__}/e_plat/resource/platform_specific")
16
18
  loader.collapse("#{__dir__}/e_plat/errors")
17
- loader.setup # ready!
19
+ loader.setup
18
20
 
19
21
 
20
22
 
21
23
  module EPlat
22
24
  extend Dry::Configurable
23
- SUPPORTED_SHOPIFY_API_VERSIONS = ['2024_01']
25
+ SUPPORTED_SHOPIFY_API_VERSIONS = ['2024_01', '2024_07']
24
26
  SUPPORTED_BIGCOMMERCE_API_VERSIONS = ['3']
27
+
28
+ SUPPORTED_PLATFORMS = ["shopify", "bigcommerce"]
29
+ SUPPORTED_RESOURCES = [
30
+ "shop",
31
+ "product",
32
+ "product/variant",
33
+ "product/image",
34
+ "product/option",
35
+ "product/image",
36
+ "order",
37
+ "order/line_item",
38
+ "order/shipping_address",
39
+ "order/billing_address",
40
+ "script_tag",
41
+ "webhook",
42
+ "metafield"
43
+ ]
25
44
 
26
- setting(:shopify_api_version, default: "2024_01", constructor: ->(value) do
45
+ setting(:shopify_api_version, default: ENV['EPLAT_SHOPIFY_API_VERSION'] || "2024_07", constructor: ->(value) do
27
46
  SUPPORTED_SHOPIFY_API_VERSIONS.include?(value.underscore) ? value.underscore : raise(ArgumentError, "Shopify API version #{value} is not supported")
28
47
  end)
29
48
 
30
- setting(:bigcommerce_api_version, default: "v3", constructor: ->(value) do
49
+ setting(:bigcommerce_api_version, default: ENV['EPLAT_BIGCOMMERCE_API_VERSION'] || "v3", constructor: ->(value) do
31
50
  v = value.downcase.gsub("v", "")
32
51
  SUPPORTED_BIGCOMMERCE_API_VERSIONS.include?(v) ? v : raise(ArgumentError, "Bigcommerce API version #{value} is not supported")
33
52
  end)
34
53
 
35
54
  setting(:woocommerce_api_version, default: "v3", constructor: ->(value) { value.downcase.gsub("v", "") })
36
55
 
56
+ setting(:print_graphql_requests, default:ENV['EPLAT_PRINT_GRAPHQL_REQUESTS'] || false)
57
+
37
58
  def self.api_display_name
38
59
  apis = Struct.new(:shopify, :bigcommerce, :woocommerce)
39
60
  apis.new(
@@ -43,5 +64,13 @@ module EPlat
43
64
  )
44
65
  end
45
66
 
67
+ def self.shopify_graphql_version
68
+ # "V#{ EPlat.config.shopify_api_version.underscore }"
69
+ "V202407"
70
+ end
71
+
46
72
  class Error < StandardError; end
73
+ class GraphqlError < Error; end
47
74
  end
75
+
76
+ EPlat::Initializer.check_required_classes!
data/lib/hash.rb ADDED
@@ -0,0 +1,39 @@
1
+ class Hash
2
+ def to_graphql_args
3
+ map do |key, value|
4
+ formatted_value =
5
+ case value
6
+ when String
7
+ "\"#{value}\""
8
+ when Hash
9
+ value.to_graphql_args
10
+ else
11
+ value
12
+ end
13
+
14
+ "#{key}: #{formatted_value}"
15
+ end.join(", ")
16
+ end
17
+
18
+ def to_camelized_graphql_args
19
+ map do |key, value|
20
+ formatted_value =
21
+ case value
22
+ when String
23
+ "\"#{value}\""
24
+ when Hash
25
+ value.to_graphql_args
26
+ else
27
+ value
28
+ end
29
+
30
+ "#{key.to_s.camelize(:lower)}: #{formatted_value}"
31
+ end.join(", ")
32
+ end
33
+
34
+ def to_shopify_graphql_query_string
35
+ map do |key, value|
36
+ "#{key}:#{value.is_a?(String) ? "\'#{value}\'" : value}"
37
+ end.join(" ")
38
+ end
39
+ end