Sha256: fb4d73c83f493a807c263b81a84f78604a61ff6a8f129156f91ca865d31a0214

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 KB

Contents

require 'forwardable'
require 'rack/contrib/cookies'

module NYNY
  class RequestScope
    extend Forwardable

    attr_reader :request, :response
    def_delegators :request, :session, :params
    def_delegators :response, :headers

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

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

    def status code
      response.status = code
    end

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

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

    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
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
nyny-3.4.3 lib/nyny/request_scope.rb
nyny-3.4.2 lib/nyny/request_scope.rb
nyny-3.4.1 lib/nyny/request_scope.rb
nyny-3.4.0 lib/nyny/request_scope.rb
nyny-3.3.1 lib/nyny/request_scope.rb