Class: Unify::HTTP
- Inherits:
-
Object
- Object
- Unify::HTTP
- Defined in:
- lib/unify/http.rb
Instance Method Summary collapse
- #_(path, options = {}, &block) ⇒ Object
- #call(env) ⇒ Object
-
#initialize(&block) ⇒ HTTP
constructor
A new instance of HTTP.
Constructor Details
#initialize(&block) ⇒ HTTP
Returns a new instance of HTTP.
9 10 11 12 13 14 15 16 17 |
# File 'lib/unify/http.rb', line 9 def initialize(&block) @path_stack = [] @routes = {} if block_given? instance_eval(&block) end @finished = true self end |
Instance Method Details
#_(path, options = {}, &block) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/unify/http.rb', line 19 def _(path, = {}, &block) return if @finished = { :method => 'GET' }.merge!() case path when Regexp @path_stack.push(path.source) when String @path_stack.push(Regexp.escape(path)) end @routes[[:method]] ||= [] @routes[[:method]] << [/^#{@path_stack.join('/').squeeze('/')}$/ix, block] if block_given? instance_eval(&block) end @path_stack.pop end |
#call(env) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/unify/http.rb', line 36 def call(env) if @routes[env['REQUEST_METHOD']] for route in @routes[env['REQUEST_METHOD']] if match = route[0].match(env['PATH_INFO']) @response = Rack::Response.new unless @response.write(route[1].call(*match.captures)) @response = nil end break end end end unless @response @response = Rack::Response.new('Page Not Found') @response.status = 404 end @response.finish end |