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"

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.



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

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

Returns the value of attribute api_version.



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

def api_version
  @api_version
end

#platformObject (readonly)

Returns the value of attribute platform.



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

def platform
  @platform
end

#store_hashObject (readonly)

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

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_urlObject



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

Returns:

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



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

#headersObject



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

def headers
	platform_headers
end

#include_format_in_path?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/e_plat/client.rb', line 163

def include_format_in_path?
	!bigcommerce?
end

#inspectObject



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

#pagination_param(direction = :forward) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/e_plat/client.rb', line 150

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

#platform_headersObject



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 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" => "Bearer #{ 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



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_versionObject



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)
	# 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
		"/" # not yet supported
	end
end

#uses_graphql_for_products?Boolean

Returns:

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

Returns:

  • (Boolean)


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

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