Class: Ecwid::Client

Inherits:
Object
  • Object
show all
Includes:
API::Order, API::Product, API::Profile
Defined in:
lib/ecwid/client.rb

Instance Method Summary collapse

Methods included from API::Product

#get_products

Methods included from API::Order

#get_orders

Methods included from API::Profile

#get_profile

Constructor Details

#initialize(store_id, token) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
# File 'lib/ecwid/client.rb', line 14

def initialize(store_id, token)
  @configuration = {
    store_id: store_id,
    token: token
  }
end

Instance Method Details

#delete(path, options = {}, headers = {}) ⇒ Object



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

def delete(path, options = {}, headers = {})
  request(:delete, path, options, headers)
end

#get(path, options = {}, headers = {}) ⇒ Object



29
30
31
# File 'lib/ecwid/client.rb', line 29

def get(path, options = {}, headers = {})
  request(:get, path, options, headers)
end

#post(path, options = {}, headers = {}) ⇒ Object



33
34
35
# File 'lib/ecwid/client.rb', line 33

def post(path, options = {}, headers = {})
  request(:post, path, options, headers)
end

#put(path, options = {}, headers = {}) ⇒ Object



37
38
39
# File 'lib/ecwid/client.rb', line 37

def put(path, options = {}, headers = {})
  request(:put, path, options, headers)
end

#request(method, path, options, headers = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ecwid/client.rb', line 45

def request(method, path, options, headers = {})
  client = RestClient::Resource.new "https://app.ecwid.com/api/v3/#{@configuration[:store_id]}#{path}"
  options.merge!(token: @configuration[:token])
  
  begin
    response = case method
               when :get then
                 client.get headers.merge({ params: options, content_type: :json })
               when :post then
                 client.post(options, headers.merge({ content_type: :json }))
               when :put then
                 client.put(options, headers.merge({ content_type: :json }))
               when :delete then
                 client.delete
               end
    
    Oj.load response if((200..201) === response.code)
  rescue RestClient::NotModified
    nil
  rescue RestClient::Forbidden => e
    raise Ecwid::HTTPUnauthorized.new 'Invalid Ecwid credentials'      
  end
end

#store_idObject



21
22
23
# File 'lib/ecwid/client.rb', line 21

def store_id
  @configuration[:store_id]
end

#tokenObject



25
26
27
# File 'lib/ecwid/client.rb', line 25

def token
  @configuration[:token]
end