Class: Weary::Request
- Inherits:
-
Object
- Object
- Weary::Request
- Includes:
- Requestable
- Defined in:
- lib/weary/request.rb
Overview
A Request is an interface to an http request. It doesn’t actually make the request. Instead, it builds itself up into a Rack environment. A Request object is technically a Rack middleware, with a call method. The Request manipulates the passed in environment, then passes on to the adapter: A Rack application. Because the Request performs so much manipulation on the Rack env, you can attach middleware to it to act on its mutated env.
Instance Attribute Summary collapse
-
#uri ⇒ Object
Returns the value of attribute uri.
Instance Method Summary collapse
- #basic_auth(*credentials) ⇒ Object
- #body(io = nil) ⇒ Object
-
#call(environment) ⇒ Object
A Rack interface for the Request.
-
#env ⇒ Object
Build a Rack environment representing this Request.
-
#initialize(url, method = 'GET') {|_self| ... } ⇒ Request
constructor
A new instance of Request.
- #json(parameters) ⇒ Object
-
#method ⇒ Object
The HTTP request method for this Request.
-
#method=(verb) ⇒ Object
Set and normalize the HTTP request method.
- #oauth(consumer_key = nil, access_token = nil, token_secret = nil, consumer_secret = nil) ⇒ Object
- #params(parameters = nil) ⇒ Object
-
#perform ⇒ Object
Returns a future-wrapped Response.
Methods included from Requestable
#adapter, #has_middleware?, #headers, #pass_values_onto_requestable, #use, #user_agent
Constructor Details
#initialize(url, method = 'GET') {|_self| ... } ⇒ Request
Returns a new instance of Request.
23 24 25 26 27 |
# File 'lib/weary/request.rb', line 23 def initialize(url, method='GET') self.uri = url self.method = method yield self if block_given? end |
Instance Attribute Details
#uri ⇒ Object
Returns the value of attribute uri.
21 22 23 |
# File 'lib/weary/request.rb', line 21 def uri @uri end |
Instance Method Details
#basic_auth(*credentials) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/weary/request.rb', line 94 def basic_auth(*credentials) if !credentials.empty? @basic_auth = true use Weary::Middleware::BasicAuth, credentials end @basic_auth end |
#body(io = nil) ⇒ Object
89 90 91 92 |
# File 'lib/weary/request.rb', line 89 def body(io=nil) @attachment = io unless io.nil? @attachment ||= stringio_encode("") end |
#call(environment) ⇒ Object
A Rack interface for the Request. Applies itself and whatever middlewares to the env and passes the new env into the adapter.
environment - A Hash for the Rack env.
Returns an Array of three items; a Rack tuple.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/weary/request.rb', line 41 def call(environment) app = adapter.new middlewares = @middlewares || [] stack = Rack::Builder.new do middlewares.each do |middleware| klass, *args = middleware use klass, *args[0...-1].flatten, &args.last end run app end stack.call rack_env_defaults.merge(environment.update(env)) end |
#env ⇒ Object
Build a Rack environment representing this Request.
55 56 57 |
# File 'lib/weary/request.rb', line 55 def env Weary::Env.new(self).env end |
#json(parameters) ⇒ Object
83 84 85 86 87 |
# File 'lib/weary/request.rb', line 83 def json(parameters) json = MultiJson.encode(parameters) body stringio_encode(json) json end |
#method ⇒ Object
The HTTP request method for this Request.
60 61 62 |
# File 'lib/weary/request.rb', line 60 def method @method end |
#method=(verb) ⇒ Object
Set and normalize the HTTP request method.
65 66 67 |
# File 'lib/weary/request.rb', line 65 def method=(verb) @method = verb.to_s.upcase end |
#oauth(consumer_key = nil, access_token = nil, token_secret = nil, consumer_secret = nil) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/weary/request.rb', line 102 def oauth(consumer_key=nil, access_token=nil, token_secret=nil, consumer_secret=nil) if !consumer_key.nil? @oauth = true = {:consumer_key => consumer_key} [:token] = access_token unless access_token.nil? || access_token.empty? [:token_secret] = token_secret unless token_secret.nil? || token_secret.empty? [:consumer_secret] = consumer_secret unless consumer_secret.nil? || consumer_secret.empty? use Weary::Middleware::OAuth, [] end @oauth end |
#params(parameters = nil) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/weary/request.rb', line 69 def params(parameters=nil) if !parameters.nil? if ["POST", "PUT"].include? method @body = query_params_from_hash(parameters) body StringIO.new(@body) use Weary::Middleware::ContentType else uri.query_values = parameters @body = uri.query end end @body end |