Class: Faraday::Connection

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

Direct Known Subclasses

TestConnection

Defined Under Namespace

Modules: Options

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  if url.is_a?(Hash)
    options = url
    url     = options[:url]
  end
  @response_class = options[:response]
  @params         = options[:params]  || {}
  @headers        = options[:headers] || {}
  self.url_prefix = url if url
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers


15
16
17
# File 'lib/faraday/connection.rb', line 15

def headers
  @headers
end

#hostObject

Returns the value of attribute host


15
16
17
# File 'lib/faraday/connection.rb', line 15

def host
  @host
end

#paramsObject

Returns the value of attribute params


15
16
17
# File 'lib/faraday/connection.rb', line 15

def params
  @params
end

#path_prefixObject

Returns the value of attribute path_prefix


16
17
18
# File 'lib/faraday/connection.rb', line 16

def path_prefix
  @path_prefix
end

#portObject

Returns the value of attribute port


15
16
17
# File 'lib/faraday/connection.rb', line 15

def port
  @port
end

#schemeObject

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(options = {})
  @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_classObject


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_requestsObject


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(options = {})
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