nephos-server 0.1.11 → 0.1.12

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: 505ec6c0b755cfae4d95193fe452246ecec7bb56
4
- data.tar.gz: aff6de3c8994e6fa3cfc85a19f60942a1d2a77cf
3
+ metadata.gz: 9ef589e2b56701d6cdac117476c0672b3cf0aced
4
+ data.tar.gz: 1dc8dbfcb20cdbb48ea3692731561136068dcd19
5
5
  SHA512:
6
- metadata.gz: 06aa251c55dd7ac4234aa5a2fdbc3ba428639c5a4bb3fcca97f6147ef6e96d31ed0ac4a062e58c6bff2201343b36b1f6028c4fdb91aaf05c2c1bcd2c15e51dcf
7
- data.tar.gz: 9bee705a410c42ff8765635f2bd12781715e72010fefc51190ef928c1f960a38816c8664c1d3c94df49700d4badedc8c22e97fa16231aa42f5162cbb9e1266ad
6
+ metadata.gz: a4a2e21ee155c0350c9c2727262fea00005bcfb9588b8b42ce1ba830290c783daff6e527afa748a34539761005ba6f01bd33393350bee6781826325e8ed237af
7
+ data.tar.gz: 773200898dc45b06b7c8cb4f82ab39a35539ae5b5698e4dbf1ceb7c773506633e5ca6c74cb9c16099ae22b5e7699d5c3034d6368b79e1c79b78d40a7b18c3c69
@@ -0,0 +1,42 @@
1
+ require 'open-uri'
2
+
3
+ module Dataset
4
+
5
+ $dataset = []
6
+ LOCK = Mutex.new
7
+
8
+ def self.start
9
+ Thread.new do
10
+ loop do
11
+ $dataset.each do |url|
12
+ print "Open #{url} "
13
+ begin
14
+ open(url)
15
+ puts "SUCCESS"
16
+ rescue => err
17
+ puts "FAILURE #{err.message}"
18
+ end
19
+ sleep 1
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ def self.<<(url)
26
+ if not $started
27
+ $started = true
28
+ start
29
+ end
30
+ LOCK.lock
31
+ $dataset << url
32
+ $dataset.uniq!
33
+ LOCK.unlock
34
+ end
35
+
36
+ def self.rm(url)
37
+ LOCK.lock
38
+ $dataset.remove url
39
+ LOCK.unlock
40
+ end
41
+
42
+ end
Binary file
@@ -0,0 +1,55 @@
1
+ class MainController < Nephos::Controller
2
+
3
+ def root
4
+ {
5
+ json: {
6
+ list: $dataset,
7
+ add: '/add',
8
+ rm: '/rm',
9
+ }
10
+ }
11
+ end
12
+
13
+ def add_url
14
+ url = arguments["url"]
15
+ if url
16
+ Dataset << url
17
+ return {plain: "#{url} added"}
18
+ else
19
+ return {plain: "url argument required"}
20
+ end
21
+ end
22
+
23
+ def rm_url
24
+ url = arguments[:url]
25
+ if url
26
+ Dataset.rm url
27
+ return {plain: "#{url} removed"}
28
+ else
29
+ return {plain: "url argument required"}
30
+ end
31
+ end
32
+
33
+ def hello
34
+ {html: "<html><body><h1>hello world</h1><p>lol</p></body></html>"}
35
+ end
36
+
37
+ AUTH_IMG_EXT = %w(.jpg .jpeg .png .gif)
38
+ def image
39
+ dir = File.expand_path('controllers/')
40
+ file = File.expand_path(params["image"], dir)
41
+ if not file[0..(dir.size-1)] == dir or not AUTH_IMG_EXT.include?(File.extname(file))
42
+ return {status: 500, content: "invalid path #{params['image']}"}
43
+ elsif not File.exists? file
44
+ return {status: 404, content: "invalid path #{params['image']}"}
45
+ else
46
+ return {type: 'image/jpeg', content: File.read(file)}
47
+ end
48
+ end
49
+
50
+ require 'pry'
51
+ def debug
52
+ binding.pry
53
+ end
54
+
55
+ end
@@ -23,6 +23,14 @@ module Nephos
23
23
  return ALL.find{|e| e[:match] =~ route and e[:verb] == verb}
24
24
  end
25
25
 
26
+ def self.parse_env(env, route)
27
+ verb = env["REQUEST_METHOD"]
28
+ from = env["REMOTE_ADDR"]
29
+ path = route.path.split("/").select{|e|not e.to_s.empty?}
30
+ args = Hash[route.query.to_s.split("&").map{|e| e.split("=")}]
31
+ return {route: route, verb: verb, from: from, path: path, args: args}
32
+ end
33
+
26
34
  def self.execute(env)
27
35
  begin
28
36
  route = URI.parse(env['REQUEST_URI'])
@@ -30,14 +38,10 @@ module Nephos
30
38
  puts "uri err #{err.message}".red
31
39
  return render(status: 500)
32
40
  end
33
- verb = env["REQUEST_METHOD"]
34
- from = env["REMOTE_ADDR"]
35
- path = route.path.split("/").select{|e|not e.to_s.empty?}
36
- args = Hash[route.query.to_s.split("&").map{|e| e.split("=")}]
37
- puts "#{from} [#{verb}] \t ---> \t #{route}"
38
- parsed = {route: route, verb: verb, from: from, path: path, args: args}
39
- call = parse_path(path, verb)
40
- return render status: 404 if call.nil?
41
+ parsed = parse_env(env, route)
42
+ puts "#{parsed[:from]} [#{parsed[:verb]}] \t ---> \t #{route}"
43
+ call = parse_path(parsed[:path], parsed[:verb])
44
+ return render(status: 404) if call.nil?
41
45
  begin
42
46
  controller = Module.const_get(call[:controller]).new(env, parsed, call)
43
47
  return render(controller.send(call[:method]) || {status: 500})
@@ -3,7 +3,9 @@ module Nephos
3
3
 
4
4
  def self.add(what, verb)
5
5
  Nephos::Route::ALL << what.merge(verb: verb)
6
- puts "[#{verb}] #{what[:url]} \t ---> \t #{what[:controller]}##{what[:method]}"
6
+ display = "[#{verb}] #{what[:url]} \t ---> \t #{what[:controller]}##{what[:method]}"
7
+ puts display unless what[:silent]
8
+ return display
7
9
  end
8
10
 
9
11
  def self.add_params!(what)
@@ -16,10 +18,13 @@ module Nephos
16
18
  what[:params] = params.map{|e| e[:name] && e[:name][1..-1]}[1..-1] || []
17
19
  end
18
20
 
19
- def self.check!(what)
21
+ def self.check_keys! what
20
22
  raise InvalidRouteUrl, "Missing URL" unless what.keys.include? :url
21
23
  raise InvalidRouteController, "Missing Controller" unless what.keys.include? :controller
22
24
  raise InvalidRouteMethod, "Missing Method" unless what.keys.include? :method
25
+ end
26
+
27
+ def self.check_controller! what
23
28
  begin
24
29
  controller = Module.const_get(what[:controller])
25
30
  rescue
@@ -28,9 +33,24 @@ module Nephos
28
33
  if not controller.ancestors.include? Nephos::Controller
29
34
  raise InvalidRouteController, "Class \"#{what[:controller]}\" is not a Nephos::Controller"
30
35
  end
31
- if not controller.new.respond_to? what[:method]
36
+ begin
37
+ instance = controller.new
38
+ rescue
39
+ raise InvalidRouteController, "Cannot initialize controller"
40
+ end
41
+ return instance
42
+ end
43
+
44
+ def self.check_method! what, instance
45
+ if not instance.respond_to? what[:method]
32
46
  raise InvalidRouteMethod, "No method named \"#{what[:method]}\""
33
- end rescue raise InvalidRouteController, "Cannot initialize controller"
47
+ end
48
+ end
49
+
50
+ def self.check!(what)
51
+ check_keys! what
52
+ instance = check_controller! what
53
+ check_method! what, instance
34
54
  end
35
55
 
36
56
  end
@@ -41,31 +61,27 @@ def route_prefix
41
61
  File.join(["/"] + @route_prefix)
42
62
  end
43
63
 
44
- # @param what [Hash]
45
- def get what
64
+ def add_route(what, verb)
46
65
  raise InvalidRoute unless what.is_a? Hash
47
66
  what[:url] = File.expand_path File.join(route_prefix, what[:url])
48
67
  Nephos::Route.check!(what)
49
68
  Nephos::Route.add_params!(what)
50
- Nephos::Route.add(what, "GET")
69
+ Nephos::Route.add(what, verb)
70
+ end
71
+
72
+ # @param what [Hash]
73
+ def get what
74
+ add_route what, "GET"
51
75
  end
52
76
 
53
77
  # @param what [Hash]
54
78
  def post what
55
- raise InvalidRoute unless what.is_a? Hash
56
- what[:url] = File.join(route_prefix, what[:url])
57
- Nephos::Route.check!(what)
58
- Nephos::Route.add_params!(what)
59
- Nephos::Route.add(what, "POST")
79
+ add_route what, "POST"
60
80
  end
61
81
 
62
82
  # @param what [Hash]
63
83
  def put what
64
- raise InvalidRoute unless what.is_a? Hash
65
- what[:url] = File.join(route_prefix, what[:url])
66
- Nephos::Route.check!(what)
67
- Nephos::Route.add_params!(what)
68
- Nephos::Route.add(what, "PUT")
84
+ add_route what, "PUT"
69
85
  end
70
86
 
71
87
  def resource(name, &block)
@@ -11,7 +11,7 @@ module Nephos
11
11
 
12
12
  # @param params [Hash] containing :type => "kind/type", example: "text/html"
13
13
  def self.ct_specific(params)
14
- kind, type = params[:type].match(/^(\w+)\/(\w+)$/)[1..2]
14
+ kind, type = params[:type].to_s.match(/^(\w+)\/(\w+)$/) && Regexp.last_match[1..2]
15
15
  if kind.nil? or type.nil?
16
16
  raise InvalidContentType, "params[:type] must match with \"kind/type\""
17
17
  end
@@ -27,14 +27,13 @@ module Nephos
27
27
  private
28
28
  # if not :status entry, set to 200
29
29
  def self.set_default_params_status params
30
- if (params.keys & [:status]).empty?
31
- params[:status] ||= 200
32
- end
30
+ params[:status] ||= 200
33
31
  end
34
32
 
35
33
  def self.set_default_params_type params
36
- return if not params[:type].nil?
37
- params[:type] = PRESET_CT[(params.keys & [:plain, :html, :json]).first || :plain]
34
+ if params[:type].nil?
35
+ params[:type] = PRESET_CT[(params.keys & [:plain, :html, :json]).first || :plain]
36
+ end
38
37
  params[:type] = ct_specific(params)
39
38
  end
40
39
 
@@ -65,7 +64,7 @@ module Nephos
65
64
 
66
65
  # @param params [Hash, Symbol]
67
66
  def self.render params
68
- return [204, plain(), [""]] if params == :empty
67
+ return [204, ct_specific({type: PRESET_CT[:plain]}), [""]] if params == :empty
69
68
  params = set_default_params(params)
70
69
  return [
71
70
  params[:status],
@@ -5,29 +5,34 @@ Gem::Specification.new do |s|
5
5
  s.summary = 'See Changelog'
6
6
  s.description = ' a minimalist server, based on rack/puma, with routing, rendering, and controllers. Designed for quick api.'
7
7
  s.authors = [
8
- 'poulet_a'
9
- ]
10
- s.email = 'poulet_a@epitech.eu',
11
- s.files = [
12
- 'lib/nephos-server.rb',
13
- 'lib/nephos-server/basic_errors.rb',
14
- 'lib/nephos-server/server/main.rb',
15
- 'lib/nephos-server/server/responder.rb',
16
- 'lib/nephos-server/routing/execute.rb',
17
- 'lib/nephos-server/routing/load.rb',
18
- 'lib/nephos-server/routing/controller.rb',
19
- 'routes.rb.example',
20
- 'README.md',
21
- 'Rakefile',
22
- 'Procfile',
23
- 'Gemfile',
24
- 'Gemfile.lock',
25
- 'nephos-server.gemspec',
26
- 'version',
27
- 'test/test.rb',
28
- 'bin/nephos-server',
29
- 'bin/nephos-generator'
8
+ 'poulet_a'
30
9
  ]
10
+ s.email = 'poulet_a@epitech.eu',
11
+ s.files = %w(
12
+ lib/nephos-server.rb
13
+ lib/nephos-server/basic_errors.rb
14
+ lib/nephos-server/server/main.rb
15
+ lib/nephos-server/server/responder.rb
16
+ lib/nephos-server/routing/execute.rb
17
+ lib/nephos-server/routing/load.rb
18
+ lib/nephos-server/routing/controller.rb
19
+ README.md
20
+ Rakefile
21
+ Procfile
22
+ Gemfile
23
+ Gemfile.lock
24
+ nephos-server.gemspec
25
+ version
26
+ test/test.rb
27
+ test/responder.rb
28
+ test/routing.rb
29
+ routes.rb
30
+ controllers/dataset.rb
31
+ controllers/image.jpg
32
+ controllers/main.rb
33
+ bin/nephos-server
34
+ bin/nephos-generator
35
+ )
31
36
  s.executables = ['nephos-server', 'nephos-generator']
32
37
  s.homepage = 'https://github.com/pouleta/NephosRubyServer'
33
38
  s.license = 'GNU/GPLv3'
data/routes.rb ADDED
@@ -0,0 +1,22 @@
1
+ resource "home" do
2
+ resource "/index" do
3
+ get url: "/", controller: "MainController", method: "root"
4
+ get url: "/add", controller: "MainController", method: "add_url"
5
+ get url: "/rm", controller: "MainController", method: "rm_url"
6
+ end
7
+ get url: "/", controller: "MainController", method: "root"
8
+ end
9
+
10
+ resource "homes" do
11
+ get url: "/", controller: "MainController", method: "root"
12
+ end
13
+
14
+ get url: "/", controller: "MainController", method: "root"
15
+ get url: "/add", controller: "MainController", method: "add_url"
16
+ get url: "/rm", controller: "MainController", method: "rm_url"
17
+ get url: "/debug", controller: "MainController", method: "debug"
18
+ get url: "/hello", controller: "MainController", method: "hello"
19
+
20
+ get url: "/image", controller: "MainController", method: "image"
21
+ get url: "/image/:image", controller: "MainController", method: "image"
22
+ get url: "img/:image", controller: "MainController", method: "image"
data/test/responder.rb ADDED
@@ -0,0 +1,90 @@
1
+
2
+ class TestNephosServerResponder < Test::Unit::TestCase
3
+
4
+ def test_content_type
5
+ assert_equal(
6
+ {'Content-type' => "KIND/TYPE" "; charset=" "CHARSET"},
7
+ Nephos::Responder.content_type("KIND", "TYPE", "CHARSET")
8
+ )
9
+ end
10
+
11
+ def test_ct_specific
12
+ p0 = {type: "text/plain"}
13
+ p1 = {type: "/"}
14
+ p2 = {type: "text/"}
15
+ p3 = {type: "/plain"}
16
+ p4 = {type: "textplain"}
17
+ p5 = {type: ""}
18
+ p6 = {type: 1}
19
+ p7 = {type: []}
20
+ p8 = {type: nil}
21
+ p9 = {}
22
+ assert Nephos::Responder.ct_specific(p0)
23
+ assert_raise Nephos::Responder::InvalidContentType do Nephos::Responder.ct_specific(p1) end
24
+ assert_raise Nephos::Responder::InvalidContentType do Nephos::Responder.ct_specific(p2) end
25
+ assert_raise Nephos::Responder::InvalidContentType do Nephos::Responder.ct_specific(p3) end
26
+ assert_raise Nephos::Responder::InvalidContentType do Nephos::Responder.ct_specific(p4) end
27
+ assert_raise Nephos::Responder::InvalidContentType do Nephos::Responder.ct_specific(p5) end
28
+ assert_raise Nephos::Responder::InvalidContentType do Nephos::Responder.ct_specific(p6) end
29
+ assert_raise Nephos::Responder::InvalidContentType do Nephos::Responder.ct_specific(p7) end
30
+ assert_raise Nephos::Responder::InvalidContentType do Nephos::Responder.ct_specific(p8) end
31
+ assert_raise Nephos::Responder::InvalidContentType do Nephos::Responder.ct_specific(p9) end
32
+ end
33
+
34
+ def test_set_default_params_status
35
+ p1 = {status: 200}
36
+ p2 = {status: 300}
37
+ p3 = {status: nil}
38
+ p4 = {}
39
+ Nephos::Responder.set_default_params_status(p1)
40
+ Nephos::Responder.set_default_params_status(p2)
41
+ Nephos::Responder.set_default_params_status(p3)
42
+ Nephos::Responder.set_default_params_status(p4)
43
+ assert_equal({status: 200}, p1)
44
+ assert_equal({status: 300}, p2)
45
+ assert_equal({status: 200}, p3)
46
+ assert_equal({status: 200}, p4)
47
+ end
48
+
49
+ def test_set_default_params_type_1
50
+ p_ref = {type: Nephos::Responder.ct_specific({type: "text/plain"})}
51
+ p1 = {type: "text/plain"}
52
+ p2 = {type: "text"}
53
+ p3 = {type: "text/plain"}
54
+ Nephos::Responder.set_default_params_type(p1)
55
+ assert_equal p1, p_ref
56
+ assert_raise Nephos::Responder::InvalidContentType do
57
+ Nephos::Responder.set_default_params_type(p2) end
58
+ Nephos::Responder.set_default_params_type(p3)
59
+ assert_equal p_ref, p3
60
+ end
61
+
62
+ def test_set_default_params_type_2
63
+ p1 = {plain: "bla"}
64
+ p2 = {html: "bla"}
65
+ p3 = {json: "bla", plain: "bla"}
66
+ p4 = {json: "bla", html: "bla"}
67
+ p5 = {json: "bla"}
68
+ Nephos::Responder.set_default_params_type(p1)
69
+ Nephos::Responder.set_default_params_type(p2)
70
+ Nephos::Responder.set_default_params_type(p3)
71
+ Nephos::Responder.set_default_params_type(p4)
72
+ Nephos::Responder.set_default_params_type(p5)
73
+ plain = Nephos::Responder.ct_specific({type: "text/plain"})
74
+ json = Nephos::Responder.ct_specific({type: "text/javascript"})
75
+ html = Nephos::Responder.ct_specific({type: "text/html"})
76
+ assert_equal plain, p1[:type]
77
+ assert_equal html, p2[:type]
78
+ assert_equal json, p3[:type]
79
+ assert_equal json, p4[:type]
80
+ assert_equal json, p5[:type]
81
+ end
82
+
83
+ def test_render_empty
84
+ assert_equal(
85
+ [204, Nephos::Responder.ct_specific({type: "text/plain"}), [""]],
86
+ Nephos::Responder.render(:empty)
87
+ )
88
+ end
89
+
90
+ end
data/test/routing.rb ADDED
@@ -0,0 +1,18 @@
1
+ class TestController < Nephos::Controller
2
+ def method
3
+ {plain: "test"}
4
+ end
5
+ end
6
+
7
+ class TestNephosServerRouting < Test::Unit::TestCase
8
+
9
+ # remove all seeds
10
+ Nephos::Route::ALL = []
11
+
12
+ def test_valid_routes
13
+ get url: "/", controller: "TestController", method: "method", silent: true
14
+ post url: "/", controller: "TestController", method: "method", silent: true
15
+ put url: "/", controller: "TestController", method: "method", silent: true
16
+ end
17
+
18
+ end
data/test/test.rb CHANGED
@@ -3,3 +3,6 @@ require_relative "../lib/nephos-server"
3
3
 
4
4
  class TestNephosServer < Test::Unit::TestCase
5
5
  end
6
+
7
+ require_relative 'responder'
8
+ require_relative 'routing'
data/version CHANGED
@@ -1 +1 @@
1
- 0.1.11
1
+ 0.1.12
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.11
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - poulet_a
@@ -49,7 +49,6 @@ email:
49
49
  - lib/nephos-server/routing/execute.rb
50
50
  - lib/nephos-server/routing/load.rb
51
51
  - lib/nephos-server/routing/controller.rb
52
- - routes.rb.example
53
52
  - README.md
54
53
  - Rakefile
55
54
  - Procfile
@@ -58,6 +57,12 @@ email:
58
57
  - nephos-server.gemspec
59
58
  - version
60
59
  - test/test.rb
60
+ - test/responder.rb
61
+ - test/routing.rb
62
+ - routes.rb
63
+ - controllers/dataset.rb
64
+ - controllers/image.jpg
65
+ - controllers/main.rb
61
66
  - bin/nephos-server
62
67
  - bin/nephos-generator
63
68
  executables:
@@ -73,6 +78,9 @@ files:
73
78
  - Rakefile
74
79
  - bin/nephos-generator
75
80
  - bin/nephos-server
81
+ - controllers/dataset.rb
82
+ - controllers/image.jpg
83
+ - controllers/main.rb
76
84
  - lib/nephos-server.rb
77
85
  - lib/nephos-server/basic_errors.rb
78
86
  - lib/nephos-server/routing/controller.rb
@@ -81,7 +89,9 @@ files:
81
89
  - lib/nephos-server/server/main.rb
82
90
  - lib/nephos-server/server/responder.rb
83
91
  - nephos-server.gemspec
84
- - routes.rb.example
92
+ - routes.rb
93
+ - test/responder.rb
94
+ - test/routing.rb
85
95
  - test/test.rb
86
96
  - version
87
97
  homepage: https://github.com/pouleta/NephosRubyServer
data/routes.rb.example DELETED
@@ -1,3 +0,0 @@
1
- get url: "/", controller: "MainController", method: "root"
2
- get url: "/add", controller: "MainController", method: "add_url"
3
- get url: "/rm", controller: "MainController", method: "rm_url"