Method: Faraday::Connection#initialize

Defined in:
lib/faraday/connection.rb

#initialize(url = nil, options = {}) {|_self| ... } ⇒ Connection

Public: Initializes a new Faraday::Connection.

url - URI or String base URL to use as a prefix for all

requests (optional).

options - Hash of settings that will be applied to every request made

from this Connection (default: {}).
:url     - URI or String base URL (default: "http:/").
:params  - Hash of URI query unencoded key/value pairs.
:headers - Hash of unencoded HTTP header key/value pairs.
:request - Hash of request options.
:ssl     - Hash of SSL options.
:proxy   - URI, String or Hash of HTTP proxy options
          (default: "http_proxy" environment variable).
          :uri      - URI or String
          :user     - String (optional)
          :password - String (optional)

Yields:

  • (_self)

Yield Parameters:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/faraday/connection.rb', line 32

def initialize(url = nil, options = {})
  if url.is_a?(Hash)
    options = url
    url     = options[:url]
  end
  @headers = Utils::Headers.new
  @params  = Utils::ParamsHash.new
  @options = options[:request] || {}
  @ssl     = options[:ssl]     || {}

  @parallel_manager = nil
  @default_parallel_manager = options[:parallel_manager]

  @builder = options[:builder] || begin
    # pass an empty block to Builder so it doesn't assume default middleware
    block = block_given?? Proc.new {|b| } : nil
    Builder.new(&block)
  end

  self.url_prefix = url || 'http:/'

  @params.update options[:params]   if options[:params]
  @headers.update options[:headers] if options[:headers]

  @proxy = nil
  proxy(options.fetch(:proxy) { ENV['http_proxy'] })

  yield self if block_given?

  @headers[:user_agent] ||= "Faraday v#{VERSION}"
end

Comments