Sha256: c362d51d19b7fdec9715996392a339a0e15d9afd265c127aff5f94ec0bf4a244

Contents?: true

Size: 1.46 KB

Versions: 7

Compression:

Stored size: 1.46 KB

Contents

Dir[File.expand_path "app/*.rb"].each do |f|
  load(f) and puts "#{f} loaded"
end
puts

module Nephos
  module Router

    ROUTES = []

    # @param arg [Hash or Symbol]
    # shortcut to #{Nephos::Responder.render}
    def self.render arg
      Responder.render arg
    end

    # @param path [Array]
    # find the right route to use from the url
    def self.parse_path path, verb
      route = File.join(["/"] + path)
      return ROUTES.find{|e| e[:match] =~ route and e[:verb] == verb}
    end

    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

    def self.execute(env)
      begin
        route = URI.parse(env['REQUEST_URI'])
      rescue => err
        puts "uri err #{err.message}".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

  end
end

require_relative 'load'

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
nephos-server-0.4.3 lib/nephos-server/router/main.rb
nephos-server-0.4.2 lib/nephos-server/router/main.rb
nephos-server-0.4.1 lib/nephos-server/router/main.rb
nephos-server-0.4.0 lib/nephos-server/router/main.rb
nephos-server-0.3.2 lib/nephos-server/router/main.rb
nephos-server-0.3.1 lib/nephos-server/router/main.rb
nephos-server-0.2.4 lib/nephos-server/router/main.rb