Class: Altertable::Adapters::FaradayAdapter

Inherits:
Base
  • Object
show all
Defined in:
lib/altertable/adapters.rb,
sig/altertable/adapters.rbs

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, timeout:, headers: {}, proxy: nil) ⇒ FaradayAdapter

proxy left unset (default) follows Faraday's own env-based auto-detection (HTTP_PROXY/HTTPS_PROXY/NO_PROXY). Pass false to disable proxying entirely for fixed, trusted destinations, or a URI string to force a specific proxy.

Parameters:

  • base_url: (String)
  • timeout: (Integer, Float)
  • headers: (::Hash[String, String]) (defaults to: {})
  • proxy: (String, bool, nil) (defaults to: nil)


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/altertable/adapters.rb', line 21

def initialize(base_url:, timeout:, headers: {}, proxy: nil)
  super(base_url: base_url, timeout: timeout, headers: headers)
  require "faraday"

  @conn = Faraday.new(url: @base_url) do |f|
    @headers.each { |k, v| f.headers[k] = v }
    f.options.timeout = @timeout
    f.proxy = proxy unless proxy.nil?
    f.adapter Faraday.default_adapter
  end
end

Instance Method Details

#post(path, body: nil, params: {}) ⇒ Response

Parameters:

  • path (String)
  • body: (String, nil) (defaults to: nil)
  • params: (::Hash[Symbol | String, untyped]) (defaults to: {})

Returns:



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/altertable/adapters.rb', line 33

def post(path, body: nil, params: {})
  resp = @conn.post(path) do |req|
    req.params = params
    req.body = body
  end
  wrap_response(resp)
rescue Faraday::ConnectionFailed => e
  raise Altertable::NetworkError.new(e.message, e)
rescue Faraday::TimeoutError => e
  raise Altertable::NetworkError.new("Timeout: #{e.message}", e)
end

#wrap_response(resp) ⇒ Response

Parameters:

  • resp (Object)

Returns:



47
48
49
# File 'lib/altertable/adapters.rb', line 47

def wrap_response(resp)
  Response.new(resp.status, resp.body, resp.headers)
end