Class: NinjaVanApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ninja_van_api/client.rb

Constant Summary collapse

BASE_URL =
"https://api.ninjavan.co".freeze
SANDBOX_BASE_URL =
"https://api-sandbox.ninjavan.co".freeze
SUPPORTED_COUNTRY_CODES =
%w[SG MY TH ID VN PH MM]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, client_secret:, country_code: "SG", test_mode: false, conn_opts: {}) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
# File 'lib/ninja_van_api/client.rb', line 12

def initialize(client_id:, client_secret:, country_code: "SG", test_mode: false, conn_opts: {})
  @client_id = client_id
  @client_secret = client_secret
  @country_code = country_code
  @test_mode = test_mode
  @conn_opts = conn_opts

  validate_country_code
end

Instance Attribute Details

#country_codeObject (readonly)

Returns the value of attribute country_code.



10
11
12
# File 'lib/ninja_van_api/client.rb', line 10

def country_code
  @country_code
end

#test_modeObject (readonly)

Returns the value of attribute test_mode.



10
11
12
# File 'lib/ninja_van_api/client.rb', line 10

def test_mode
  @test_mode
end

Instance Method Details

#connectionObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ninja_van_api/client.rb', line 22

def connection
  @connection ||=
    Faraday.new do |conn|
      conn.url_prefix = url_prefix
      conn.options.merge!(@conn_opts)
      # access_token will be evaluated on each request using proc
      conn.request :authorization, :Bearer, -> { access_token }
      conn.request :json
      conn.response :json, content_type: "application/json"
    end
end

#ordersObject



34
35
36
# File 'lib/ninja_van_api/client.rb', line 34

def orders
  @orders ||= OrderResource.new(self)
end

#refresh_access_tokenObject



42
43
44
45
# File 'lib/ninja_van_api/client.rb', line 42

def refresh_access_token
  Rails.cache.delete(cache_key)
  fetch_access_token
end

#waybillsObject



38
39
40
# File 'lib/ninja_van_api/client.rb', line 38

def waybills
  @waybills ||= WaybillResource.new(self)
end