Class: Alice::Request

Inherits:
Struct
  • Object
show all
Extended by:
AutoloadHelper
Defined in:
lib/alice/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['b']         = '2' # header
  req.body = 'abc'
end

Defined Under Namespace

Classes: ActiveSupportJson, Yajl

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


11
12
13
# File 'lib/alice/request.rb', line 11

def body
  @body
end

#headersObject

Returns the value of attribute headers

Returns:

  • (Object)

    the current value of headers


11
12
13
# File 'lib/alice/request.rb', line 11

def headers
  @headers
end

#paramsObject

Returns the value of attribute params

Returns:

  • (Object)

    the current value of params


11
12
13
# File 'lib/alice/request.rb', line 11

def params
  @params
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path


11
12
13
# File 'lib/alice/request.rb', line 11

def path
  @path
end

Class Method Details

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

Yields:

  • (req)

29
30
31
32
33
# File 'lib/alice/request.rb', line 29

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

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

Yields:

  • (req)

23
24
25
26
27
# File 'lib/alice/request.rb', line 23

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

Instance Method Details

#[](key) ⇒ Object


40
41
42
# File 'lib/alice/request.rb', line 40

def [](key)
  headers[key]
end

#[]=(key, value) ⇒ Object


44
45
46
# File 'lib/alice/request.rb', line 44

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

#run(connection, request_method) ⇒ Object


71
72
73
74
75
# File 'lib/alice/request.rb', line 71

def run(connection, request_method)
  app = connection.to_app
  env = to_env_hash(connection, request_method)
  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 :response - the actual response object that stores the rack response


57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/alice/request.rb', line 57

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,
    :response         => Response.new}
end

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


35
36
37
38
# File 'lib/alice/request.rb', line 35

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