class Trialday::Router
Public Class Methods
new(request)
click to toggle source
# File lib/trialday/router.rb, line 20 def initialize(request) @request = request end
route(path, verb, &block)
click to toggle source
# File lib/trialday/router.rb, line 48 def route(path, verb, &block) @@routes << {"path"=>path, "body"=>block, "verb"=>"GET"} end
Public Instance Methods
action()
click to toggle source
# File lib/trialday/router.rb, line 39 def action '/'+ route_info[:resource] end
route!()
click to toggle source
# 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_list()
click to toggle source
# File lib/trialday/router.rb, line 43 def routes_list @@routes end
Private Instance Methods
add_route_info_to_request_params!()
click to toggle source
# File lib/trialday/router.rb, line 70 def add_route_info_to_request_params! @request.params.merge!(route_info) end
find_id_and_action(fragment)
click to toggle source
# File lib/trialday/router.rb, line 66 def find_id_and_action(fragment) @request.get? ? :index : :create end
not_found(msg = "Route not found.")
click to toggle source
# File lib/trialday/router.rb, line 55 def not_found(msg = "Route not found.") [404, { "Content-Type" => "text/plain" }, [msg]] end
path_fragments()
click to toggle source
# File lib/trialday/router.rb, line 74 def path_fragments @fragments ||= @request.path.split("/").reject { |s| s.empty? } end
route_info()
click to toggle source
# File lib/trialday/router.rb, line 59 def route_info @route_info ||= begin resource = path_fragments[0] { resource: resource } end end