Class: EPlat::Client

Inherits:
Object
  • Object
show all
Includes:
DefaultRequestArgs, PlatformConvenienceMethods
Defined in:
lib/e_plat/client.rb,
lib/e_plat/client/default_request_args.rb,
lib/e_plat/client/platform_convenience_methods.rb

Defined Under Namespace

Modules: DefaultRequestArgs, PlatformConvenienceMethods

Constant Summary collapse

SHOPIFY_REST_PAGINATION_PARAM =
"page_info"
SHOPIFY_GRAPHQL_FORWARD_PAGINATION_PARAM =
"after"
SHOPIFY_GRAPHQL_BACKWARD_PAGINATION_PARAM =
"before"
BIGCOMMERCE_PAGINATION_PARAM =
"page"
WOOCOMMERCE_PAGINATION_PARAM =
"page"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PlatformConvenienceMethods

#bigcommerce?, #shopify?, #woocommerce?

Methods included from DefaultRequestArgs

#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_tokenObject (readonly)

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_versionObject (readonly)

Returns the value of attribute api_version.



13
14
15
# File 'lib/e_plat/client.rb', line 13

def api_version
  @api_version
end

#platformObject (readonly)

Returns the value of attribute platform.



13
14
15
# File 'lib/e_plat/client.rb', line 13

def platform
  @platform
end

#store_hashObject (readonly)

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_urlObject (readonly)

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_urlObject



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

Returns:

  • (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 # haven't tested this is the case yet
		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_urlObject



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

#headersObject



19
20
21
# File 'lib/e_plat/client.rb', line 19

def headers
	platform_headers
end

#include_format_in_path?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/e_plat/client.rb', line 167

def include_format_in_path?
	!bigcommerce? && !woocommerce?
end

#inspectObject



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

#pagination_param(direction = :forward) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/e_plat/client.rb', line 152

def pagination_param(direction=:forward)
	case platform
	when :shopify
		if uses_graphql_for_products? 
			direction == :backward ? SHOPIFY_GRAPHQL_BACKWARD_PAGINATION_PARAM : SHOPIFY_GRAPHQL_FORWARD_PAGINATION_PARAM
		else 
			SHOPIFY_REST_PAGINATION_PARAM
		end
	when :bigcommerce 
		BIGCOMMERCE_PAGINATION_PARAM
	when :woocommerce
		WOOCOMMERCE_PAGINATION_PARAM
	end
end

#platform_headersObject



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 platform_headers
	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

#platform_klass(klass) ⇒ Object

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_versionObject



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)
	# for nested resources like "/products/:product/variants/:id"
	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 }" # not yet supported
	end
end

#uses_graphql_for_products?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


42
43
44
# File 'lib/e_plat/client.rb', line 42

def valid?
	platform.present? && store_url.present? && api_token.present? 
end