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
# 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)
  @orders     = Api::Orders.new(self)
  @products   = Api::Products.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

#ordersObject (readonly)

Returns the value of attribute orders


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

def orders
  @orders
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


64
65
66
# File 'lib/ecwid_api/client.rb', line 64

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

#post(*args, &block) ⇒ Object


56
57
58
# File 'lib/ecwid_api/client.rb', line 56

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


75
76
77
78
79
# File 'lib/ecwid_api/client.rb', line 75

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

#put(*args, &block) ⇒ Object


60
61
62
# File 'lib/ecwid_api/client.rb', line 60

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

#store_urlObject

Public: The URL of the API for the Ecwid Store


50
51
52
# File 'lib/ecwid_api/client.rb', line 50

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