Class: IntersoftAPI::Client
- Inherits:
-
Object
- Object
- IntersoftAPI::Client
- Defined in:
- lib/intersoft_api/client.rb
Constant Summary collapse
- BASE_URL =
'https://api.intersoftsapient.net/v4/'
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
readonly
Returns the value of attribute client_secret.
-
#test ⇒ Object
readonly
Returns the value of attribute test.
Instance Method Summary collapse
- #connection ⇒ Object
- #handle_response(response) ⇒ Object
-
#initialize(client_id:, client_secret:, adapter: Faraday.default_adapter, test: false) ⇒ Client
constructor
A new instance of Client.
- #shipment ⇒ Object
- #shipping_account ⇒ Object
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_token ⇒ Object (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 |
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
7 8 9 |
# File 'lib/intersoft_api/client.rb', line 7 def adapter @adapter end |
#client_id ⇒ Object (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_secret ⇒ Object (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 |
#test ⇒ Object (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
#connection ⇒ Object
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) = response.body case response.status when 400 raise Error, "A bad request or a validation exception has occurred. #{}" when 401 raise Error, "Invalid authorization credentials. #{}" when 403 raise Error, "Connection doesn't have permission to access the resource. #{}" when 404 raise Error, "The resource you have specified cannot be found. #{}" when 429 raise Error, "The API rate limit for your application has been exceeded. #{}" when 500 raise Error, "An unhandled error with the server. Contact the Intersoft team if problems persist. #{}" when 503 raise Error, "API is currently unavailable – typically due to a scheduled outage – try again soon. #{}" end response end |
#shipment ⇒ Object
27 28 29 |
# File 'lib/intersoft_api/client.rb', line 27 def shipment ShipmentResource.new(self) end |
#shipping_account ⇒ Object
31 32 33 |
# File 'lib/intersoft_api/client.rb', line 31 def shipping_account ShippingAccountResource.new(self) end |