Sha256: e2bf5f76c7cdf88213ca23d3064a6cb693bec128a3a177bd1e33676958087952

Contents?: true

Size: 966 Bytes

Versions: 1

Compression:

Stored size: 966 Bytes

Contents

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

module NYNY
  class RequestScope
    extend Forwardable

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

    def initialize request
      @request  = request
      @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.status = status
      response.headers.merge! headers
      response.body = body
      throw :halt, response.finish
    end

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

    def apply_to &handler
      response.body = instance_eval(&handler)
      cookies.finish!(response)
      response.finish
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nyny-3.0.1 lib/nyny/request_scope.rb