nephos-server 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +0 -2
- data/lib/nephos-server/routing/controller.rb +2 -2
- data/lib/nephos-server/routing/execute.rb +2 -2
- data/lib/nephos-server/routing/load.rb +3 -9
- data/lib/nephos-server/server/basic_errors.rb +6 -0
- data/lib/nephos-server/server/main.rb +1 -1
- data/lib/nephos-server/server/responder.rb +64 -19
- data/version +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 47e385b6e019a466c3af125cbb247264e2fcd452
|
4
|
+
data.tar.gz: d6f6a10f417bf70a4178d61a1322d6d04a3d9596
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6be74ee7209d60848fa64ec901320fddcdf1e11bd8f24a22ca789a85df9e97e20cd4cfcb869bafac0e0bd693d68b31a369b7119e9a9b1633fb4450009fc89cfa
|
7
|
+
data.tar.gz: c899551d96c76d48d94606d32b892093e35e6bcaca6d32efd028db49ac51a260ceefa68c57b50d79a8fb97db118ac57c5f6ef17b6a6ed223c579009311dd656c
|
data/README.md
CHANGED
@@ -3,8 +3,8 @@ module Nephos
|
|
3
3
|
|
4
4
|
attr_reader :env, :infos
|
5
5
|
|
6
|
-
# @
|
7
|
-
# @
|
6
|
+
# @param env [Hash] env extracted from the http request
|
7
|
+
# @param parsed [Hash] pre-parsed env with parameters, ...
|
8
8
|
def initialize env, parsed
|
9
9
|
@env= env
|
10
10
|
@infos= parsed
|
@@ -10,13 +10,13 @@ module Nephos
|
|
10
10
|
|
11
11
|
ALL = []
|
12
12
|
|
13
|
-
# @
|
13
|
+
# @param arg [Hash or Symbol]
|
14
14
|
# shortcut to #{Nephos::Responder.render}
|
15
15
|
def self.render arg
|
16
16
|
Responder.render arg
|
17
17
|
end
|
18
18
|
|
19
|
-
# @
|
19
|
+
# @param path [Array]
|
20
20
|
# find the right route to use from the url
|
21
21
|
def self.parse_path path, verb
|
22
22
|
route = File.join(["/"] + path)
|
@@ -1,9 +1,3 @@
|
|
1
|
-
class RoutingError < StandardError; end
|
2
|
-
class InvalidRoute < RoutingError; end
|
3
|
-
class InvalidRouteUrl < InvalidRoute; end
|
4
|
-
class InvalidRouteController < InvalidRoute; end
|
5
|
-
class InvalidRouteMethod < InvalidRoute; end
|
6
|
-
|
7
1
|
module Nephos
|
8
2
|
module Route
|
9
3
|
|
@@ -37,7 +31,7 @@ def route_prefix
|
|
37
31
|
File.join(["/"] + @route_prefix)
|
38
32
|
end
|
39
33
|
|
40
|
-
# @
|
34
|
+
# @param what [Hash]
|
41
35
|
def get what
|
42
36
|
raise InvalidRoute unless what.is_a? Hash
|
43
37
|
what[:url] = File.expand_path File.join(route_prefix, what[:url])
|
@@ -45,7 +39,7 @@ def get what
|
|
45
39
|
Nephos::Route.add(what, "GET")
|
46
40
|
end
|
47
41
|
|
48
|
-
# @
|
42
|
+
# @param what [Hash]
|
49
43
|
def post what
|
50
44
|
raise InvalidRoute unless what.is_a? Hash
|
51
45
|
what[:url] = File.join(route_prefix, what[:url])
|
@@ -53,7 +47,7 @@ def post what
|
|
53
47
|
Nephos::Route.add(what, "POST")
|
54
48
|
end
|
55
49
|
|
56
|
-
# @
|
50
|
+
# @param what [Hash]
|
57
51
|
def put what
|
58
52
|
raise InvalidRoute unless what.is_a? Hash
|
59
53
|
what[:url] = File.join(route_prefix, what[:url])
|
@@ -1 +1,7 @@
|
|
1
|
+
class RoutingError < StandardError; end
|
2
|
+
class InvalidRoute < RoutingError; end
|
3
|
+
class InvalidRouteUrl < InvalidRoute; end
|
4
|
+
class InvalidRouteController < InvalidRoute; end
|
5
|
+
class InvalidRouteMethod < InvalidRoute; end
|
6
|
+
|
1
7
|
class InvalidApplicationError < RuntimeError; end
|
@@ -1,29 +1,74 @@
|
|
1
1
|
module Nephos
|
2
2
|
module Responder
|
3
3
|
|
4
|
-
|
5
|
-
CT_TP = {'Content-type' => 'text/plain' + CT_CHARSET_}
|
6
|
-
CT_TJ = {'Content-type' => 'text/javascript' + CT_CHARSET_}
|
7
|
-
CT_TH = {'Content-type' => 'text/html' + CT_CHARSET_}
|
4
|
+
class InvalidContentType < StandardError; end
|
8
5
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
6
|
+
CT_CHARSET_PREFIX = '; charset='
|
7
|
+
|
8
|
+
def self.content_type(kind, type, charset='UTF-8')
|
9
|
+
{'Content-type' => "#{kind}/#{type}" + CT_CHARSET_PREFIX + charset}
|
10
|
+
end
|
11
|
+
def self.ct_plain
|
12
|
+
content_type(:text, :plain)
|
13
|
+
end
|
14
|
+
def self.ct_html
|
15
|
+
content_type(:text, :html)
|
16
|
+
end
|
17
|
+
def self.ct_json
|
18
|
+
content_type(:text, :javascript)
|
19
|
+
end
|
20
|
+
# @param params [Hash] containing :type => "kind/type", example: "text/html"
|
21
|
+
def self.ct_specific(params)
|
22
|
+
kind, type = params[:type].match(/^(\w+)\/(\w+)$/)[1..2]
|
23
|
+
if kind.nil? or type.nil?
|
24
|
+
raise InvalidContentType, "params[:type] must match with \"kind/type\""
|
25
|
+
end
|
26
|
+
content_type(kind, type)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Fill params with default parameters (status, plain errors)
|
30
|
+
def self.set_default_params params
|
31
|
+
if (params.keys & [:status]).empty?
|
32
|
+
params[:status] ||= 200
|
33
|
+
end
|
34
|
+
if (params.keys & [:plain, :html, :json, :type]).empty?
|
35
|
+
if params[:status].to_s.match(/^[345]\d\d$/)
|
36
|
+
params[:plain] ||= "Error: #{params[:status]} code"
|
37
|
+
else
|
38
|
+
params[:plain] ||= "ok"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
params
|
42
|
+
end
|
43
|
+
|
44
|
+
# @return [Symbol, nil] :plain, :html, :json, or nil
|
45
|
+
# search the content type
|
46
|
+
def self.params_content_type params
|
47
|
+
(params.keys & [:plain, :html, :json]).first
|
48
|
+
end
|
49
|
+
|
50
|
+
# search the content to render from the params,
|
51
|
+
# based on content_type (plain, html, ...).
|
52
|
+
# If not, check for specific type
|
53
|
+
def self.params_content_type_value params
|
54
|
+
type = params_content_type(params)
|
55
|
+
if type
|
56
|
+
self.send("ct_#{type}")
|
23
57
|
else
|
24
|
-
|
58
|
+
self.send("ct_specific", params)
|
25
59
|
end
|
26
60
|
end
|
27
61
|
|
62
|
+
# @param params [Hash, Symbol]
|
63
|
+
def self.render params
|
64
|
+
return [204, plain(), [""]] if params == :empty
|
65
|
+
params = set_default_params(params)
|
66
|
+
return [
|
67
|
+
params[:status],
|
68
|
+
params_content_type_value(params),
|
69
|
+
[params[params_content_type(params) || :content]],
|
70
|
+
]
|
71
|
+
end
|
72
|
+
|
28
73
|
end
|
29
74
|
end
|
data/version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.8
|