Class: Weary::Request

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Yields:

  • (_self)

Yield Parameters:



24
25
26
27
28
# File 'lib/weary/request.rb', line 24

def initialize(url, method='GET')
  self.uri = url
  self.method = method
  yield self if block_given?
end

Instance Attribute Details

#uriObject

Returns the value of attribute uri



22
23
24
# File 'lib/weary/request.rb', line 22

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.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/weary/request.rb', line 42

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

#envObject

Build a Rack environment representing this Request.



56
57
58
# File 'lib/weary/request.rb', line 56

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

#methodObject

The HTTP request method for this Request.



61
62
63
# File 'lib/weary/request.rb', line 61

def method
  @method
end

#method=(verb) ⇒ Object

Set and normalize the HTTP request method.



66
67
68
# File 'lib/weary/request.rb', line 66

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
    options = {:consumer_key => consumer_key}
    options[:token] = access_token unless access_token.nil? || access_token.empty?
    options[:token_secret] = token_secret unless token_secret.nil? || token_secret.empty?
    options[:consumer_secret] = consumer_secret unless consumer_secret.nil? || consumer_secret.empty?
    use Weary::Middleware::OAuth, [options]
  end
  @oauth
end

#params(parameters = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/weary/request.rb', line 70

def params(parameters=nil)
  if !parameters.nil?
    @body = query_params_from_hash(parameters)
    if ["POST", "PUT"].include? method
      body stringio_encode(@body)
      use Weary::Middleware::ContentType
    else
      uri.query = @body
    end
  end
  @body
end

#performObject

Returns a future-wrapped Response.



115
116
117
118
119
120
121
122
# File 'lib/weary/request.rb', line 115

def perform
  future do
    status, headers, body = call(rack_env_defaults)
    response = Weary::Response.new body, status, headers
    yield response if block_given?
    response
  end
end