Class: Faraday::Connection

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

Constant Summary collapse

HEADERS =
Hash.new { |h, k| k.respond_to?(:to_str) ? k : k.to_s.capitalize }.update \
:content_type    => "Content-Type",
:content_length  => "Content-Length",
:accept_charset  => "Accept-Charset",
:accept_encoding => "Accept-Encoding"
METHODS =
Set.new [:get, :post, :put, :delete, :head]
METHODS_WITH_BODIES =
Set.new [:post, :put]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

:url :params :headers



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

def initialize(url = nil, options = {}, &block)
  if url.is_a?(Hash)
    options = url
    url     = options[:url]
  end
  @headers          = HeaderHash.new
  @params           = {}
  @parallel_manager = options[:parallel]
  self.url_prefix = url if url
  merge_params  @params,  options[:params]  if options[:params]
  merge_headers @headers, options[:headers] if options[:headers]
  if block
    @builder = Builder.create(&block)
  else
    @builder = options[:builder] || Builder.new
  end
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



19
20
21
# File 'lib/faraday/connection.rb', line 19

def builder
  @builder
end

#headersObject

Returns the value of attribute headers.



18
19
20
# File 'lib/faraday/connection.rb', line 18

def headers
  @headers
end

#hostObject

Returns the value of attribute host.



18
19
20
# File 'lib/faraday/connection.rb', line 18

def host
  @host
end

#parallel_managerObject

Returns the value of attribute parallel_manager.



18
19
20
# File 'lib/faraday/connection.rb', line 18

def parallel_manager
  @parallel_manager
end

#paramsObject

Returns the value of attribute params.



18
19
20
# File 'lib/faraday/connection.rb', line 18

def params
  @params
end

#path_prefixObject

Returns the value of attribute path_prefix.



19
20
21
# File 'lib/faraday/connection.rb', line 19

def path_prefix
  @path_prefix
end

#portObject

Returns the value of attribute port.



18
19
20
# File 'lib/faraday/connection.rb', line 18

def port
  @port
end

#schemeObject

Returns the value of attribute scheme.



18
19
20
# File 'lib/faraday/connection.rb', line 18

def scheme
  @scheme
end

Instance Method Details

#build(&block) ⇒ Object



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

def build(&block)
  @builder.build(&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


138
139
140
141
142
143
144
145
146
147
148
# File 'lib/faraday/connection.rb', line 138

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

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



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

def delete(url = nil, headers = nil, &block)
  run_request :delete, url, nil, headers, &block
end

#dupObject



150
151
152
# File 'lib/faraday/connection.rb', line 150

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

#escape(s) ⇒ Object

Be sure to URI escape ‘+’ symbols to %2B. Otherwise, they get interpreted as spaces.



187
188
189
190
191
# File 'lib/faraday/connection.rb', line 187

def escape(s)
  s.to_s.gsub(/([^a-zA-Z0-9_.-]+)/n) do
    '%' << $1.unpack('H2'*bytesize($1)).join('%').tap { |c| c.upcase! }
  end
end

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



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

def get(url = nil, headers = nil, &block)
  run_request :get, url, nil, headers, &block
end

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



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

def head(url = nil, headers = nil, &block)
  run_request :head, url, nil, headers, &block
end

#in_parallel(manager) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/faraday/connection.rb', line 83

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

#in_parallel?Boolean

Returns:

  • (Boolean)


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

def in_parallel?
  !!@parallel_manager
end

#merge_headers(existing_headers, new_headers) ⇒ Object

turns headers keys and values into strings. Look up symbol keys in the the HEADERS hash.

h = merge_headers(HeaderHash.new, :content_type => 'text/plain')
h['Content-Type'] # = 'text/plain'


179
180
181
182
183
# File 'lib/faraday/connection.rb', line 179

def merge_headers(existing_headers, new_headers)
  new_headers.each do |key, value|
    existing_headers[HEADERS[key]] = value.to_s
  end
end

#merge_params(existing_params, new_params) ⇒ Object

turns param keys into strings



167
168
169
170
171
# File 'lib/faraday/connection.rb', line 167

def merge_params(existing_params, new_params)
  new_params.each do |key, value|
    existing_params[key.to_s] = value
  end
end

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



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

def post(url = nil, body = nil, headers = nil, &block)
  run_request :post, url, body, headers, &block
end

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



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

def put(url = nil, body = nil, headers = nil, &block)
  run_request :put, url, body, headers, &block
end

#replace_query(uri, params) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/faraday/connection.rb', line 154

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

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



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/faraday/connection.rb', line 66

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

#to_appObject

return the assembled Rack application for this instance.



92
93
94
# File 'lib/faraday/connection.rb', line 92

def to_app
  @builder.to_app
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


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

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
end