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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PlatformConvenienceMethods

#bigcommerce?, #shopify?, #woocommerce?

Methods included from DefaultRequestArgs

#order_default_find_args, #product_default_find_args

Constructor Details

#initialize(platform: nil, store_url: nil, api_token: nil, store_hash: nil) ⇒ Client

Returns a new instance of Client.



9
10
11
12
# File 'lib/e_plat/client.rb', line 9

def initialize(platform: nil, store_url: nil, api_token: nil, store_hash: nil)      
  @platform, @store_url, @api_token, @store_hash = platform&.to_sym, store_url, api_token, store_hash
  get_api_version
end

Instance Attribute Details

#api_tokenObject

Returns the value of attribute api_token.



7
8
9
# File 'lib/e_plat/client.rb', line 7

def api_token
  @api_token
end

#api_versionObject

Returns the value of attribute api_version.



7
8
9
# File 'lib/e_plat/client.rb', line 7

def api_version
  @api_version
end

#platformObject

Returns the value of attribute platform.



7
8
9
# File 'lib/e_plat/client.rb', line 7

def platform
  @platform
end

#store_hashObject

Returns the value of attribute store_hash.



7
8
9
# File 'lib/e_plat/client.rb', line 7

def store_hash
  @store_hash
end

#store_urlObject

Returns the value of attribute store_url.



7
8
9
# File 'lib/e_plat/client.rb', line 7

def store_url
  @store_url
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/e_plat/client.rb', line 26

def active?
  valid?
end

#base_urlObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/e_plat/client.rb', line 38

def base_url 
  case platform
  when :shopify
    "https://#{ store_url }"
  when :bigcommerce
    "https://api.bigcommerce.com"
  when :woocommerce
    "/"
  end
end

#clear!Object



34
35
36
# File 'lib/e_plat/client.rb', line 34

def clear!
  self.platform = nil
end

#get_api_versionObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/e_plat/client.rb', line 81

def get_api_version
  self.api_version = 
    case platform
    when :shopify
      EPlat.config.shopify_api_version
    when :bigcommerce
      EPlat.config.bigcommerce_api_version
    when :woocommerce
      EPlat.config.woocommerce_api_version
    end
end

#headersObject



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

def headers
  platform_headers
end

#inactive?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/e_plat/client.rb', line 22

def inactive?
  !valid?
end

#inspectObject



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

def inspect
  "#<#{self.class} platform=#{platform.inspect} store_url=#{store_url.inspect} api_version=#{api_version.inspect}>"
end

#platform_headersObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/e_plat/client.rb', line 60

def platform_headers
  case platform
  when :shopify
    {
      "Content-Type" => "application/json",
      "X-Shopify-Access-Token" => api_token
    }
  when :bigcommerce
    {
      "Content-Type" => "application/json",
      "X-Auth-Token" => api_token,
      "host" => "api.bigcommerce.com"
    }
  when :woocommerce
    {
      "Content-Type" => "application/json",
      "Authorization" => "Bearer #{ api_token }"
    }
  end
end

#url_prefixObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/e_plat/client.rb', line 49

def url_prefix
  case platform
  when :shopify
    "/admin/api/#{ api_version.dasherize }/"
  when :bigcommerce
    "/stores/#{ store_hash }/v#{ api_version }/catalog/"
  when :woocommerce
    "/"
  end
end

#valid?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/e_plat/client.rb', line 30

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