Class: Weary::Route
- Inherits:
-
Object
- Object
- Weary::Route
- Defined in:
- lib/weary/route.rb
Constant Summary collapse
- NotFoundError =
Class.new(StandardError)
- NotAllowedError =
Class.new(StandardError)
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(resources, domain = "") ⇒ Route
constructor
A new instance of Route.
- #route(request) ⇒ Object
Constructor Details
#initialize(resources, domain = "") ⇒ Route
Returns a new instance of Route.
8 9 10 11 |
# File 'lib/weary/route.rb', line 8 def initialize(resources, domain="") @resources = resources @domain = domain end |
Instance Method Details
#call(env) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/weary/route.rb', line 13 def call(env) begin request = Rack::Request.new(env) resource = route(request) url_variables = resource.url.extract("#{@domain}#{request.path}") resource.request(url_variables.merge(request.params)).call(env) rescue NotFoundError => e [404, {'Content-Type' => "text/plain"}, [e.]] rescue NotAllowedError => e [405, {'Content-Type' => "text/plain"}, [e.]] rescue Weary::Resource::UnmetRequirementsError => e [403, {'Content-Type' => "text/plain"}, [e.]] rescue Exception => e [500, {'Content-Type' => "text/plain"}, [e.]] end end |
#route(request) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/weary/route.rb', line 30 def route(request) subset = select_by_url("#{@domain}#{request.path}") raise NotFoundError, "Not Found" if subset.empty? subset = select_by_method(request.request_method, subset) raise NotAllowedError, "Method Not Allowed" if subset.empty? subset.first end |