Class: Faraday::Connection
- 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
-
#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
- #basic_auth(login, pass) ⇒ Object
- #build(options = {}, &block) ⇒ Object
-
#build_url(url, 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, &block) ⇒ Object
- #dup ⇒ Object
-
#escape(s) ⇒ Object
Be sure to URI escape ‘+’ symbols to %2B.
- #get(url = nil, headers = nil, &block) ⇒ Object
- #head(url = nil, headers = nil, &block) ⇒ Object
- #in_parallel(manager) ⇒ Object
- #in_parallel? ⇒ Boolean
-
#initialize(url = nil, options = {}, &block) ⇒ Connection
constructor
:url :params :headers :request :ssl.
-
#merge_headers(existing_headers, new_headers) ⇒ Object
turns headers keys and values into strings.
-
#merge_params(existing_params, new_params) ⇒ Object
turns param keys into strings.
- #post(url = nil, body = nil, headers = nil, &block) ⇒ Object
- #proxy(arg = nil) ⇒ Object
- #proxy_arg_to_uri(arg) ⇒ Object
- #put(url = nil, body = nil, headers = nil, &block) ⇒ Object
- #replace_query(uri, params) ⇒ Object
- #run_request(method, url, body, headers) ⇒ Object
-
#to_app ⇒ Object
return the assembled Rack application for this instance.
- #token_auth(token, options = {}) ⇒ Object
-
#url_prefix=(url) ⇒ Object
Parses the giving url with Addressable::URI and stores the individual components in this connection.
Constructor Details
#initialize(url = nil, options = {}, &block) ⇒ Connection
:url :params :headers :request :ssl
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 27 def initialize(url = nil, = {}, &block) if url.is_a?(Hash) = url url = [:url] end @headers = HeaderHash.new @params = {} = [:request] || {} @ssl = [:ssl] || {} @parallel_manager = [:parallel] self.url_prefix = url if url proxy([:proxy]) merge_params @params, [:params] if [:params] merge_headers @headers, [:headers] if [:headers] if block @builder = Builder.create(&block) else @builder = [:builder] || Builder.new end end |
Instance Attribute Details
#builder ⇒ Object (readonly)
Returns the value of attribute builder.
20 21 22 |
# File 'lib/faraday/connection.rb', line 20 def builder @builder end |
#headers ⇒ Object
Returns the value of attribute headers.
19 20 21 |
# File 'lib/faraday/connection.rb', line 19 def headers @headers end |
#host ⇒ Object
Returns the value of attribute host.
19 20 21 |
# File 'lib/faraday/connection.rb', line 19 def host @host end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
20 21 22 |
# File 'lib/faraday/connection.rb', line 20 def end |
#parallel_manager ⇒ Object
Returns the value of attribute parallel_manager.
19 20 21 |
# File 'lib/faraday/connection.rb', line 19 def parallel_manager @parallel_manager end |
#params ⇒ Object
Returns the value of attribute params.
19 20 21 |
# File 'lib/faraday/connection.rb', line 19 def params @params end |
#path_prefix ⇒ Object
Returns the value of attribute path_prefix.
20 21 22 |
# File 'lib/faraday/connection.rb', line 20 def path_prefix @path_prefix end |
#port ⇒ Object
Returns the value of attribute port.
19 20 21 |
# File 'lib/faraday/connection.rb', line 19 def port @port end |
#scheme ⇒ Object
Returns the value of attribute scheme.
19 20 21 |
# File 'lib/faraday/connection.rb', line 19 def scheme @scheme end |
#ssl ⇒ Object (readonly)
Returns the value of attribute ssl.
20 21 22 |
# File 'lib/faraday/connection.rb', line 20 def ssl @ssl end |
Instance Method Details
#basic_auth(login, pass) ⇒ Object
72 73 74 |
# File 'lib/faraday/connection.rb', line 72 def basic_auth(login, pass) @headers['authorization'] = "Basic #{Base64.encode64("#{login}:#{pass}").strip}" end |
#build(options = {}, &block) ⇒ Object
48 49 50 |
# File 'lib/faraday/connection.rb', line 48 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
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/faraday/connection.rb', line 174 def build_url(url, params = nil) uri = URI.parse(url.to_s) uri.host ||= @host uri.port ||= @port uri.scheme ||= @scheme 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
68 69 70 |
# File 'lib/faraday/connection.rb', line 68 def delete(url = nil, headers = nil, &block) run_request :delete, url, nil, headers, &block end |
#dup ⇒ Object
186 187 188 |
# File 'lib/faraday/connection.rb', line 186 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.
223 224 225 226 227 |
# File 'lib/faraday/connection.rb', line 223 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
52 53 54 |
# File 'lib/faraday/connection.rb', line 52 def get(url = nil, headers = nil, &block) run_request :get, url, nil, headers, &block end |
#head(url = nil, headers = nil, &block) ⇒ Object
64 65 66 |
# File 'lib/faraday/connection.rb', line 64 def head(url = nil, headers = nil, &block) run_request :head, url, nil, headers, &block end |
#in_parallel(manager) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/faraday/connection.rb', line 90 def in_parallel(manager) @parallel_manager = manager yield @parallel_manager && @parallel_manager.run ensure @parallel_manager = nil end |
#in_parallel? ⇒ Boolean
86 87 88 |
# File 'lib/faraday/connection.rb', line 86 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'
215 216 217 218 219 |
# File 'lib/faraday/connection.rb', line 215 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
203 204 205 206 207 |
# File 'lib/faraday/connection.rb', line 203 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
56 57 58 |
# File 'lib/faraday/connection.rb', line 56 def post(url = nil, body = nil, headers = nil, &block) run_request :post, url, body, headers, &block end |
#proxy(arg = nil) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/faraday/connection.rb', line 98 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 then arg 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
229 230 231 232 233 234 |
# File 'lib/faraday/connection.rb', line 229 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, &block) ⇒ Object
60 61 62 |
# File 'lib/faraday/connection.rb', line 60 def put(url = nil, body = nil, headers = nil, &block) run_request :put, url, body, headers, &block end |
#replace_query(uri, params) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/faraday/connection.rb', line 190 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
150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/faraday/connection.rb', line 150 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_app ⇒ Object
return the assembled Rack application for this instance.
146 147 148 |
# File 'lib/faraday/connection.rb', line 146 def to_app @builder.to_app end |
#token_auth(token, options = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/faraday/connection.rb', line 76 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
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/faraday/connection.rb', line 125 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 |