nephos-server 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0198ab59046527552ad7b7f66c25dcd04ca3727f
4
- data.tar.gz: aeb7527f299c85860b4941a528c15635ed6024a8
3
+ metadata.gz: c470980b6da991d95c060efa740e0a89cc82eb30
4
+ data.tar.gz: d7236c0810fd2c019e5d1a180b63270d950e0a06
5
5
  SHA512:
6
- metadata.gz: b6aee265e6d6fb09239476dcb545c6ea282fea41ef548432cd28134d94bff3fdf6698399a933a8eca2411f30b08ae0452c9ec00301e8be989682d4feaa61b118
7
- data.tar.gz: 66fc08b9fe7a964d87e34eb8e4113f0aa2d68eedc3127ef4f0374ff8a24fa6e5b96be526bd7ab78448fe87b39489e13bf68300b1c7818a1d4f38dd801b798ef6
6
+ metadata.gz: d9d1b932659d821538bc1dd5d706cd9581fb19e6ccc9ab688d96cd745e9be9d2c80b6c6cb39c6bd34209b74d817d93d10f2fabeac6c1cfe7cdd10e68541e3a6a
7
+ data.tar.gz: a267ce338cc75df3ca25e91e71dd29473bb952dccb6244ed99ccaf5dfccd35d80bcb2c15a492225506192d16e336890b0dfad48882e4b2093e808b45d8169fb8
@@ -3,6 +3,8 @@ module Nephos
3
3
 
4
4
  attr_reader :env, :infos
5
5
 
6
+ # @params: env [Hash] env extracted from the http request
7
+ # @params: parsed [Hash] pre-parsed env with parameters, ...
6
8
  def initialize env, parsed
7
9
  @env= env
8
10
  @infos= parsed
@@ -10,13 +10,17 @@ module Nephos
10
10
 
11
11
  ALL = []
12
12
 
13
+ # @params: arg [Hash or Symbol]
14
+ # shortcut to #{Nephos::Responder.render}
13
15
  def self.render arg
14
16
  Responder.render arg
15
17
  end
16
18
 
17
- def self.parse_path path
19
+ # @params: path [Array]
20
+ # find the right route to use from the url
21
+ def self.parse_path path, verb
18
22
  route = "/" + path.join("/")
19
- return ALL.find{|e| e[:url] == route}
23
+ return ALL.find{|e| e[:url] == route and e[:verb] == verb}
20
24
  end
21
25
 
22
26
  def self.execute(env)
@@ -27,9 +31,8 @@ module Nephos
27
31
  args = Hash[route.query.to_s.split("&").map{|e| e.split("=")}]
28
32
  puts "#{from} [#{verb}] \t ---> \t #{route}"
29
33
  parsed = {route: route, verb: verb, from: from, path: path, args: args}
30
- call = parse_path(path)
34
+ call = parse_path(path, verb)
31
35
  return render status: 404 if call.nil?
32
- call = parse_path(path)
33
36
  begin
34
37
  controller = Module.const_get(call[:controller]).new(env, parsed)
35
38
  return render(controller.send(call[:method]) || {status: 500})
@@ -2,17 +2,41 @@ class RoutingError < Exception; end
2
2
  class InvalidRoute < RoutingError; end
3
3
  class InvalidGetRoute < InvalidRoute; end
4
4
 
5
- # @param: what [Hash]
5
+ module Nephos
6
+ module Route
7
+
8
+ def self.add(what, verb)
9
+ Nephos::Route::ALL << what.merge(verb: verb)
10
+ puts "[#{verb}] #{what[:url]} \t ---> \t #{what[:controller]}##{what[:method]}"
11
+ end
12
+
13
+ def self.check!(what)
14
+ raise InvalidGetRoute if not what.keys.include? :url
15
+ raise InvalidGetRoute if not what.keys.include? :controller
16
+ raise InvalidGetRoute if not what.keys.include? :method
17
+ # TODO: more check to do
18
+ end
19
+
20
+ end
21
+ end
22
+
23
+ # @params: what [Hash]
6
24
  def get what
7
- raise InvalidGetRoute if not what.keys.include? :url
8
- raise InvalidGetRoute if not what.keys.include? :controller
9
- raise InvalidGetRoute if not what.keys.include? :method
25
+ Nephos::Route.check!(what)
26
+ Nephos::Route.add(what, "GET")
27
+ end
10
28
 
11
- # TODO: more check to do
29
+ # @params: what [Hash]
30
+ def post what
31
+ Nephos::Route.check!(what)
32
+ Nephos::Route.add(what, "POST")
33
+ end
12
34
 
13
- Nephos::Route::ALL << what
14
- puts "get route: #{what}"
35
+ # @params: what [Hash]
36
+ def put what
37
+ Nephos::Route.check!(what)
38
+ Nephos::Route.add(what, "PUT")
15
39
  end
16
40
 
17
- # require_relative '../../../routes.rb'
18
41
  load 'routes.rb'
42
+ puts
@@ -0,0 +1 @@
1
+ class InvalidApplicationError < RuntimeError ; end
@@ -7,14 +7,19 @@ module Nephos
7
7
 
8
8
  SERVER = lambda {|env| return Route.execute(env)}
9
9
 
10
+ attr_accessor :port
11
+
12
+ # @params: port [Integer] port to listen
10
13
  def initialize port
11
- @port = port
14
+ @port = Integer(port)
12
15
  end
13
16
 
17
+ # start the Rack server
14
18
  def start
15
19
  Rack::Server.start :app => SERVER, :Port => @port
16
20
  end
17
21
 
22
+ # start the Rack server on a instance of Nephos::Server
18
23
  def self.start port
19
24
  Server.new(port).start
20
25
  end
@@ -5,6 +5,8 @@ module Nephos
5
5
  CT_TP = {'Content-type' => 'text/plain' + CT_CHARSET_}
6
6
  CT_TJ = {'Content-type' => 'text/javascript' + CT_CHARSET_}
7
7
  CT_TH = {'Content-type' => 'text/html' + CT_CHARSET_}
8
+
9
+ # @params: params [Hash, Symbol]
8
10
  def self.render params
9
11
  if params == :empty
10
12
  return [204, CT_TP, [""]]
@@ -12,6 +12,7 @@ Gem::Specification.new do |s|
12
12
  'lib/nephos-server.rb',
13
13
  'lib/nephos-server/server/main.rb',
14
14
  'lib/nephos-server/server/responder.rb',
15
+ 'lib/nephos-server/server/basic_errors.rb',
15
16
  'lib/nephos-server/routing/execute.rb',
16
17
  'lib/nephos-server/routing/load.rb',
17
18
  'lib/nephos-server/routing/controller.rb',
data/version CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nephos-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - poulet_a
@@ -45,6 +45,7 @@ email:
45
45
  - - lib/nephos-server.rb
46
46
  - lib/nephos-server/server/main.rb
47
47
  - lib/nephos-server/server/responder.rb
48
+ - lib/nephos-server/server/basic_errors.rb
48
49
  - lib/nephos-server/routing/execute.rb
49
50
  - lib/nephos-server/routing/load.rb
50
51
  - lib/nephos-server/routing/controller.rb
@@ -76,6 +77,7 @@ files:
76
77
  - lib/nephos-server/routing/controller.rb
77
78
  - lib/nephos-server/routing/execute.rb
78
79
  - lib/nephos-server/routing/load.rb
80
+ - lib/nephos-server/server/basic_errors.rb
79
81
  - lib/nephos-server/server/main.rb
80
82
  - lib/nephos-server/server/responder.rb
81
83
  - nephos-server.gemspec