Class: Faraday::Request

Inherits:
Struct
  • Object
show all
Extended by:
AutoloadHelper
Defined in:
lib/faraday/request.rb

Overview

Used to setup urls, params, headers, and the request body in a sane manner.

@connection.post do |req|
  req.url 'http://localhost', 'a' => '1' # 'http://localhost?a=1'
  req.headers['b'] = '2' # Header
  req.params['c']  = '3' # GET Param
  req['b']         = '2' # also Header
  req.body = 'abc'
end

Defined Under Namespace

Classes: JSON, Multipart, UrlEncoded

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AutoloadHelper

all_loaded_constants, autoload_all, load_autoloaded_constants, lookup_module, register_lookup_modules

Instance Attribute Details

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



12
13
14
# File 'lib/faraday/request.rb', line 12

def body
  @body
end

#headersObject

Returns the value of attribute headers

Returns:

  • (Object)

    the current value of headers



12
13
14
# File 'lib/faraday/request.rb', line 12

def headers
  @headers
end

#paramsObject

Returns the value of attribute params

Returns:

  • (Object)

    the current value of params



12
13
14
# File 'lib/faraday/request.rb', line 12

def params
  @params
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



12
13
14
# File 'lib/faraday/request.rb', line 12

def path
  @path
end

Class Method Details

.create {|req| ... } ⇒ Object

Yields:

  • (req)


31
32
33
34
35
# File 'lib/faraday/request.rb', line 31

def self.create
  req = new(nil, {}, {}, nil)
  yield req if block_given?
  req
end

.run(connection, request_method) {|req| ... } ⇒ Object

Yields:

  • (req)


25
26
27
28
29
# File 'lib/faraday/request.rb', line 25

def self.run(connection, request_method)
  req = create
  yield req if block_given?
  req.run(connection, request_method)
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  headers[key]
end

#[]=(key, value) ⇒ Object



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

def []=(key, value)
  headers[key] = value
end

#run(connection, request_method) ⇒ Object



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

def run(connection, request_method)
  app = lambda { |env|
    response = Response.new
    response.finish(env) unless env[:parallel_manager]
    env[:response] = response
  }
  env = to_env_hash(connection, request_method)
  connection.builder.to_app(app).call(env)
end

#to_env_hash(connection, request_method) ⇒ Object

ENV Keys :method - a symbolized request method (:get, :post) :body - the request body that will eventually be converted to a string. :url - Addressable::URI instance of the URI for the current request. :status - HTTP response status code :request_headers - hash of HTTP Headers to be sent to the server :response_headers - Hash of HTTP headers from the server :parallel_manager - sent if the connection is in parallel mode :request - Hash of options for configuring the request.

:timeout      - open/read timeout Integer in seconds
:open_timeout - read timeout Integer in seconds
:proxy        - Hash of proxy options
  :uri        - Proxy Server URI
  :user       - Proxy server username
  :password   - Proxy server password

:ssl - Hash of options for configuring SSL requests.



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

def to_env_hash(connection, request_method)
  env_headers = connection.headers.dup
  env_params  = connection.params.dup
  connection.merge_headers(env_headers, headers)
  connection.merge_params(env_params,  params)

  { :method           => request_method,
    :body             => body,
    :url              => connection.build_url(path, env_params),
    :request_headers  => env_headers,
    :parallel_manager => connection.parallel_manager,
    :request          => connection.options.merge(:proxy => connection.proxy),
    :ssl              => connection.ssl}
end

#url(path, params = {}) ⇒ Object



37
38
39
40
# File 'lib/faraday/request.rb', line 37

def url(path, params = {})
  self.path   = path
  self.params = params
end