Class: IntersoftAPI::Client

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

Constant Summary collapse

BASE_URL =
'https://api.intersoftsapient.net/v4/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, client_secret:, adapter: Faraday.default_adapter, test: false) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
# File 'lib/intersoft_api/client.rb', line 9

def initialize(client_id:, client_secret:, adapter: Faraday.default_adapter, test: false)
  @client_id = client_id
  @client_secret = client_secret
  @adapter = adapter
  @test = test
  fetch_access_token
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



7
8
9
# File 'lib/intersoft_api/client.rb', line 7

def access_token
  @access_token
end

#adapterObject (readonly)

Returns the value of attribute adapter.



7
8
9
# File 'lib/intersoft_api/client.rb', line 7

def adapter
  @adapter
end

#client_idObject (readonly)

Returns the value of attribute client_id.



7
8
9
# File 'lib/intersoft_api/client.rb', line 7

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



7
8
9
# File 'lib/intersoft_api/client.rb', line 7

def client_secret
  @client_secret
end

#testObject (readonly)

Returns the value of attribute test.



7
8
9
# File 'lib/intersoft_api/client.rb', line 7

def test
  @test
end

Instance Method Details

#connectionObject



17
18
19
20
21
22
23
24
25
# File 'lib/intersoft_api/client.rb', line 17

def connection
  @connection ||= Faraday.new do |conn|
    conn.url_prefix = BASE_URL
    conn.headers['Authorization'] = "Bearer #{access_token}"
    conn.request :json
    conn.response :json, content_type: "application/json"
    conn.adapter adapter
  end
end

#handle_response(response) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/intersoft_api/client.rb', line 35

def handle_response(response)
  error_message = response.body

  case response.status
  when 400
    raise Error, "A bad request or a validation exception has occurred. #{error_message}"
  when 401
    raise Error, "Invalid authorization credentials. #{error_message}"
  when 403
    raise Error, "Connection doesn't have permission to access the resource. #{error_message}"
  when 404
    raise Error, "The resource you have specified cannot be found. #{error_message}"
  when 429
    raise Error, "The API rate limit for your application has been exceeded. #{error_message}"
  when 500
    raise Error,
          "An unhandled error with the server. Contact the Intersoft team if problems persist. #{error_message}"
  when 503
    raise Error,
          "API is currently unavailable – typically due to a scheduled outage – try again soon. #{error_message}"
  end

  response
end

#shipmentObject



27
28
29
# File 'lib/intersoft_api/client.rb', line 27

def shipment
  ShipmentResource.new(self)
end

#shipping_accountObject



31
32
33
# File 'lib/intersoft_api/client.rb', line 31

def 
  ShippingAccountResource.new(self)
end