Class: NYNY::RequestScope

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/nyny/request_scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ RequestScope

Returns a new instance of RequestScope.



12
13
14
15
16
# File 'lib/nyny/request_scope.rb', line 12

def initialize env
  @request  = Request.new(env)
  @request.params.merge! env['nyny.params']
  @response = Response.new [], 200, {'Content-Type' => 'text/html'}
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



8
9
10
# File 'lib/nyny/request_scope.rb', line 8

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



8
9
10
# File 'lib/nyny/request_scope.rb', line 8

def response
  @response
end

Instance Method Details

#apply_to(&handler) ⇒ Object



37
38
39
40
41
42
# File 'lib/nyny/request_scope.rb', line 37

def apply_to &handler
  data = instance_eval(&handler)
  data.respond_to?(:each) ? response.body = data : response.write(data)
  cookies.finish!(response) if @cookies
  response
end

#cookiesObject



18
19
20
# File 'lib/nyny/request_scope.rb', line 18

def cookies
  @cookies ||= Rack::Cookies::CookieJar.new(request.cookies)
end

#halt(status, headers = {}, body = '') ⇒ Object



26
27
28
29
30
# File 'lib/nyny/request_scope.rb', line 26

def halt status, headers={}, body=''
  @response = Response.new body, status, headers
  cookies.finish!(response) if @cookies
  throw :halt, response
end

#redirect_to(uri, status = 302) ⇒ Object Also known as: redirect



32
33
34
# File 'lib/nyny/request_scope.rb', line 32

def redirect_to uri, status=302
  halt status, {'Location' => uri}
end

#status(code) ⇒ Object



22
23
24
# File 'lib/nyny/request_scope.rb', line 22

def status code
  response.status = code
end