nephos-server 0.1.8 → 0.1.10

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: 47e385b6e019a466c3af125cbb247264e2fcd452
4
- data.tar.gz: d6f6a10f417bf70a4178d61a1322d6d04a3d9596
3
+ metadata.gz: 4ebd683681680431232080fb04b684f7096990ea
4
+ data.tar.gz: 7f2eb0f1413f4b05dba58f0f6cf4b5687d9faacc
5
5
  SHA512:
6
- metadata.gz: 6be74ee7209d60848fa64ec901320fddcdf1e11bd8f24a22ca789a85df9e97e20cd4cfcb869bafac0e0bd693d68b31a369b7119e9a9b1633fb4450009fc89cfa
7
- data.tar.gz: c899551d96c76d48d94606d32b892093e35e6bcaca6d32efd028db49ac51a260ceefa68c57b50d79a8fb97db118ac57c5f6ef17b6a6ed223c579009311dd656c
6
+ metadata.gz: 9c135eb80f258c823ea98467fb958696fdf99e1587fd797130e2b1cb2f5d6d3aa11bb1d261c105b33997b3fe1a5a997905c7d4bae6c5746bd68d3cb5733864fc
7
+ data.tar.gz: 9d4df9a01574a401b93d90f5ce8330d2a0ddef27271264d2254abda8492c522494f777a8cfb1d4c98f3c39eea33e2ee3f7e70a3b67cc8781ab19310ade63d180
data/Gemfile CHANGED
@@ -1,4 +1,9 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ # web server
3
4
  gem 'rack'
4
- gem 'nomorebeer'
5
+
6
+ # colorize messages
7
+ gem 'colorize'
8
+
9
+ gem 'nomorebeer' # action against drunk driving
data/Gemfile.lock CHANGED
@@ -1,13 +1,15 @@
1
1
  GEM
2
2
  remote: https://rubygems.org/
3
3
  specs:
4
- nomorebeer (1.0)
4
+ colorize (0.7.7)
5
+ nomorebeer (1.1)
5
6
  rack (1.6.4)
6
7
 
7
8
  PLATFORMS
8
9
  ruby
9
10
 
10
11
  DEPENDENCIES
12
+ colorize
11
13
  nomorebeer
12
14
  rack
13
15
 
data/README.md CHANGED
@@ -15,9 +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
- - Routing
19
- - improve get with arguments
20
- - add ressource (elarge urls)
21
18
  - Database connection
22
19
 
23
20
 
@@ -52,8 +49,11 @@ In a controller, use:
52
49
 
53
50
  ```ruby
54
51
  return {status: code}
55
- return {json: {...}}
52
+ return {status: code, content: "Not today"}
53
+ return {json: {status: "resource created"}, status: 201}
56
54
  return {plain: "text"}
55
+ return {html: "<html><body><h1>:D</h1></body></html>"}
56
+ return {type: "image/jpeg", content: File.read("images/photo.jpg")}
57
57
  return :empty
58
58
  ```
59
59
 
@@ -64,7 +64,9 @@ Like in ``/routes.rb``, you have to route manually the api.
64
64
  ```ruby
65
65
  get url: "/", controller: "MainController", method: "root" # /
66
66
  get url: "/add", controller: "MainController", method: "add_url" # /add
67
+ get url: "/add/:url", controller: "MainController", method: "add_url" # /add
67
68
  get url: "/rm", controller: "MainController", method: "rm_url" # /rm
69
+ get url: "/rm/:url", controller: "MainController", method: "rm_url" # /rm
68
70
  resource "infos" do
69
71
  get url: "/", controller: "MainController", method: "root" # generate /infos
70
72
  end
data/bin/nephos-generator CHANGED
@@ -42,6 +42,14 @@ def initialize_application
42
42
  File.write "routes.rb", ROUTE_RB
43
43
  File.write "Gemfile", GEMFILE
44
44
  Dir.mkdir "controllers"
45
+ begin
46
+ `git init .`
47
+ puts "Git repository initialized"
48
+ rescue Errno::ENOENT => err
49
+ puts "Warning: git repository not initialized"
50
+ rescue => err
51
+ puts "Error: #{err.message}"
52
+ end
45
53
  exec("bundle install")
46
54
  end
47
55
 
data/bin/nephos-server CHANGED
@@ -26,13 +26,13 @@ begin
26
26
  if not $debug and
27
27
  (not File.exists? "Gemfile.lock" or
28
28
  not File.read("Gemfile.lock").split.index("nephos-server"))
29
- raise InvalidApplicationError
29
+ raise InvalidApplication
30
30
  end
31
31
 
32
32
  Nephos::Server.start($server_port)
33
33
 
34
34
  rescue RoutingError => err
35
- puts "Routing Error. Check out the documentation and `routes.rb` file: #{err.message}"
35
+ puts "#{err.class}. Check out the documentation and `routes.rb` file: #{err.message}"
36
36
 
37
37
  rescue => err
38
38
  puts "Error: #{err.message}"
data/lib/nephos-server.rb CHANGED
@@ -4,5 +4,6 @@
4
4
  require 'json'
5
5
  require 'open-uri'
6
6
  require 'rack'
7
+ require 'colorize'
7
8
 
8
9
  require_relative 'nephos-server/server/main'
@@ -4,4 +4,6 @@ class InvalidRouteUrl < InvalidRoute; end
4
4
  class InvalidRouteController < InvalidRoute; end
5
5
  class InvalidRouteMethod < InvalidRoute; end
6
6
 
7
- class InvalidApplicationError < RuntimeError; end
7
+ class InvalidApplication < RuntimeError; end
8
+ class InvalidGit < InvalidApplication; end
9
+ class NoGitBinary < InvalidGit; end
@@ -1,18 +1,23 @@
1
1
  module Nephos
2
2
  class Controller
3
3
 
4
- attr_reader :env, :infos
4
+ attr_reader :env, :infos, :callpath
5
5
 
6
6
  # @param env [Hash] env extracted from the http request
7
7
  # @param parsed [Hash] pre-parsed env with parameters, ...
8
- def initialize env, parsed
8
+ def initialize env={}, parsed={path: [], args: {}}, callpath={params: []}
9
9
  @env= env
10
10
  @infos= parsed
11
+ @callpath= callpath
12
+ @params= parsed[:args]
13
+ @params= @params.merge Hash[callpath[:params].zip @infos[:path]]
14
+ @params= @params.select{|k,v|k}
11
15
  end
12
16
 
13
17
  def arguments
14
- @infos[:args]
18
+ @params
15
19
  end
20
+ alias :params :arguments
16
21
 
17
22
  end
18
23
  end
@@ -20,11 +20,16 @@ module Nephos
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)
23
- return ALL.find{|e| e[:url] == route and e[:verb] == verb}
23
+ return ALL.find{|e| e[:match] =~ route and e[:verb] == verb}
24
24
  end
25
25
 
26
26
  def self.execute(env)
27
- route = URI.parse(env['REQUEST_URI']) rescue (puts "uri err"; return render(status: 500))
27
+ begin
28
+ route = URI.parse(env['REQUEST_URI'])
29
+ rescue => err
30
+ puts "uri err #{err.message}".red
31
+ return render(status: 500)
32
+ end
28
33
  verb = env["REQUEST_METHOD"]
29
34
  from = env["REMOTE_ADDR"]
30
35
  path = route.path.split("/").select{|e|not e.to_s.empty?}
@@ -34,10 +39,9 @@ module Nephos
34
39
  call = parse_path(path, verb)
35
40
  return render status: 404 if call.nil?
36
41
  begin
37
- controller = Module.const_get(call[:controller]).new(env, parsed)
42
+ controller = Module.const_get(call[:controller]).new(env, parsed, call)
38
43
  return render(controller.send(call[:method]) || {status: 500})
39
44
  rescue => err
40
- # require 'pry'; binding.pry
41
45
  return render(status: 500)
42
46
  end
43
47
  end
@@ -6,6 +6,15 @@ module Nephos
6
6
  puts "[#{verb}] #{what[:url]} \t ---> \t #{what[:controller]}##{what[:method]}"
7
7
  end
8
8
 
9
+ def self.add_params!(what)
10
+ params = what[:url].split('/').map do |p|
11
+ p.match(/:\w+/) ? {p: "[[:graph:]]+", name: p} : {p: p, name: nil}
12
+ end
13
+ url = params.map{|e| e[:p]}.join("/")
14
+ what[:match] = /^#{url}$/
15
+ what[:params] = params.map{|e| e[:name] && e[:name][1..-1]}[1..-1]
16
+ end
17
+
9
18
  def self.check!(what)
10
19
  raise InvalidRouteUrl, "Missing URL" unless what.keys.include? :url
11
20
  raise InvalidRouteController, "Missing Controller" unless what.keys.include? :controller
@@ -18,7 +27,7 @@ module Nephos
18
27
  if not controller.ancestors.include? Nephos::Controller
19
28
  raise InvalidRouteController, "Class \"#{what[:controller]}\" is not a Nephos::Controller"
20
29
  end
21
- if not controller.new({}, {}).respond_to? what[:method]
30
+ if not controller.new.respond_to? what[:method]
22
31
  raise InvalidRouteMethod, "No method named \"#{what[:method]}\""
23
32
  end rescue raise InvalidRouteController, "Cannot initialize controller"
24
33
  end
@@ -36,6 +45,7 @@ def get what
36
45
  raise InvalidRoute unless what.is_a? Hash
37
46
  what[:url] = File.expand_path File.join(route_prefix, what[:url])
38
47
  Nephos::Route.check!(what)
48
+ Nephos::Route.add_params!(what)
39
49
  Nephos::Route.add(what, "GET")
40
50
  end
41
51
 
@@ -44,6 +54,7 @@ def post what
44
54
  raise InvalidRoute unless what.is_a? Hash
45
55
  what[:url] = File.join(route_prefix, what[:url])
46
56
  Nephos::Route.check!(what)
57
+ Nephos::Route.add_params!(what)
47
58
  Nephos::Route.add(what, "POST")
48
59
  end
49
60
 
@@ -52,6 +63,7 @@ def put what
52
63
  raise InvalidRoute unless what.is_a? Hash
53
64
  what[:url] = File.join(route_prefix, what[:url])
54
65
  Nephos::Route.check!(what)
66
+ Nephos::Route.add_params!(what)
55
67
  Nephos::Route.add(what, "PUT")
56
68
  end
57
69
 
@@ -1,4 +1,4 @@
1
- require_relative 'basic_errors'
1
+ require_relative '../basic_errors'
2
2
  require_relative 'responder'
3
3
  require_relative '../routing/execute'
4
4
 
@@ -32,10 +32,12 @@ module Nephos
32
32
  params[:status] ||= 200
33
33
  end
34
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"
35
+ if params[:status].to_s.match(/^[45]\d\d$/)
36
+ params[:plain] ||= params[:content] || "Error: #{params[:status]} code"
37
+ elsif params[:status].to_s.match(/^[3]\d\d$/)
38
+ params[:plain] ||= params[:content] || "Redirected: #{params[:status]} code"
37
39
  else
38
- params[:plain] ||= "ok"
40
+ params[:plain] ||= params[:content] || "Status: #{params[:status]} code"
39
41
  end
40
42
  end
41
43
  params
@@ -10,9 +10,9 @@ Gem::Specification.new do |s|
10
10
  s.email = 'poulet_a@epitech.eu',
11
11
  s.files = [
12
12
  'lib/nephos-server.rb',
13
+ 'lib/nephos-server/basic_errors.rb',
13
14
  'lib/nephos-server/server/main.rb',
14
15
  'lib/nephos-server/server/responder.rb',
15
- 'lib/nephos-server/server/basic_errors.rb',
16
16
  'lib/nephos-server/routing/execute.rb',
17
17
  'lib/nephos-server/routing/load.rb',
18
18
  'lib/nephos-server/routing/controller.rb',
data/version CHANGED
@@ -1 +1 @@
1
- 0.1.8
1
+ 0.1.10
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nephos-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - poulet_a
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-23 00:00:00.000000000 Z
11
+ date: 2015-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nomorebeer
@@ -43,9 +43,9 @@ description: " a minimalist server, based on rack/puma, with routing, rendering,
43
43
  email:
44
44
  - poulet_a@epitech.eu
45
45
  - - lib/nephos-server.rb
46
+ - lib/nephos-server/basic_errors.rb
46
47
  - lib/nephos-server/server/main.rb
47
48
  - lib/nephos-server/server/responder.rb
48
- - lib/nephos-server/server/basic_errors.rb
49
49
  - lib/nephos-server/routing/execute.rb
50
50
  - lib/nephos-server/routing/load.rb
51
51
  - lib/nephos-server/routing/controller.rb
@@ -74,10 +74,10 @@ files:
74
74
  - bin/nephos-generator
75
75
  - bin/nephos-server
76
76
  - lib/nephos-server.rb
77
+ - lib/nephos-server/basic_errors.rb
77
78
  - lib/nephos-server/routing/controller.rb
78
79
  - lib/nephos-server/routing/execute.rb
79
80
  - lib/nephos-server/routing/load.rb
80
- - lib/nephos-server/server/basic_errors.rb
81
81
  - lib/nephos-server/server/main.rb
82
82
  - lib/nephos-server/server/responder.rb
83
83
  - nephos-server.gemspec