Class: Desertcart::Client
- Inherits:
-
Object
- Object
- Desertcart::Client
- Includes:
- LedgerSync::Ledgers::Client::Mixin
- Defined in:
- lib/desertcart/client.rb
Constant Summary collapse
- ROOT_URI =
'https://api.desertcart.com/api'
- REQUEST_HEADERS =
{ 'Accept' => 'application/vnd.api+json; version:3.0', 'Content-Type' => 'application/json' }.freeze
Instance Attribute Summary collapse
-
#store_id ⇒ Object
readonly
Returns the value of attribute store_id.
-
#store_token ⇒ Object
readonly
Returns the value of attribute store_token.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
-
#user_token ⇒ Object
readonly
Returns the value of attribute user_token.
Instance Method Summary collapse
- #auth_headers ⇒ Object
- #create(**keywords) ⇒ Object
-
#initialize(args = {}) ⇒ Client
constructor
A new instance of Client.
- #read(**keywords) ⇒ Object
- #request(args = {}) ⇒ Object
- #update(**keywords) ⇒ Object
- #url_from_path(path:) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 |
# File 'lib/desertcart/client.rb', line 17 def initialize(args = {}) @user_id = args[:user_id] @user_token = args[:user_token] @store_id = args[:store_id] @store_token = args[:store_token] end |
Instance Attribute Details
#store_id ⇒ Object (readonly)
Returns the value of attribute store_id.
12 13 14 |
# File 'lib/desertcart/client.rb', line 12 def store_id @store_id end |
#store_token ⇒ Object (readonly)
Returns the value of attribute store_token.
12 13 14 |
# File 'lib/desertcart/client.rb', line 12 def store_token @store_token end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
12 13 14 |
# File 'lib/desertcart/client.rb', line 12 def user_id @user_id end |
#user_token ⇒ Object (readonly)
Returns the value of attribute user_token.
12 13 14 |
# File 'lib/desertcart/client.rb', line 12 def user_token @user_token end |
Instance Method Details
#auth_headers ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/desertcart/client.rb', line 36 def auth_headers { 'X-User-Id' => @user_id, 'X-User-Token' => @user_token, 'X-Store-Id' => @store_id, 'X-Store-Token' => @store_token } end |
#create(**keywords) ⇒ Object
28 29 30 |
# File 'lib/desertcart/client.rb', line 28 def create(**keywords) request(keywords.merge(method: :post)) end |
#read(**keywords) ⇒ Object
24 25 26 |
# File 'lib/desertcart/client.rb', line 24 def read(**keywords) request(keywords.merge(method: :get)) end |
#request(args = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/desertcart/client.rb', line 45 def request(args = {}) request = LedgerSync::Ledgers::Request.new( body: args.fetch(:body, nil), headers: args.fetch(:headers, {}) .merge(REQUEST_HEADERS) .merge(auth_headers), method: args.fetch(:method), url: url_from_path(path: args.fetch(:path)), params: args.fetch(:params, {}) ) request.perform end |
#update(**keywords) ⇒ Object
32 33 34 |
# File 'lib/desertcart/client.rb', line 32 def update(**keywords) request(keywords.merge(method: :put)) end |
#url_from_path(path:) ⇒ Object
59 60 61 62 63 |
# File 'lib/desertcart/client.rb', line 59 def url_from_path(path:) request_url = ROOT_URI request_url += '/' unless path.to_s.start_with?('/') request_url + path.to_s end |