Class: LoogiHttp::Configuration

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/loogi_http/configuration.rb

Overview

Configure a ‘Faraday::Connection` (aka a stack).

Instance Method Summary collapse

Constructor Details

#initialize(faraday_connection) ⇒ Configuration

Returns a new instance of Configuration.

Parameters:

  • faraday_connection (Faraday::Connection)

    Stack to be configured



11
12
13
# File 'lib/loogi_http/configuration.rb', line 11

def initialize(faraday_connection)
  @faraday_connection = faraday_connection
end

Instance Method Details

#adapter(*args) {|adapter| ... } ⇒ Object

Configure the adapter to be used by the stack. The default stack is ‘Net::HTTP`.

Example customization of ‘Net::HTTP`:

config.adapter :net_http do |http| # yields Net::HTTP

http.idle_timeout = 100
http.verify_callback = lambda do | preverify_ok, cert_store |
  # do something here...
end

end

More details in the Faraday docs

Parameters:

  • args (Array<Object>)

    Adapter arguments, name first, defaults to ‘:net_http`

Yields:

  • (adapter)

    Configure the adapter before use

See Also:



66
67
68
69
# File 'lib/loogi_http/configuration.rb', line 66

def adapter(*args, &block)
  @adapter_args = args
  @adapter_block = block
end

#configure {|config| ... } ⇒ Faraday::Connection

Custom configuration of the stack in a block. By default, the stack is configured to:

  • instrument call with ‘ActiveSupport::Notifications`

  • follow redirects

  • use ‘Net::HTTP` adapter

Example:

config.configure do |config|

config.json

end

Yields:

  • (config)

    Customize the stack within this block

Yield Parameters:

Returns:

  • (Faraday::Connection)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/loogi_http/configuration.rb', line 31

def configure(&_block)
  use :instrumentation if defined? ::ActiveSupport::Notifications

  logger = LoogiHttp.logger
  level = LoogiHttp.log_level
  use :log_request, logger: logger, level: level if logger

  response :follow_redirects

  yield self if block_given?

  use :debug_http
  faraday_connection.adapter(*adapter_args, &adapter_block)

  faraday_connection
end

#jsonObject

Configure the stack for JSON, using three middlewares:

  • Set the ‘Accept` header

  • ensure the body sent is JSON (converts non-String via ‘to_json`)

  • parse result body as JSON for JSON content type



76
77
78
79
80
# File 'lib/loogi_http/configuration.rb', line 76

def json
  request :accept_json
  request :json
  response :json, content_type: 'application/json'
end