Class: EPlat::Client
Defined Under Namespace
Modules: DefaultRequestArgs, PlatformConvenienceMethods
Constant Summary
collapse
"page_info"
"after"
"before"
"page"
Instance Attribute Summary collapse
Instance Method Summary
collapse
#bigcommerce?, #shopify?, #woocommerce?
#metafield_default_request_args, #order_default_request_args, #product_default_request_args, #variant_default_request_args
Constructor Details
#initialize(platform: nil, store_url: nil, api_token: nil, store_hash: nil, api_version: nil) ⇒ Client
Returns a new instance of Client.
14
15
16
|
# File 'lib/e_plat/client.rb', line 14
def initialize(platform: nil, store_url: nil, api_token: nil, store_hash: nil, api_version: nil)
@platform, @store_url, @api_token, @store_hash, @api_version = platform&.to_sym, store_url, api_token, store_hash, api_version
end
|
Instance Attribute Details
#api_token ⇒ Object
Returns the value of attribute api_token.
12
13
14
|
# File 'lib/e_plat/client.rb', line 12
def api_token
@api_token
end
|
#api_version ⇒ Object
Returns the value of attribute api_version.
12
13
14
|
# File 'lib/e_plat/client.rb', line 12
def api_version
@api_version
end
|
Returns the value of attribute platform.
12
13
14
|
# File 'lib/e_plat/client.rb', line 12
def platform
@platform
end
|
#store_hash ⇒ Object
Returns the value of attribute store_hash.
12
13
14
|
# File 'lib/e_plat/client.rb', line 12
def store_hash
@store_hash
end
|
#store_url ⇒ Object
Returns the value of attribute store_url.
12
13
14
|
# File 'lib/e_plat/client.rb', line 12
def store_url
@store_url
end
|
Instance Method Details
#base_url ⇒ Object
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/e_plat/client.rb', line 49
def base_url
case platform
when :shopify
"https://#{ store_url }"
when :bigcommerce
"https://api.bigcommerce.com"
when :woocommerce
"/"
end
end
|
#can_update_nested_resources? ⇒ Boolean
142
143
144
145
146
147
148
|
# File 'lib/e_plat/client.rb', line 142
def can_update_nested_resources?
case platform
when :shopify then true
when :bigcommerce then false
else false
end
end
|
#clear! ⇒ Object
45
46
47
|
# File 'lib/e_plat/client.rb', line 45
def clear!
self.platform = nil
end
|
#graphql_request_url ⇒ Object
90
91
92
93
94
95
96
97
|
# File 'lib/e_plat/client.rb', line 90
def graphql_request_url
case platform
when :shopify
"https://#{ store_url }/admin/api/#{ api_version.dasherize }/graphql.json"
else
raise EPlat::Error, "GraphQL is not supported for #{ platform }"
end
end
|
18
19
20
|
# File 'lib/e_plat/client.rb', line 18
def
end
|
163
164
165
|
# File 'lib/e_plat/client.rb', line 163
def include_format_in_path?
!bigcommerce?
end
|
#inspect ⇒ Object
37
38
39
|
# File 'lib/e_plat/client.rb', line 37
def inspect
"#<#{self.class} platform=#{platform.inspect} store_url=#{store_url.inspect} api_version=#{api_version.inspect}>"
end
|
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/e_plat/client.rb', line 103
def
case platform
when :shopify
{
"Content-Type" => "application/json",
"X-Shopify-Access-Token" => api_token
}
when :bigcommerce
{
"Content-Type" => "application/json",
"Accept" => "application/json",
"X-Auth-Token" => api_token,
"host" => "api.bigcommerce.com"
}
when :woocommerce
{
"Content-Type" => "application/json",
"Authorization" => "Bearer #{ api_token }"
}
end
end
|
Dynamically determines the class based on the platform needs to return nil if there isn’t a more specific class than the klass passed in
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/e_plat/client.rb', line 24
def platform_klass(klass)
return if klass.to_s.include? "EPlat::#{platform.capitalize}"
platform_specific_class_name = klass.to_s.gsub("EPlat::", "EPlat::#{platform.capitalize}::")
platform_specific_class = platform_specific_class_name.safe_constantize
if platform_specific_class_name != klass.to_s && platform_specific_class
platform_specific_class
else
nil
end
end
|
#shopify_graphql_version ⇒ Object
138
139
140
|
# File 'lib/e_plat/client.rb', line 138
def shopify_graphql_version
"V202407"
end
|
#url_prefix(klass: nil) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/e_plat/client.rb', line 60
def url_prefix(klass: nil)
klass_without_platform_scoping = nil
EPlat::SUPPORTED_PLATFORMS.each do |platform|
klass_without_platform_scoping = klass.to_s.gsub!("#{ platform.capitalize }::", "") if klass.to_s.include?("#{ platform.capitalize }::")
end
klass = klass_without_platform_scoping&.safe_constantize || klass
collection_path =
if klass.to_s.scan("::").length > 1
parent_klass = klass.to_s.split("::")[1].underscore
"#{ parent_klass.pluralize }/:#{ parent_klass }/"
else
nil
end
return "/stores/#{ store_hash }/v2/#{ collection_path }" if bigcommerce? && klass == EPlat::Order
return "/stores/#{ store_hash }/v3/content/" if bigcommerce? && klass == EPlat::ScriptTag
return "/stores/#{ store_hash }/v3/" if bigcommerce? && klass == EPlat::Webhook
case platform
when :shopify
"/admin/api/#{ api_version.dasherize }/#{ collection_path }"
when :bigcommerce
"/stores/#{ store_hash }/v#{ api_version }/catalog/#{ collection_path }"
when :woocommerce
"/" end
end
|
#uses_graphql_for_products? ⇒ Boolean
99
100
101
|
# File 'lib/e_plat/client.rb', line 99
def uses_graphql_for_products?
platform == :shopify && api_version != "2024_01"
end
|
#valid? ⇒ Boolean
41
42
43
|
# File 'lib/e_plat/client.rb', line 41
def valid?
platform.present? && store_url.present? && api_token.present?
end
|