Class: Unit::Connection
- Inherits:
-
Object
- Object
- Unit::Connection
- Defined in:
- lib/unit-ruby/util/connection.rb
Class Attribute Summary collapse
-
.api_key ⇒ Object
Returns the value of attribute api_key.
-
.base_url ⇒ Object
Returns the value of attribute base_url.
-
.trust_token ⇒ Object
Returns the value of attribute trust_token.
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
- #from_json_api(response_body) ⇒ Object
-
#get(path, params = nil) ⇒ Object
Executes a GET request to the API.
- #handle_errors(response) ⇒ Object
-
#initialize ⇒ Connection
constructor
A new instance of Connection.
-
#patch(path, data = nil) ⇒ Object
Executes a PATCH request to the API.
-
#post(path, data = nil) ⇒ Unit::APIResource
Executes a POST request to the API.
Constructor Details
#initialize ⇒ Connection
Returns a new instance of Connection.
13 14 15 16 17 18 19 20 21 |
# File 'lib/unit-ruby/util/connection.rb', line 13 def initialize @connection = Faraday.new(self.class.base_url) do |f| f.headers['UNIT_TRUST_TOKEN'] = self.class.trust_token if self.class.trust_token f.headers['Authorization'] = "Bearer #{self.class.api_key}" f.request :json # encode req bodies as JSON f.request :retry # retry transient failures f.response :json # decode response bodies as JSON end end |
Class Attribute Details
.api_key ⇒ Object
Returns the value of attribute api_key.
8 9 10 |
# File 'lib/unit-ruby/util/connection.rb', line 8 def api_key @api_key end |
.base_url ⇒ Object
Returns the value of attribute base_url.
8 9 10 |
# File 'lib/unit-ruby/util/connection.rb', line 8 def base_url @base_url end |
.trust_token ⇒ Object
Returns the value of attribute trust_token.
8 9 10 |
# File 'lib/unit-ruby/util/connection.rb', line 8 def trust_token @trust_token end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
11 12 13 |
# File 'lib/unit-ruby/util/connection.rb', line 11 def connection @connection end |
Instance Method Details
#from_json_api(response_body) ⇒ Object
62 63 64 65 66 |
# File 'lib/unit-ruby/util/connection.rb', line 62 def from_json_api(response_body) response_body.deep_transform_keys do |key| key.to_s.underscore.to_sym end.fetch(:data) end |
#get(path, params = nil) ⇒ Object
Executes a GET request to the API
26 27 28 29 30 31 32 |
# File 'lib/unit-ruby/util/connection.rb', line 26 def get(path, params = nil) response = connection.get(path, params) handle_errors(response) from_json_api(response.body) end |
#handle_errors(response) ⇒ Object
68 69 70 71 72 |
# File 'lib/unit-ruby/util/connection.rb', line 68 def handle_errors(response) return if response.success? raise(Error, response.body) end |
#patch(path, data = nil) ⇒ Object
Executes a PATCH request to the API
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/unit-ruby/util/connection.rb', line 50 def patch(path, data = nil) response = connection.patch do |req| req.url path req.headers['Content-Type'] = 'application/vnd.api+json' req.body = data.deep_transform_keys! { |key| key.to_s.camelize(:lower) } if data end handle_errors(response) from_json_api(response.body) end |
#post(path, data = nil) ⇒ Unit::APIResource
Executes a POST request to the API
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/unit-ruby/util/connection.rb', line 37 def post(path, data = nil) response = connection.post do |req| req.url path req.headers['Content-Type'] = 'application/vnd.api+json' req.body = data.deep_transform_keys! { |key| key.to_s.camelize(:lower) } if data end handle_errors(response) from_json_api(response.body) end |