Module: Nephos::Router
- Defined in:
- lib/nephos-server/router/load.rb,
lib/nephos-server/router/main.rb
Constant Summary collapse
- ROUTES =
[]
Class Method Summary collapse
- .add(what, verb) ⇒ Object
- .add_params!(what) ⇒ Object
- .check!(what) ⇒ Object
- .check_controller!(what) ⇒ Object
- .check_keys!(what) ⇒ Object
- .check_method!(what, instance) ⇒ Object
- .execute(env) ⇒ Object
- .parse_env(env, route) ⇒ Object
-
.parse_path(path, verb) ⇒ Object
find the right route to use from the url.
-
.render(arg) ⇒ Object
shortcut to #Nephos::Responder.render.
Class Method Details
.add(what, verb) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/nephos-server/router/load.rb', line 4 def self.add(what, verb) Nephos::Router::ROUTES << what.merge(verb: verb) display = "[#{verb}] #{what[:url]} \t ---> \t #{what[:controller]}##{what[:method]}" puts display unless what[:silent] return display end |
.add_params!(what) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/nephos-server/router/load.rb', line 11 def self.add_params!(what) params = what[:url].split('/').map do |p| p.match(/:\w+/) ? {p: "[[:graph:]]+", name: p} : {p: p, name: nil} end url = params.map{|e| e[:p]}.join("/") url = "/" if url.empty? what[:match] = /^#{url}$/ what[:params] = params.map{|e| e[:name] && e[:name][1..-1]}[1..-1] || [] end |
.check!(what) ⇒ Object
50 51 52 53 54 |
# File 'lib/nephos-server/router/load.rb', line 50 def self.check!(what) check_keys! what instance = check_controller! what check_method! what, instance end |
.check_controller!(what) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/nephos-server/router/load.rb', line 27 def self.check_controller! what begin controller = Module.const_get(what[:controller]) rescue => err raise InvalidRouteController, "Controller \"#{what[:controller]}\" doesn't exists" end if not controller.ancestors.include? Nephos::Controller raise InvalidRouteController, "Class \"#{what[:controller]}\" is not a Nephos::Controller" end begin instance = controller.new rescue => err raise InvalidRouteController, "Cannot initialize controller" end return instance end |
.check_keys!(what) ⇒ Object
21 22 23 24 25 |
# File 'lib/nephos-server/router/load.rb', line 21 def self.check_keys! what raise InvalidRouteUrl, "Missing URL" unless what.keys.include? :url raise InvalidRouteController, "Missing Controller" unless what.keys.include? :controller raise InvalidRouteMethod, "Missing Method" unless what.keys.include? :method end |
.check_method!(what, instance) ⇒ Object
44 45 46 47 48 |
# File 'lib/nephos-server/router/load.rb', line 44 def self.check_method! what, instance if not instance.respond_to? what[:method] raise InvalidRouteMethod, "No method named \"#{what[:method]}\"" end end |
.execute(env) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/nephos-server/router/main.rb', line 32 def self.execute(env) begin route = URI.parse(env['REQUEST_URI']) rescue => err puts "uri err #{err.}".red return render(status: 500) end parsed = parse_env(env, route) puts "#{parsed[:from]} [#{parsed[:verb]}] \t ---> \t #{route}" call = parse_path(parsed[:path], parsed[:verb]) return render(status: 404) if call.nil? begin controller = Module.const_get(call[:controller]).new(env, parsed, call) return render(controller.send(call[:method]) || {status: 500}) rescue => err return render(status: 500) end end |
.parse_env(env, route) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/nephos-server/router/main.rb', line 24 def self.parse_env(env, route) verb = env["REQUEST_METHOD"] from = env["REMOTE_ADDR"] path = route.path.split("/").select{|e|not e.to_s.empty?} args = Hash[route.query.to_s.split("&").map{|e| e.split("=")}] return {route: route, verb: verb, from: from, path: path, args: args} end |
.parse_path(path, verb) ⇒ Object
find the right route to use from the url
19 20 21 22 |
# File 'lib/nephos-server/router/main.rb', line 19 def self.parse_path path, verb route = File.join(["/"] + path) return ROUTES.find{|e| e[:match] =~ route and e[:verb] == verb} end |
.render(arg) ⇒ Object
shortcut to #Nephos::Responder.render
13 14 15 |
# File 'lib/nephos-server/router/main.rb', line 13 def self.render arg Responder.render arg end |