Class: Faraday::Connection
- Includes:
- Addressable
- Defined in:
- lib/faraday/connection.rb
Constant Summary collapse
- METHODS =
Set.new [:get, :post, :put, :delete, :head, :patch, :options]
- METHODS_WITH_BODIES =
Set.new [:post, :put, :patch, :options]
Instance Attribute Summary collapse
-
#builder ⇒ Object
readonly
Returns the value of attribute builder.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#host ⇒ Object
Returns the value of attribute host.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parallel_manager ⇒ Object
Returns the value of attribute parallel_manager.
-
#params ⇒ Object
Returns the value of attribute params.
-
#path_prefix ⇒ Object
Returns the value of attribute path_prefix.
-
#port ⇒ Object
Returns the value of attribute port.
-
#scheme ⇒ Object
Returns the value of attribute scheme.
-
#ssl ⇒ Object
readonly
Returns the value of attribute ssl.
Instance Method Summary collapse
- #adapter(key, *args, &block) ⇒ Object
-
#app ⇒ Object
The “rack app” wrapped in middleware.
- #basic_auth(login, pass) ⇒ Object
- #build(options = {}, &block) ⇒ Object
-
#build_url(url, extra_params = nil) ⇒ Object
Takes a relative url for a request and combines it with the defaults set on the connection instance.
- #delete(url = nil, headers = nil) ⇒ Object
- #dup ⇒ Object
- #get(url = nil, headers = nil) ⇒ Object
- #head(url = nil, headers = nil) ⇒ Object
- #in_parallel(manager) ⇒ Object
- #in_parallel? ⇒ Boolean
-
#initialize(url = nil, options = {}) ⇒ Connection
constructor
:url :params :headers :request :ssl.
- #patch(url = nil, body = nil, headers = nil) ⇒ Object
- #post(url = nil, body = nil, headers = nil) ⇒ Object
- #proxy(arg = nil) ⇒ Object
- #proxy_arg_to_uri(arg) ⇒ Object
- #put(url = nil, body = nil, headers = nil) ⇒ Object
- #request(key, *args, &block) ⇒ Object
- #response(key, *args, &block) ⇒ Object
- #run_request(method, url, body, headers) ⇒ Object
- #token_auth(token, options = {}) ⇒ Object
-
#url_prefix=(url) ⇒ Object
Parses the giving url with Addressable::URI and stores the individual components in this connection.
- #use(klass, *args, &block) ⇒ Object
Constructor Details
#initialize(url = nil, options = {}) ⇒ Connection
:url :params :headers :request :ssl
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/faraday/connection.rb', line 24 def initialize(url = nil, = {}) if url.is_a?(Hash) = url url = [:url] end @headers = Utils::Headers.new @params = Utils::ParamsHash.new @options = [:request] || {} @ssl = [:ssl] || {} @parallel_manager = [:parallel] self.url_prefix = url if url proxy([:proxy]) @params.update [:params] if [:params] @headers.update [:headers] if [:headers] if block_given? @builder = Builder.create { |b| yield b } else @builder = [:builder] || Builder.new end end |
Instance Attribute Details
#builder ⇒ Object (readonly)
Returns the value of attribute builder.
17 18 19 |
# File 'lib/faraday/connection.rb', line 17 def builder @builder end |
#headers ⇒ Object
Returns the value of attribute headers.
16 17 18 |
# File 'lib/faraday/connection.rb', line 16 def headers @headers end |
#host ⇒ Object
Returns the value of attribute host.
16 17 18 |
# File 'lib/faraday/connection.rb', line 16 def host @host end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
17 18 19 |
# File 'lib/faraday/connection.rb', line 17 def @options end |
#parallel_manager ⇒ Object
Returns the value of attribute parallel_manager.
16 17 18 |
# File 'lib/faraday/connection.rb', line 16 def parallel_manager @parallel_manager end |
#params ⇒ Object
Returns the value of attribute params.
16 17 18 |
# File 'lib/faraday/connection.rb', line 16 def params @params end |
#path_prefix ⇒ Object
Returns the value of attribute path_prefix.
17 18 19 |
# File 'lib/faraday/connection.rb', line 17 def path_prefix @path_prefix end |
#port ⇒ Object
Returns the value of attribute port.
16 17 18 |
# File 'lib/faraday/connection.rb', line 16 def port @port end |
#scheme ⇒ Object
Returns the value of attribute scheme.
16 17 18 |
# File 'lib/faraday/connection.rb', line 16 def scheme @scheme end |
#ssl ⇒ Object (readonly)
Returns the value of attribute ssl.
17 18 19 |
# File 'lib/faraday/connection.rb', line 17 def ssl @ssl end |
Instance Method Details
#adapter(key, *args, &block) ⇒ Object
60 61 62 |
# File 'lib/faraday/connection.rb', line 60 def adapter(key, *args, &block) @builder.adapter(key, *args, &block) end |
#app ⇒ Object
The “rack app” wrapped in middleware. All requests are sent here.
The builder is responsible for creating the app object. After this, the builder gets locked to ensure no further modifications are made to the middleware stack.
Returns an object that responds to ‘call` and returns a Response.
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/faraday/connection.rb', line 75 def app @app ||= begin builder.lock! builder.to_app(lambda { |env| # the inner app that creates and returns the Response object response = Response.new response.finish(env) unless env[:parallel_manager] env[:response] = response }) end end |
#basic_auth(login, pass) ⇒ Object
117 118 119 120 121 |
# File 'lib/faraday/connection.rb', line 117 def basic_auth(login, pass) auth = Base64.encode64("#{login}:#{pass}") auth.gsub!("\n", "") @headers['authorization'] = "Basic #{auth}" end |
#build(options = {}, &block) ⇒ Object
64 65 66 |
# File 'lib/faraday/connection.rb', line 64 def build( = {}, &block) @builder.build(, &block) end |
#build_url(url, extra_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
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/faraday/connection.rb', line 221 def build_url(url, extra_params = nil) uri = URI.parse(url.to_s) if @path_prefix && uri.path !~ /^\// new_path = @path_prefix.size > 1 ? @path_prefix.dup : '' new_path << "/#{uri.path}" unless uri.path.empty? uri.path = new_path end uri.host ||= @host uri.port ||= @port uri.scheme ||= @scheme params = @params.dup.merge_query(uri.query) params.update extra_params if extra_params uri.query = params.empty? ? nil : params.to_query uri end |
#delete(url = nil, headers = nil) ⇒ Object
112 113 114 115 |
# File 'lib/faraday/connection.rb', line 112 def delete(url = nil, headers = nil) block = block_given? ? Proc.new : nil run_request(:delete, url, nil, headers, &block) end |
#dup ⇒ Object
239 240 241 |
# File 'lib/faraday/connection.rb', line 239 def dup self.class.new(build_url(''), :headers => headers.dup, :params => params.dup, :builder => builder.dup, :ssl => ssl.dup) end |
#get(url = nil, headers = nil) ⇒ Object
87 88 89 90 |
# File 'lib/faraday/connection.rb', line 87 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
107 108 109 110 |
# File 'lib/faraday/connection.rb', line 107 def head(url = nil, headers = nil) block = block_given? ? Proc.new : nil run_request(:head, url, nil, headers, &block) end |
#in_parallel(manager) ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/faraday/connection.rb', line 137 def in_parallel(manager) @parallel_manager = manager yield @parallel_manager && @parallel_manager.run ensure @parallel_manager = nil end |
#in_parallel? ⇒ Boolean
133 134 135 |
# File 'lib/faraday/connection.rb', line 133 def in_parallel? !!@parallel_manager end |
#patch(url = nil, body = nil, headers = nil) ⇒ Object
102 103 104 105 |
# File 'lib/faraday/connection.rb', line 102 def patch(url = nil, body = nil, headers = nil) block = block_given? ? Proc.new : nil run_request(:patch, url, body, headers, &block) end |
#post(url = nil, body = nil, headers = nil) ⇒ Object
92 93 94 95 |
# File 'lib/faraday/connection.rb', line 92 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
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/faraday/connection.rb', line 145 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
243 244 245 246 247 248 |
# File 'lib/faraday/connection.rb', line 243 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
97 98 99 100 |
# File 'lib/faraday/connection.rb', line 97 def put(url = nil, body = nil, headers = nil) block = block_given? ? Proc.new : nil run_request(:put, url, body, headers, &block) end |
#request(key, *args, &block) ⇒ Object
52 53 54 |
# File 'lib/faraday/connection.rb', line 52 def request(key, *args, &block) @builder.request(key, *args, &block) end |
#response(key, *args, &block) ⇒ Object
56 57 58 |
# File 'lib/faraday/connection.rb', line 56 def response(key, *args, &block) @builder.response(key, *args, &block) end |
#run_request(method, url, body, headers) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/faraday/connection.rb', line 194 def run_request(method, url, body, headers) if !METHODS.include?(method) raise ArgumentError, "unknown http method: #{method}" end request = Request.create(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 env = request.to_env(self) self.app.call(env) end |
#token_auth(token, options = {}) ⇒ Object
123 124 125 126 127 128 129 130 131 |
# File 'lib/faraday/connection.rb', line 123 def token_auth(token, = {}) values = ["token=#{token.to_s.inspect}"] .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
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/faraday/connection.rb', line 172 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 @params.merge_query(uri.query) basic_auth(uri.user, uri.password) if uri.user && uri.password uri end |
#use(klass, *args, &block) ⇒ Object
48 49 50 |
# File 'lib/faraday/connection.rb', line 48 def use(klass, *args, &block) @builder.use(klass, *args, &block) end |