class EcwidApi::Client

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"

Constants

DEFAULT_URL

The default base URL for the Ecwid API

Attributes

adapter[R]
categories[R]
connection[R]
customers[R]
orders[R]
product_types[R]
products[R]
store_id[R]

Public: Returns the Ecwid Store ID

token[R]

Public Class Methods

new(store_id, token, adapter = Faraday.default_adapter) click to toggle source

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

Public Instance Methods

delete(*args, &block) click to toggle source
# File lib/ecwid_api/client.rb, line 66
def delete(*args, &block)
  raise_on_failure connection.delete(*args, &block)
end
post(*args, &block) click to toggle source
# File lib/ecwid_api/client.rb, line 58
def post(*args, &block)
  raise_on_failure connection.post(*args, &block)
end
post_image(url, filename) click to toggle source

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

# 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) click to toggle source
# File lib/ecwid_api/client.rb, line 62
def put(*args, &block)
  raise_on_failure connection.put(*args, &block)
end
store_url() click to toggle source

Public: The URL of the API for the Ecwid Store

# File lib/ecwid_api/client.rb, line 52
def store_url
  "#{DEFAULT_URL}/#{store_id}"
end

Private Instance Methods

raise_on_failure(response) click to toggle source

Private: Raises a ResponseError if the request failed

response - a Faraday::Response object that is the result of a request

Raises ResponseError if the request wasn't successful

Returns the original response if the request was successful

# File lib/ecwid_api/client.rb, line 94
def raise_on_failure(response)
  if response.success?
    response
  else
    raise ResponseError.new(response)
  end
end