Class: EPlat::Client
Defined Under Namespace
Modules: DefaultRequestArgs, PlatformConvenienceMethods
Constant Summary
collapse
"page_info"
"after"
"before"
"page"
"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.
15
16
17
|
# File 'lib/e_plat/client.rb', line 15
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.
13
14
15
|
# File 'lib/e_plat/client.rb', line 13
def api_token
@api_token
end
|
#api_version ⇒ Object
Returns the value of attribute api_version.
13
14
15
|
# File 'lib/e_plat/client.rb', line 13
def api_version
@api_version
end
|
Returns the value of attribute platform.
13
14
15
|
# File 'lib/e_plat/client.rb', line 13
def platform
@platform
end
|
#store_hash ⇒ Object
Returns the value of attribute store_hash.
13
14
15
|
# File 'lib/e_plat/client.rb', line 13
def store_hash
@store_hash
end
|
#store_url ⇒ Object
Returns the value of attribute store_url.
13
14
15
|
# File 'lib/e_plat/client.rb', line 13
def store_url
@store_url
end
|
Instance Method Details
#base_url ⇒ Object
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/e_plat/client.rb', line 50
def base_url
case platform
when :shopify
"https://#{ store_url }"
when :bigcommerce
"https://api.bigcommerce.com"
when :woocommerce
"https://#{ store_url }"
end
end
|
#can_update_nested_resources? ⇒ Boolean
143
144
145
146
147
148
149
150
|
# File 'lib/e_plat/client.rb', line 143
def can_update_nested_resources?
case platform
when :shopify then true
when :bigcommerce then false
when :woocommerce then false else false
end
end
|
#clear! ⇒ Object
46
47
48
|
# File 'lib/e_plat/client.rb', line 46
def clear!
self.platform = nil
end
|
#graphql_request_url ⇒ Object
91
92
93
94
95
96
97
98
|
# File 'lib/e_plat/client.rb', line 91
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
|
19
20
21
|
# File 'lib/e_plat/client.rb', line 19
def
end
|
167
168
169
|
# File 'lib/e_plat/client.rb', line 167
def include_format_in_path?
!bigcommerce? && !woocommerce?
end
|
#inspect ⇒ Object
38
39
40
|
# File 'lib/e_plat/client.rb', line 38
def inspect
"#<#{self.class} platform=#{platform.inspect} store_url=#{store_url.inspect} api_version=#{api_version.inspect}>"
end
|
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/e_plat/client.rb', line 104
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" => "Basic #{ 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
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/e_plat/client.rb', line 25
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
139
140
141
|
# File 'lib/e_plat/client.rb', line 139
def shopify_graphql_version
"V202407"
end
|
#url_prefix(klass: nil) ⇒ Object
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
89
|
# File 'lib/e_plat/client.rb', line 61
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
"/wp-json/wc/v#{ api_version }/#{ collection_path }" end
end
|
#uses_graphql_for_products? ⇒ Boolean
100
101
102
|
# File 'lib/e_plat/client.rb', line 100
def uses_graphql_for_products?
platform == :shopify && api_version != "2024_01"
end
|
#valid? ⇒ Boolean
42
43
44
|
# File 'lib/e_plat/client.rb', line 42
def valid?
platform.present? && store_url.present? && api_token.present?
end
|