Class: Usher::Interface::Rack

Inherits:
Object
  • Object
show all
Defined in:
lib/usher/interface/rack.rb

Defined Under Namespace

Classes: Builder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, &blk) ⇒ Rack

Returns a new instance of Rack.



41
42
43
44
45
# File 'lib/usher/interface/rack.rb', line 41

def initialize(app = nil, &blk)
  @app = app || lambda { |env| ::Rack::Response.new("No route found", 404).finish }
  @router = Usher.new(:request_methods => [:request_method, :host, :port, :scheme], :generator => Usher::Util::Generators::URL.new)
  instance_eval(&blk) if blk
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



39
40
41
# File 'lib/usher/interface/rack.rb', line 39

def app
  @app
end

#routerObject (readonly)

Returns the value of attribute router.



38
39
40
# File 'lib/usher/interface/rack.rb', line 38

def router
  @router
end

Instance Method Details

#add(path, options = nil) ⇒ Object



56
57
58
# File 'lib/usher/interface/rack.rb', line 56

def add(path, options = nil)
  @router.add_route(path, options)
end

#after_match(request, response) ⇒ Object

Allows a hook to be placed for sub classes to make use of between matching and calling the application



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/usher/interface/rack.rb', line 116

def after_match(request, response)
  params = response.path.route.default_values ?
    response.path.route.default_values.merge(Hash[*response.params.flatten]) :
    Hash[*response.params.flatten]
  
  request.env['usher.params'] ?
    request.env['usher.params'].merge!(params) :
    (request.env['usher.params'] = params)
  
  # consume the path_info to the script_name
  # response.remaining_path
  consume_path!(request, response) if response.partial_match?
end

#call(env) ⇒ Object



101
102
103
104
105
106
# File 'lib/usher/interface/rack.rb', line 101

def call(env)
  request = ::Rack::Request.new(env)
  response = @router.recognize(request, request.path_info)
  after_match(request, response) if response
  determine_respondant(response).call(env)
end

#consume_path!(request, response) ⇒ Object

Consume the path from path_info to script_name



148
149
150
151
# File 'lib/usher/interface/rack.rb', line 148

def consume_path!(request, response)
  request.env["SCRIPT_NAME"] = (request.env["SCRIPT_NAME"] + response.matched_path)   || ""
  request.env["PATH_INFO"] = response.remaining_path    || ""
end

#default(app = nil, &block) ⇒ Object

default { |env| … } default DefaultApp



62
63
64
# File 'lib/usher/interface/rack.rb', line 62

def default(app = nil, &block)
  @app = app ? app : block
end

#delete(path, options = {}) ⇒ Object



85
86
87
# File 'lib/usher/interface/rack.rb', line 85

def delete(path, options = {})
  self.add(path, options.merge!(:conditions => {:request_method => "DELETE"}))
end

#determine_respondant(response) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determines which application to respond with.

Within the request when determine respondant is called
If there is a matching route to an application, that
application is called, Otherwise the middleware application is called.


137
138
139
140
141
142
143
144
145
# File 'lib/usher/interface/rack.rb', line 137

def determine_respondant(response)
  unless response
    app
  else
    respondant = response.path.route.destination
    respondant = app unless respondant.respond_to?(:call)
    respondant
  end
end

#dupObject



47
48
49
50
51
52
53
54
# File 'lib/usher/interface/rack.rb', line 47

def dup
  new_one = super
  original = self
  new_one.instance_eval do
    @router = router.dup
  end
  new_one
end

#generate(route, options = nil) ⇒ Object



108
109
110
# File 'lib/usher/interface/rack.rb', line 108

def generate(route, options = nil)
  @router.generator.generate(route, options)
end

#get(path, options = {}) ⇒ Object

it returns route, and because you may want to work with the route, for example give it a name, we returns the route with GET request



73
74
75
# File 'lib/usher/interface/rack.rb', line 73

def get(path, options = {})
  self.add(path, options.merge!(:conditions => {:request_method => ["HEAD", "GET"]}))
end

#parent_routeObject



93
94
95
# File 'lib/usher/interface/rack.rb', line 93

def parent_route
  @router.parent_route
end

#parent_route=(route) ⇒ Object



89
90
91
# File 'lib/usher/interface/rack.rb', line 89

def parent_route=(route)
  @router.parent_route = route
end

#post(path, options = {}) ⇒ Object



77
78
79
# File 'lib/usher/interface/rack.rb', line 77

def post(path, options = {})
  self.add(path, options.merge!(:conditions => {:request_method => "POST"}))
end

#put(path, options = {}) ⇒ Object



81
82
83
# File 'lib/usher/interface/rack.rb', line 81

def put(path, options = {})
  self.add(path, options.merge!(:conditions => {:request_method => "PUT"}))
end

#reset!Object



97
98
99
# File 'lib/usher/interface/rack.rb', line 97

def reset!
  @router.reset!
end