Class: Refraction

Inherits:
Object
  • Object
show all
Defined in:
lib/refraction.rb,
lib/refraction/version.rb

Defined Under Namespace

Classes: Request

Constant Summary collapse

VERSION =
'0.2.1'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Refraction

Returns a new instance of Refraction.



93
94
95
# File 'lib/refraction.rb', line 93

def initialize(app)
  @app = app
end

Class Method Details

.configure(&block) ⇒ Object

class Request



85
86
87
# File 'lib/refraction.rb', line 85

def self.configure(&block)
  @rules = block
end

.rulesObject



89
90
91
# File 'lib/refraction.rb', line 89

def self.rules
  @rules
end

Instance Method Details

#call(env) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/refraction.rb', line 101

def call(env)
  return @app.call(env) unless rules

  request = Request.new(env)
  rules.call(request)

  case request.action
  when :permanent, :found, :respond
    request.response
  when :rewrite
    env["rack.url_scheme"]  = request.scheme
    env["HTTP_HOST"]        = request.http_host
    env["SERVER_NAME"]      = request.host
    env["HTTP_PORT"]        = request.port if request.port
    env["PATH_INFO"]        = request.path
    env["QUERY_STRING"]     = request.query
    env["REQUEST_URI"]      = request.fullpath
    @app.call(env)
  else
    @app.call(env)
  end
end

#rulesObject



97
98
99
# File 'lib/refraction.rb', line 97

def rules
  self.class.rules
end