Class: Ecwid::Client
- Inherits:
-
Object
- Object
- Ecwid::Client
- Includes:
- API::Order, API::Product, API::Profile
- Defined in:
- lib/ecwid/client.rb
Instance Method Summary collapse
- #delete(path, options = {}, headers = {}) ⇒ Object
- #get(path, options = {}, headers = {}) ⇒ Object
-
#initialize(store_id, token) ⇒ Client
constructor
A new instance of Client.
- #post(path, options = {}, headers = {}) ⇒ Object
- #put(path, options = {}, headers = {}) ⇒ Object
- #request(method, path, options, headers = {}) ⇒ Object
- #store_id ⇒ Object
- #token ⇒ Object
Methods included from API::Product
Methods included from API::Order
Methods included from API::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, = {}, headers = {}) request(:delete, path, , headers) end |
#get(path, options = {}, headers = {}) ⇒ Object
29 30 31 |
# File 'lib/ecwid/client.rb', line 29 def get(path, = {}, headers = {}) request(:get, path, , headers) end |
#post(path, options = {}, headers = {}) ⇒ Object
33 34 35 |
# File 'lib/ecwid/client.rb', line 33 def post(path, = {}, headers = {}) request(:post, path, , headers) end |
#put(path, options = {}, headers = {}) ⇒ Object
37 38 39 |
# File 'lib/ecwid/client.rb', line 37 def put(path, = {}, headers = {}) request(:put, path, , 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, , headers = {}) client = RestClient::Resource.new "https://app.ecwid.com/api/v3/#{@configuration[:store_id]}#{path}" .merge!(token: @configuration[:token]) begin response = case method when :get then client.get headers.merge({ params: , content_type: :json }) when :post then client.post(, headers.merge({ content_type: :json })) when :put then client.put(, 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_id ⇒ Object
21 22 23 |
# File 'lib/ecwid/client.rb', line 21 def store_id @configuration[:store_id] end |
#token ⇒ Object
25 26 27 |
# File 'lib/ecwid/client.rb', line 25 def token @configuration[:token] end |