Class: Trialday::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/trialday/router.rb

Constant Summary collapse

@@routes =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Router

Returns a new instance of Router.



20
21
22
# File 'lib/trialday/router.rb', line 20

def initialize(request)
  @request = request
end

Class Method Details

.route(path, verb, &block) ⇒ Object



48
49
50
# File 'lib/trialday/router.rb', line 48

def route(path, verb, &block)
  @@routes << {"path"=>path, "body"=>block, "verb"=>"GET"}
end

Instance Method Details

#actionObject



39
40
41
# File 'lib/trialday/router.rb', line 39

def action
  '/'+ route_info[:resource]
end

#route!Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/trialday/router.rb', line 24

def route!
  routes_list.each do |r|
    if action == r['path']
      verb = @request.get? ? 'GET' : 'POST'
      if verb == 'GET'
        return [200, { "Content-Type" => "application/json" }, [r['body'].call.to_json]]
      elsif verb == 'POST'
        return [200, { "Content-Type" => "application/json" }, [@request.body.read]]
      end
    end
  end

  not_found
end

#routes_listObject



43
44
45
# File 'lib/trialday/router.rb', line 43

def routes_list
  @@routes
end