Class: EcwidApi::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ecwid_api/client.rb

Overview

Public: Client objects manage the connection and interface to a single Ecwid store.

Examples

client = EcwidApi::Client.new(store_id: '12345', token: 'the access_token')
client.get "/products"

Constant Summary collapse

DEFAULT_URL =

The default base URL for the Ecwid API

"https://app.ecwid.com/api/v3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store_id, token, adapter = Faraday.default_adapter) ⇒ Client

Public: Initializes a new Client to interact with the API

store_id - the Ecwid store_id to interact with token - the authorization token provided by oAuth. See the

Authentication class


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ecwid_api/client.rb', line 31

def initialize(store_id, token, adapter = Faraday.default_adapter)
  @store_id, @token, @adapter = store_id, token, adapter

  @connection = Faraday.new store_url do |conn|
    conn.request  :oauth2, token, param_name: :token
    conn.request  :json

    conn.response :json, content_type: /\bjson$/
    conn.response :logger

    conn.adapter  adapter
  end

  @categories     = Api::Categories.new(self)
  @customers      = Api::Customers.new(self)
  @orders         = Api::Orders.new(self)
  @products       = Api::Products.new(self)
  @product_types  = Api::ProductTypes.new(self)
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



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

def adapter
  @adapter
end

#categoriesObject (readonly)

Returns the value of attribute categories.



23
24
25
# File 'lib/ecwid_api/client.rb', line 23

def categories
  @categories
end

#connectionObject (readonly)

Returns the value of attribute connection.



23
24
25
# File 'lib/ecwid_api/client.rb', line 23

def connection
  @connection
end

#customersObject (readonly)

Returns the value of attribute customers.



23
24
25
# File 'lib/ecwid_api/client.rb', line 23

def customers
  @customers
end

#ordersObject (readonly)

Returns the value of attribute orders.



23
24
25
# File 'lib/ecwid_api/client.rb', line 23

def orders
  @orders
end

#product_typesObject (readonly)

Returns the value of attribute product_types.



23
24
25
# File 'lib/ecwid_api/client.rb', line 23

def product_types
  @product_types
end

#productsObject (readonly)

Returns the value of attribute products.



23
24
25
# File 'lib/ecwid_api/client.rb', line 23

def products
  @products
end

#store_idObject (readonly)

Public: Returns the Ecwid Store ID



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

def store_id
  @store_id
end

#tokenObject (readonly)

Returns the value of attribute token.



20
21
22
# File 'lib/ecwid_api/client.rb', line 20

def token
  @token
end

Instance Method Details

#delete(*args, &block) ⇒ Object



66
67
68
# File 'lib/ecwid_api/client.rb', line 66

def delete(*args, &block)
  raise_on_failure connection.delete(*args, &block)
end

#post(*args, &block) ⇒ Object



58
59
60
# File 'lib/ecwid_api/client.rb', line 58

def post(*args, &block)
  raise_on_failure connection.post(*args, &block)
end

#post_image(url, filename) ⇒ Object

Public: A helper method for POSTing an image

url - the URL to POST the image to filename - the path or URL to the image to upload

Returns a Faraday::Response



77
78
79
80
81
# File 'lib/ecwid_api/client.rb', line 77

def post_image(url, filename)
  post(url) do |req|
    req.body = open(filename).read
  end
end

#put(*args, &block) ⇒ Object



62
63
64
# File 'lib/ecwid_api/client.rb', line 62

def put(*args, &block)
  raise_on_failure connection.put(*args, &block)
end

#store_urlObject

Public: The URL of the API for the Ecwid Store



52
53
54
# File 'lib/ecwid_api/client.rb', line 52

def store_url
  "#{DEFAULT_URL}/#{store_id}"
end