Class: Desertcart::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_idObject (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_tokenObject (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_idObject (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_tokenObject (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_headersObject



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