Class: Faraday::Connection

Inherits:
Object show all
Includes:
Addressable, Utils
Defined in:
lib/faraday/connection.rb

Constant Summary collapse

METHODS =
Set.new [:get, :post, :put, :delete, :head]
METHODS_WITH_BODIES =
Set.new [:post, :put]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#build_nested_query, #escape, #merge_headers, #merge_params, #normalize_path

Constructor Details

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

:url :params :headers :request :ssl



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/faraday/connection.rb', line 20

def initialize(url = nil, options = {})
  if url.is_a?(Hash)
    options = url
    url     = options[:url]
  end
  @headers          = Headers.new
  @params           = {}
  @options          = options[:request] || {}
  @ssl              = options[:ssl]     || {}
  @parallel_manager = options[:parallel]
  self.url_prefix = url if url
  proxy(options[:proxy])
  merge_params  @params,  options[:params]  if options[:params]
  merge_headers @headers, options[:headers] if options[:headers]

  if block_given?
    @builder = Builder.create { |b| yield b }
  else
    @builder = options[:builder] || Builder.new
  end
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



13
14
15
# File 'lib/faraday/connection.rb', line 13

def builder
  @builder
end

#headersObject

Returns the value of attribute headers.



12
13
14
# File 'lib/faraday/connection.rb', line 12

def headers
  @headers
end

#hostObject

Returns the value of attribute host.



12
13
14
# File 'lib/faraday/connection.rb', line 12

def host
  @host
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/faraday/connection.rb', line 13

def options
  @options
end

#parallel_managerObject

Returns the value of attribute parallel_manager.



12
13
14
# File 'lib/faraday/connection.rb', line 12

def parallel_manager
  @parallel_manager
end

#paramsObject

Returns the value of attribute params.



12
13
14
# File 'lib/faraday/connection.rb', line 12

def params
  @params
end

#path_prefixObject

Returns the value of attribute path_prefix.



13
14
15
# File 'lib/faraday/connection.rb', line 13

def path_prefix
  @path_prefix
end

#portObject

Returns the value of attribute port.



12
13
14
# File 'lib/faraday/connection.rb', line 12

def port
  @port
end

#schemeObject

Returns the value of attribute scheme.



12
13
14
# File 'lib/faraday/connection.rb', line 12

def scheme
  @scheme
end

#sslObject (readonly)

Returns the value of attribute ssl.



13
14
15
# File 'lib/faraday/connection.rb', line 13

def ssl
  @ssl
end

Instance Method Details

#adapter(key, *args, &block) ⇒ Object



54
55
56
# File 'lib/faraday/connection.rb', line 54

def adapter(key, *args, &block)
  @builder.adapter(key, *args, &block)
end

#basic_auth(login, pass) ⇒ Object



87
88
89
90
91
# File 'lib/faraday/connection.rb', line 87

def basic_auth(, pass)
  auth = Base64.encode64("#{}:#{pass}")
  auth.gsub!("\n", "")
  @headers['authorization'] = "Basic #{auth}"
end

#build(options = {}, &block) ⇒ Object



58
59
60
# File 'lib/faraday/connection.rb', line 58

def build(options = {}, &block)
  @builder.build(options, &block)
end

#build_url(url, params = nil) ⇒ Object

Takes a relative url for a request and combines it with the defaults set on the connection instance.

conn = Faraday::Connection.new { ... }
conn.url_prefix = "https://sushi.com/api?token=abc"
conn.scheme      # => https
conn.path_prefix # => "/api"

conn.build_url("nigiri?page=2")      # => https://sushi.com/api/nigiri?token=abc&page=2
conn.build_url("nigiri", :page => 2) # => https://sushi.com/api/nigiri?token=abc&page=2


189
190
191
192
193
194
195
196
197
198
199
# File 'lib/faraday/connection.rb', line 189

def build_url(url, params = nil)
  uri          = URI.parse(url.to_s)
  if @path_prefix && uri.path !~ /^\//
    uri.path = "#{@path_prefix.size > 1 ? @path_prefix : nil}/#{uri.path}"
  end
  uri.host   ||= @host
  uri.port   ||= @port
  uri.scheme ||= @scheme
  replace_query(uri, params)
  uri
end

#delete(url = nil, headers = nil) ⇒ Object



82
83
84
85
# File 'lib/faraday/connection.rb', line 82

def delete(url = nil, headers = nil)
  block = block_given? ? Proc.new : nil
  run_request(:delete, url, nil, headers, &block)
end

#dupObject



201
202
203
# File 'lib/faraday/connection.rb', line 201

def dup
  self.class.new(build_url(''), :headers => headers.dup, :params => params.dup, :builder => builder.dup)
end

#get(url = nil, headers = nil) ⇒ Object



62
63
64
65
# File 'lib/faraday/connection.rb', line 62

def get(url = nil, headers = nil)
  block = block_given? ? Proc.new : nil
  run_request(:get, url, nil, headers, &block)
end

#head(url = nil, headers = nil) ⇒ Object



77
78
79
80
# File 'lib/faraday/connection.rb', line 77

def head(url = nil, headers = nil)
  block = block_given? ? Proc.new : nil
  run_request(:head, url, nil, headers, &block)
end

#in_parallel(manager) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/faraday/connection.rb', line 107

def in_parallel(manager)
  @parallel_manager = manager
  yield
  @parallel_manager && @parallel_manager.run
ensure
  @parallel_manager = nil
end

#in_parallel?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/faraday/connection.rb', line 103

def in_parallel?
  !!@parallel_manager
end

#post(url = nil, body = nil, headers = nil) ⇒ Object



67
68
69
70
# File 'lib/faraday/connection.rb', line 67

def post(url = nil, body = nil, headers = nil)
  block = block_given? ? Proc.new : nil
  run_request(:post, url, body, headers, &block)
end

#proxy(arg = nil) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/faraday/connection.rb', line 115

def proxy(arg = nil)
  return @proxy if arg.nil?

  @proxy =
    case arg
      when String then {:uri => proxy_arg_to_uri(arg)}
      when URI    then {:uri => arg}
      when Hash
        if arg[:uri] = proxy_arg_to_uri(arg[:uri])
          arg
        else
          raise ArgumentError, "no :uri option."
        end
    end
end

#proxy_arg_to_uri(arg) ⇒ Object



217
218
219
220
221
222
# File 'lib/faraday/connection.rb', line 217

def proxy_arg_to_uri(arg)
  case arg
    when String then URI.parse(arg)
    when URI    then arg
  end
end

#put(url = nil, body = nil, headers = nil) ⇒ Object



72
73
74
75
# File 'lib/faraday/connection.rb', line 72

def put(url = nil, body = nil, headers = nil)
  block = block_given? ? Proc.new : nil
  run_request(:put, url, body, headers, &block)
end

#replace_query(uri, params) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
# File 'lib/faraday/connection.rb', line 205

def replace_query(uri, params)
  url_params = @params.dup
  if uri.query && !uri.query.empty?
    merge_params(url_params, parse_query(uri.query))
  end
  if params && !params.empty?
    merge_params(url_params, params)
  end
  uri.query = url_params.empty? ? nil : build_query(url_params)
  uri
end

#request(key, *args, &block) ⇒ Object



46
47
48
# File 'lib/faraday/connection.rb', line 46

def request(key, *args, &block)
  @builder.request(key, *args, &block)
end

#response(key, *args, &block) ⇒ Object



50
51
52
# File 'lib/faraday/connection.rb', line 50

def response(key, *args, &block)
  @builder.response(key, *args, &block)
end

#run_request(method, url, body, headers) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/faraday/connection.rb', line 165

def run_request(method, url, body, headers)
  if !METHODS.include?(method)
    raise ArgumentError, "unknown http method: #{method}"
  end

  Request.run(self, method) do |req|
    req.url(url)                if url
    req.headers.update(headers) if headers
    req.body = body             if body
    yield req if block_given?
  end
end

#token_auth(token, options = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/faraday/connection.rb', line 93

def token_auth(token, options = {})
  values = ["token=#{token.to_s.inspect}"]
  options.each do |key, value|
    values << "#{key}=#{value.to_s.inspect}"
  end
  # 21 = "Authorization: Token ".size
  comma = ",\n#{' ' * 21}"
  @headers['authorization'] = "Token #{values * comma}"
end

#url_prefix=(url) ⇒ Object

Parses the giving url with Addressable::URI and stores the individual components in this connection. These components serve as defaults for requests made by this connection.

conn = Faraday::Connection.new { ... }
conn.url_prefix = "https://sushi.com/api"
conn.scheme      # => https
conn.path_prefix # => "/api"

conn.get("nigiri?page=2") # accesses https://sushi.com/api/nigiri


142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/faraday/connection.rb', line 142

def url_prefix=(url)
  uri              = URI.parse(url)
  self.scheme      = uri.scheme
  self.host        = uri.host
  self.port        = uri.port
  self.path_prefix = uri.path
  if uri.query && !uri.query.empty?
    merge_params @params, parse_query(uri.query)
  end
  if uri.user && uri.password
    basic_auth(uri.user, uri.password)
  end
end

#use(klass, *args, &block) ⇒ Object



42
43
44
# File 'lib/faraday/connection.rb', line 42

def use(klass, *args, &block)
  @builder.use(klass, *args, &block)
end