Class: Ashikawa::Core::FaradayFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/ashikawa-core/faraday_factory.rb

Overview

Create Faraday objects

Constant Summary collapse

DEFAULTS =

Defaults for the options of create connection

{
  additional_request_middlewares: [],
  additional_response_middlewares: [],
  additional_middlewares: [],
  adapter: Faraday.default_adapter
}
DEFAULT_REQUEST_MIDDLEWARES =

Request middlewares that will be prepended

[:json, :x_arango_version]
DEFAULT_RESPONSE_MIDDLEWARES =

Response middlewares that will be prepended

[:error_response, :json]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, additional_request_middlewares, additional_response_middlewares, additional_middlewares) ⇒ FaradayFactory

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new Faraday Factory with additional middlewares


59
60
61
62
63
64
# File 'lib/ashikawa-core/faraday_factory.rb', line 59

def initialize(adapter, additional_request_middlewares, additional_response_middlewares, additional_middlewares)
  @adapter = adapter
  @request_middlewares = DEFAULT_REQUEST_MIDDLEWARES + additional_request_middlewares
  @response_middlewares = DEFAULT_RESPONSE_MIDDLEWARES + additional_response_middlewares
  @additional_middlewares = additional_middlewares
end

Instance Attribute Details

#debug_headersObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Debug headers to be used by Faraday


50
51
52
# File 'lib/ashikawa-core/faraday_factory.rb', line 50

def debug_headers
  @debug_headers
end

Class Method Details

.create_connection(url, options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a Faraday object

Examples:

Create a FaradayObject with the given configuration

faraday = FaradayFactory.new('http://localhost:8529/_db/mydb/_api', logger: my_logger)

Options Hash (options):

  • adapter (Object)

    The Faraday adapter you want to use. Defaults to Default Adapter

  • logger (Object)

    The logger you want to use. Defaults to no logger.

  • additional_request_middlewares (Array)

    Additional request middlewares

  • additional_response_middlewares (Array)

    Additional response middlewares

  • additional_middlewares (Array)

    Additional middlewares


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ashikawa-core/faraday_factory.rb', line 34

def self.create_connection(url, options)
  options = DEFAULTS.merge(options)
  faraday = new(
    options.fetch(:adapter),
    options.fetch(:additional_request_middlewares),
    options.fetch(:additional_response_middlewares),
    options.fetch(:additional_middlewares)
  )
  faraday.debug_headers = options.fetch(:debug_headers) { false }
  faraday.logger = options.fetch(:logger) if options.key?(:logger)
  faraday.faraday_for(url)
end

Instance Method Details

#faraday_for(url) ⇒ Faraday

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create the Faraday for the given URL


79
80
81
82
83
84
85
86
# File 'lib/ashikawa-core/faraday_factory.rb', line 79

def faraday_for(url)
  Faraday.new(url) do |connection|
    @request_middlewares.each { |middleware| connection.request(*middleware) }
    @response_middlewares.each { |middleware| connection.response(*middleware) }
    @additional_middlewares.each { |middleware| connection.use(*middleware) }
    connection.adapter(*@adapter)
  end
end

#logger=(logger) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Logger to be used by Faraday


70
71
72
# File 'lib/ashikawa-core/faraday_factory.rb', line 70

def logger=(logger)
  @response_middlewares << [:minimal_logger, logger, debug_headers: debug_headers]
end