rack-app 0.10.1 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2df00445bf28113e1aae2013345dad70f163dc27
4
- data.tar.gz: 860463dbdc86d2c72fb41fad5745a9c58cd31371
3
+ metadata.gz: d90aa076001c7ddbd654f22791f34854fbcd1d80
4
+ data.tar.gz: df0aa5a38c939340419270aae93830def72f84f0
5
5
  SHA512:
6
- metadata.gz: aa27e35b11ea46817f6b91d31e64fc7b1ddee0148d11c6dcac3e236e1afbb0dd35e00fdc212f33a75462535078d495d2f9691d099e41d3a4e60341860dca1708
7
- data.tar.gz: 67fa1277b0fcedc5119ad9d9ba848274083f62ec3012461681f47c47baeb72eadd4491caf63aac16a1148ee637600ea4a3c2483063fdb4facf726cc343f6d4db
6
+ metadata.gz: de99d24d08255b30a9962f0307da406558ab7e3212a19116ef4fff00aadeede2b74b8fe008244dc19f11d7d8fa87bb201b22c0b620d90f61bb50bc53e25547b1
7
+ data.tar.gz: 6d2e15dd8548668f67786b7c96c89b9bdce77adce5dc006fcf250965fa958b1deaf8a14ed905e2a0dc9541f243492cb9e8e0be33879e8e4aa0a76027e4d48cdf
data/README.md CHANGED
@@ -4,6 +4,8 @@
4
4
  [travis-link]: http://travis-ci.org/adamluzsi/rack-app.rb
5
5
  [travis-home]: http://travis-ci.org/
6
6
 
7
+ ![Rack::App](http://rack-app-website.herokuapp.com/image/msruby.png)
8
+
7
9
  Your next favourite rack based micro framework that is totally addition free!
8
10
  Have a cup of awesomeness with your performance designed framework!
9
11
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.10.1
1
+ 0.11.0
@@ -1,8 +1,5 @@
1
1
  require 'rack/app'
2
- require 'rack/app/file/version'
3
-
4
2
  module Rack::App::File
5
3
  require 'rack/app/file/streamer'
6
- require 'rack/app/file/parser'
7
4
  require 'rack/app/file/server'
8
5
  end
@@ -1,32 +1,36 @@
1
- module Rack::App::File::Server
1
+ class Rack::App::File::Server
2
2
 
3
- include Rack::App::File::Parser::Factory
3
+ def initialize(root_folder, options={})
4
+ require 'rack/file'
4
5
 
5
- def source_folder(relative_folder_path)
6
+ namespace = formatted_namespace(options)
7
+ namespace.freeze
6
8
 
7
- source_folder = Rack::App::Utils.pwd(relative_folder_path)
8
- Dir.glob(File.join(source_folder, '**', '*')).each do |file_path|
9
+ @namespace_rgx = /#{Regexp.escape(namespace)}/.freeze
10
+ @rack_file_server = ::Rack::File.new(Rack::App::Utils.pwd(root_folder), {})
9
11
 
10
- file_parser_class = find_file_parser_class_for(File.extname(file_path))
11
-
12
- raw_endpoint_name = file_path.sub(source_folder, '').split(File::Separator).join('/')
13
- request_path = file_parser_class.format_request_path(raw_endpoint_name)
14
-
15
- options request_path do
16
- response.finish
17
- end
12
+ end
18
13
 
19
- get request_path do
20
- file_parser = file_parser_class.new(self)
14
+ def call(env)
15
+ env[::Rack::PATH_INFO]= clean_path_info(env).sub(@namespace_rgx, '')
16
+ @rack_file_server.call(env)
17
+ end
21
18
 
22
- response.body = file_parser.parse(file_path)
23
- response.headers[Rack::CONTENT_TYPE]= Rack::Mime.mime_type(file_parser.file_type(file_path)).to_s
19
+ protected
24
20
 
25
- response.finish
26
- end
21
+ def raw_namespace(options)
22
+ options[:to] || '/'
23
+ end
27
24
 
28
- end
25
+ def formatted_namespace(options)
26
+ namespace = raw_namespace(options).to_s.sub(/^\//, '').sub(/\/$/, '')
27
+ namespace += '/' unless namespace.empty?
28
+ namespace
29
+ end
29
30
 
31
+ def clean_path_info(env)
32
+ path_info = ::Rack::Utils.unescape(env[::Rack::PATH_INFO])
33
+ return clean_path_info = ::Rack::Utils.clean_path_info(path_info)
30
34
  end
31
35
 
32
36
  end
@@ -3,11 +3,21 @@ class Rack::App::Router
3
3
  require 'rack/app/router/static'
4
4
  require 'rack/app/router/dynamic'
5
5
 
6
- #TD
7
- def endpoint_paths
8
- @static.endpoints.map do |indentifiers, endpoint|
9
- [[*indentifiers].join("\t"),endpoint.properties[:description]].compact.join("\t\t\t")
6
+ def show_endpoints
7
+ endpoints = (@static.show_endpoints + @dynamic.show_endpoints)
8
+
9
+ wd0 = endpoints.map { |row| row[0].to_s.length }.max
10
+ wd1 = endpoints.map { |row| row[1].to_s.length }.max
11
+ wd2 = endpoints.map { |row| row[2].to_s.length }.max
12
+
13
+ return endpoints.map do |row|
14
+ [
15
+ row[0].to_s.ljust(wd0),
16
+ row[1].to_s.ljust(wd1),
17
+ row[2].to_s.ljust(wd2)
18
+ ].join(' ')
10
19
  end
20
+
11
21
  end
12
22
 
13
23
  def add_endpoint(request_method, request_path, endpoint)
@@ -39,7 +49,7 @@ class Rack::App::Router
39
49
  end
40
50
 
41
51
  def defined_path_is_dynamic?(path_str)
42
- !!(path_str.to_s =~ /\/:\w+/i)
52
+ !!(path_str.to_s =~ /\/:\w+/i) or !!(path_str.to_s =~ /\/\*/i)
43
53
  end
44
54
 
45
55
  end
@@ -1,22 +1,35 @@
1
1
  class Rack::App::Router::Dynamic
2
2
 
3
- ANY = 'Rack::App::Router::Dynamic::ANY'.freeze
3
+ PATH_PARAM = :"#{self}::PATH_PARAM".freeze
4
+ PARTIAL = :"#{self}::PARTIAL".freeze
5
+
6
+ # def endpoint_paths
7
+ # end
4
8
 
5
9
  def add_endpoint(request_method, request_path, endpoint)
6
10
  request_path = Rack::App::Utils.normalize_path(request_path)
11
+ @endpoint_paths << [request_method, request_path, endpoint.properties[:description]]
7
12
 
8
13
  current_cluster = main_cluster(request_method)
9
14
  path_params = {}
15
+ break_build = false
16
+
10
17
  request_path.split('/').each.with_index do |path_part, index|
11
18
 
12
19
  new_cluster_name = if path_part_is_dynamic?(path_part)
13
- path_params[index]= path_part.sub(/^:/,'')
14
- ANY
20
+ path_params[index]= path_part.sub(/^:/, '')
21
+ PATH_PARAM
22
+
23
+ elsif path_part_is_a_partial?(path_part)
24
+ break_build = true
25
+ PARTIAL
26
+
15
27
  else
16
28
  path_part
17
29
  end
18
30
 
19
31
  current_cluster = (current_cluster[new_cluster_name] ||= {})
32
+ break if break_build
20
33
 
21
34
  end
22
35
 
@@ -31,38 +44,51 @@ class Rack::App::Router::Dynamic
31
44
 
32
45
  current_cluster = main_cluster(request_method)
33
46
  normalized_request_path.split('/').each do |path_part|
34
- current_cluster = current_cluster[path_part] || current_cluster[ANY]
35
- return nil if current_cluster.nil?
47
+ previous_cluster = current_cluster
48
+ current_cluster = current_cluster[path_part] || current_cluster[PATH_PARAM]
49
+ if current_cluster.nil?
50
+ if previous_cluster[PARTIAL]
51
+ current_cluster = previous_cluster[PARTIAL]
52
+ break
53
+ else
54
+ return nil
55
+ end
56
+ end
36
57
  end
37
58
 
38
59
  current_cluster[:endpoint]
39
60
  end
40
61
 
41
62
  def merge!(router)
42
- raise(ArgumentError,"invalid route object, must be instance of #{self.class.to_s}") unless router.is_a?(self.class)
43
- deep_merge!(@http_method_cluster,router.instance_variable_get(:@http_method_cluster))
63
+ raise(ArgumentError, "invalid route object, must be instance of #{self.class.to_s}") unless router.is_a?(self.class)
64
+ deep_merge!(@http_method_cluster, router.instance_variable_get(:@http_method_cluster))
44
65
  nil
45
66
  end
46
67
 
68
+ def show_endpoints
69
+ @endpoint_paths
70
+ end
71
+
47
72
  protected
48
73
 
49
74
  def initialize
50
75
  @http_method_cluster = {}
76
+ @endpoint_paths = []
51
77
  end
52
78
 
53
79
  def path_part_is_dynamic?(path_part_str)
54
80
  !!(path_part_str.to_s =~ /^:\w+$/i)
55
81
  end
56
82
 
57
- def deep_merge!(hash,other_hash)
83
+ def deep_merge!(hash, other_hash)
58
84
  other_hash.each_pair do |current_key, other_value|
59
85
 
60
86
  this_value = hash[current_key]
61
87
 
62
88
  hash[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash)
63
- deep_merge!(this_value,other_value)
89
+ deep_merge!(this_value, other_value)
64
90
  else
65
- other_value
91
+ other_value
66
92
  end
67
93
  end
68
94
 
@@ -73,4 +99,8 @@ class Rack::App::Router::Dynamic
73
99
  (@http_method_cluster[request_method.to_s.upcase] ||= {})
74
100
  end
75
101
 
102
+ def path_part_is_a_partial?(path_part)
103
+ (path_part == '**' or path_part == '*')
104
+ end
105
+
76
106
  end
@@ -12,11 +12,16 @@ class Rack::App::Router::Static
12
12
  end
13
13
 
14
14
  def merge!(static_router)
15
- raise(ArgumentError,"Invalid argument given, must be instance of a #{self.class.to_s}") unless static_router.is_a?(self.class)
15
+ raise(ArgumentError, "Invalid argument given, must be instance of a #{self.class.to_s}") unless static_router.is_a?(self.class)
16
16
  @endpoints.merge!(static_router.instance_variable_get(:@endpoints))
17
17
  nil
18
18
  end
19
19
 
20
+ def show_endpoints
21
+ @endpoints.map do |identifiers, endpoint|
22
+ [identifiers, endpoint.properties[:description]].flatten
23
+ end
24
+ end
20
25
 
21
26
  protected
22
27
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-app
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-01-29 00:00:00.000000000 Z
12
+ date: 2016-01-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -92,9 +92,6 @@ files:
92
92
  - lib/rack/app/endpoint.rb
93
93
  - lib/rack/app/endpoint/not_found.rb
94
94
  - lib/rack/app/file.rb
95
- - lib/rack/app/file/parser.rb
96
- - lib/rack/app/file/parser/erb.rb
97
- - lib/rack/app/file/parser/factory.rb
98
95
  - lib/rack/app/file/server.rb
99
96
  - lib/rack/app/file/streamer.rb
100
97
  - lib/rack/app/file/version.rb
@@ -1,24 +0,0 @@
1
- class Rack::App::File::Parser
2
-
3
- require 'rack/app/file/parser/factory'
4
- require 'rack/app/file/parser/erb'
5
-
6
- def self.format_request_path(request_path)
7
- request_path
8
- end
9
-
10
- def parse(file_path)
11
- Rack::App::File::Streamer.new(file_path)
12
- end
13
-
14
- def file_type(file_path)
15
- File.extname(file_path)
16
- end
17
-
18
- protected
19
-
20
- def initialize(app)
21
- @app = app
22
- end
23
-
24
- end
@@ -1,18 +0,0 @@
1
- require 'erb'
2
- class Rack::App::File::Parser::ERB < Rack::App::File::Parser
3
-
4
- require 'erb'
5
-
6
- def parse(file_path)
7
- [::ERB.new(File.read(file_path)).result(@app.instance_eval { binding })]
8
- end
9
-
10
- def file_type(file_path)
11
- super(file_path.sub(/\.erb$/, ''))
12
- end
13
-
14
- def self.format_request_path(request_path)
15
- request_path.sub(/\.erb$/, '')
16
- end
17
-
18
- end
@@ -1,22 +0,0 @@
1
- module Rack::App::File::Parser::Factory
2
-
3
- def use_file_parser(parser_class, *extensions)
4
- extensions.each do |extension|
5
- file_parser_classes[extension.to_s]= parser_class
6
- end
7
- nil
8
- end
9
-
10
- def find_file_parser_class_for(extension)
11
- file_parser_classes[extension.to_s] || Rack::App::File::Parser
12
- end
13
-
14
- protected
15
-
16
- def file_parser_classes
17
- @file_parser_classes ||= {
18
- '.erb' => Rack::App::File::Parser::ERB
19
- }
20
- end
21
-
22
- end