Class: Weary::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/weary/route.rb

Constant Summary

NotFoundError =
Class.new(StandardError)
NotAllowedError =
Class.new(StandardError)

Instance Method Summary (collapse)

Constructor Details

- (Route) initialize(resources, domain = "")



8
9
10
11
# File 'lib/weary/route.rb', line 8

def initialize(resources, domain="")
  @resources = resources
  @domain = domain
end

Instance Method Details

- (Object) call(env)



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.message]]
  rescue NotAllowedError => e
    [405, {'Content-Type' => "text/plain"}, [e.message]]
  rescue Weary::Resource::UnmetRequirementsError => e
    [403, {'Content-Type' => "text/plain"}, [e.message]]
  rescue Exception => e
    [500, {'Content-Type' => "text/plain"}, [e.message]]
  end
end

- (Object) route(request)

Raises:



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