Class: Faraday::Connection
- Includes:
- Addressable
- Defined in:
- lib/faraday/connection.rb
Direct Known Subclasses
Defined Under Namespace
Modules: Options
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#host ⇒ Object
Returns the value of attribute host.
-
#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.
Instance Method Summary collapse
- #build_hash(method, existing) ⇒ Object
- #build_headers(existing) ⇒ Object
- #build_params(existing) ⇒ Object
- #build_uri(url, params = nil) ⇒ Object
-
#escape_for_querystring(s) ⇒ Object
Some servers convert +‘s in URL query params to spaces.
-
#get(url, params = nil, headers = nil) ⇒ Object
Override in a subclass, or include an adapter.
- #in_parallel(options = {}) ⇒ Object
- #in_parallel? ⇒ Boolean
-
#initialize(url = nil, options = {}) ⇒ Connection
constructor
:url :params :headers :response.
- #params_to_query(params) ⇒ Object
- #path_for(uri) ⇒ Object
- #response_class ⇒ Object
- #response_class=(v) ⇒ Object
- #run_parallel_requests ⇒ Object
- #setup_parallel_manager(options = {}) ⇒ Object
- #url_prefix=(url) ⇒ Object
Constructor Details
#initialize(url = nil, options = {}) ⇒ Connection
:url :params :headers :response
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/faraday/connection.rb', line 22 def initialize(url = nil, = {}) if url.is_a?(Hash) = url url = [:url] end @response_class = [:response] @params = [:params] || {} @headers = [:headers] || {} self.url_prefix = url if url end |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
15 16 17 |
# File 'lib/faraday/connection.rb', line 15 def headers @headers end |
#host ⇒ Object
Returns the value of attribute host.
15 16 17 |
# File 'lib/faraday/connection.rb', line 15 def host @host end |
#params ⇒ Object
Returns the value of attribute params.
15 16 17 |
# File 'lib/faraday/connection.rb', line 15 def params @params end |
#path_prefix ⇒ Object
Returns the value of attribute path_prefix.
16 17 18 |
# File 'lib/faraday/connection.rb', line 16 def path_prefix @path_prefix end |
#port ⇒ Object
Returns the value of attribute port.
15 16 17 |
# File 'lib/faraday/connection.rb', line 15 def port @port end |
#scheme ⇒ Object
Returns the value of attribute scheme.
15 16 17 |
# File 'lib/faraday/connection.rb', line 15 def scheme @scheme end |
Instance Method Details
#build_hash(method, existing) ⇒ Object
119 120 121 |
# File 'lib/faraday/connection.rb', line 119 def build_hash(method, existing) existing ? send(method).merge(existing) : send(method) end |
#build_headers(existing) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/faraday/connection.rb', line 111 def build_headers(existing) build_hash(:headers, existing).tap do |headers| headers.keys.each do |key| headers[key] = headers.delete(key).to_s end end end |
#build_params(existing) ⇒ Object
107 108 109 |
# File 'lib/faraday/connection.rb', line 107 def build_params(existing) build_hash :params, existing end |
#build_uri(url, params = nil) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/faraday/connection.rb', line 86 def build_uri(url, params = nil) uri = URI.parse(url) 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 if params && !params.empty? uri.query = params_to_query(params) end uri end |
#escape_for_querystring(s) ⇒ Object
Some servers convert +‘s in URL query params to spaces. Go ahead and encode it.
131 132 133 134 135 |
# File 'lib/faraday/connection.rb', line 131 def escape_for_querystring(s) URI.encode_component(s.to_s, Addressable::URI::CharacterClasses::QUERY).tap do |escaped| escaped.gsub! /\+/, "%2B" end end |
#get(url, params = nil, headers = nil) ⇒ Object
Override in a subclass, or include an adapter
def _get(uri, headers)
end
46 47 48 49 |
# File 'lib/faraday/connection.rb', line 46 def get(url, params = nil, headers = nil) uri = build_uri(url, build_params(params)) _get(uri, build_headers(headers)) end |
#in_parallel(options = {}) ⇒ Object
66 67 68 69 70 |
# File 'lib/faraday/connection.rb', line 66 def in_parallel( = {}) @parallel_manager = true yield @parallel_manager = false end |
#in_parallel? ⇒ Boolean
62 63 64 |
# File 'lib/faraday/connection.rb', line 62 def in_parallel? !!@parallel_manager end |
#params_to_query(params) ⇒ Object
123 124 125 126 127 |
# File 'lib/faraday/connection.rb', line 123 def params_to_query(params) params.inject([]) do |memo, (key, val)| memo << "#{escape_for_querystring(key)}=#{escape_for_querystring(val)}" end.join("&") end |
#path_for(uri) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/faraday/connection.rb', line 100 def path_for(uri) uri.path.tap do |s| s << "?#{uri.query}" if uri.query s << "##{uri.fragment}" if uri.fragment end end |
#response_class ⇒ Object
51 52 53 |
# File 'lib/faraday/connection.rb', line 51 def response_class @response_class || Response end |
#response_class=(v) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/faraday/connection.rb', line 55 def response_class=(v) if v.respond_to?(:loaded?) && !v.loaded? raise ArgumentError, "The response class: #{v.inspect} does not appear to be loaded." end @response_class = v end |
#run_parallel_requests ⇒ Object
75 76 |
# File 'lib/faraday/connection.rb', line 75 def run_parallel_requests end |
#setup_parallel_manager(options = {}) ⇒ Object
72 73 |
# File 'lib/faraday/connection.rb', line 72 def setup_parallel_manager( = {}) end |
#url_prefix=(url) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/faraday/connection.rb', line 33 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 end |