Class: TrueTrial::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/truetrial/http_client.rb

Overview

Low-level HTTP client wrapping Faraday for TrueTrial API communication.

Instance Method Summary collapse

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.

Parameters:

  • path (String)

    the API endpoint path

Returns:

  • (Hash)

    parsed JSON response



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.

Parameters:

  • path (String)

    the API endpoint path

  • params (Hash) (defaults to: {})

    optional query parameters

Returns:

  • (Hash)

    parsed JSON response



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.

Parameters:

  • path (String)

    the API endpoint path

  • body (Hash) (defaults to: {})

    the request body

Returns:

  • (Hash)

    parsed JSON response



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