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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b13edc23b32693d79a6ff436f805f2ac7a57d941
4
- data.tar.gz: a25a361fe88d7763e4f7aee48a92cea306855197
3
+ metadata.gz: 47e385b6e019a466c3af125cbb247264e2fcd452
4
+ data.tar.gz: d6f6a10f417bf70a4178d61a1322d6d04a3d9596
5
5
  SHA512:
6
- metadata.gz: 66d81d163d29377255e3a5c866fdc4076e70e8d70d8276798152f03bee4900615b20010429c79831448c78597026ea5035bd1449babffeabebf517408881bff3
7
- data.tar.gz: f63d4ede65f21ad854d88b5d4ff02d50552be506c3e347a087eafb09339dca12d40f87325b433fee0cb42aab7c0a27e5b57958ee5826bb603a8aae6f6376a0ff
6
+ metadata.gz: 6be74ee7209d60848fa64ec901320fddcdf1e11bd8f24a22ca789a85df9e97e20cd4cfcb869bafac0e0bd693d68b31a369b7119e9a9b1633fb4450009fc89cfa
7
+ data.tar.gz: c899551d96c76d48d94606d32b892093e35e6bcaca6d32efd028db49ac51a260ceefa68c57b50d79a8fb97db118ac57c5f6ef17b6a6ed223c579009311dd656c
data/README.md CHANGED
@@ -15,8 +15,6 @@ This is a simple web server, based on rack and puma, with a minimal help:
15
15
 
16
16
  # TODO
17
17
 
18
- - Improve rendering
19
- - html support
20
18
  - Routing
21
19
  - improve get with arguments
22
20
  - add ressource (elarge urls)
@@ -3,8 +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
+ # @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
- # @params arg [Hash or Symbol]
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
- # @params path [Array]
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
- # @params what [Hash]
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
- # @params what [Hash]
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
- # @params what [Hash]
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
@@ -9,7 +9,7 @@ module Nephos
9
9
 
10
10
  attr_accessor :port
11
11
 
12
- # @params port [Integer] port to listen
12
+ # @param port [Integer] port to listen
13
13
  def initialize port
14
14
  @port = Integer(port)
15
15
  end
@@ -1,29 +1,74 @@
1
1
  module Nephos
2
2
  module Responder
3
3
 
4
- CT_CHARSET_ = '; charset=UTF-8'
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
- # @params params [Hash, Symbol]
10
- def self.render params
11
- if params == :empty
12
- return [204, CT_TP, [""]]
13
- elsif params[:status] == 404
14
- return [404, CT_TP, ['Error 404 : Not found !']]
15
- elsif params[:status] == 500
16
- return [500, CT_TP, ['Error 5OO : Internal Server Error !']]
17
- elsif params[:status].is_a? Fixnum
18
- return [params[:status], CT_TP, ["Error #{params[:status]}"]]
19
- elsif params[:json]
20
- return [200, CT_TJ, [params[:json].to_json]]
21
- elsif params[:plain]
22
- return [200, CT_TJ, [params[:plain].to_s]]
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
- render(:empty)
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.7
1
+ 0.1.8
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.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - poulet_a