Class: LoogiHttp::Configuration
- Inherits:
-
Object
- Object
- LoogiHttp::Configuration
- Extended by:
- Forwardable
- Defined in:
- lib/loogi_http/configuration.rb
Overview
Configure a ‘Faraday::Connection` (aka a stack).
Instance Method Summary collapse
-
#adapter(*args) {|adapter| ... } ⇒ Object
Configure the adapter to be used by the stack.
-
#configure {|config| ... } ⇒ Faraday::Connection
Custom configuration of the stack in a block.
-
#initialize(faraday_connection) ⇒ Configuration
constructor
A new instance of Configuration.
-
#json ⇒ Object
Configure the stack for JSON, using three middlewares:.
Constructor Details
#initialize(faraday_connection) ⇒ Configuration
Returns a new instance of Configuration.
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
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
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 |
#json ⇒ Object
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 |