Class: TrueTrial::HttpClient
- Inherits:
-
Object
- Object
- TrueTrial::HttpClient
- Defined in:
- lib/truetrial/http_client.rb
Overview
Low-level HTTP client wrapping Faraday for TrueTrial API communication.
Instance Method Summary collapse
-
#delete(path) ⇒ Hash
Performs a DELETE request.
-
#get(path, params: {}) ⇒ Hash
Performs a GET request.
-
#initialize(api_key:, base_url:) ⇒ HttpClient
constructor
A new instance of HttpClient.
-
#post(path, body: {}) ⇒ Hash
Performs a POST request.
Constructor Details
#initialize(api_key:, base_url:) ⇒ HttpClient
Returns a new instance of HttpClient.
9 10 11 12 13 14 15 16 17 |
# File 'lib/truetrial/http_client.rb', line 9 def initialize(api_key:, base_url:) @connection = Faraday.new(url: base_url) do |conn| conn.headers["X-Api-Key"] = api_key conn.headers["Content-Type"] = "application/json" conn.headers["Accept"] = "application/json" conn.headers["User-Agent"] = "truetrial-ruby/#{TrueTrial::VERSION}" conn.adapter Faraday.default_adapter end end |
Instance Method Details
#delete(path) ⇒ Hash
Performs a DELETE request.
47 48 49 50 |
# File 'lib/truetrial/http_client.rb', line 47 def delete(path) response = @connection.delete(path) handle_response(response) end |
#get(path, params: {}) ⇒ Hash
Performs a GET request.
24 25 26 27 28 29 |
# File 'lib/truetrial/http_client.rb', line 24 def get(path, params: {}) response = @connection.get(path) do |req| req.params = params unless params.empty? end handle_response(response) end |
#post(path, body: {}) ⇒ Hash
Performs a POST request.
36 37 38 39 40 41 |
# File 'lib/truetrial/http_client.rb', line 36 def post(path, body: {}) response = @connection.post(path) do |req| req.body = JSON.generate(body) end handle_response(response) end |