rack-app 0.19.0 → 0.20.0

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: 287ab34c8649763c80531a5028f4014eddb24020
4
- data.tar.gz: bd7e5137d8bb0d85b35b2a3ecbf82228d92e9334
3
+ metadata.gz: 657c3f30f9056e50b3fe23c9ff0eb15b921f9828
4
+ data.tar.gz: fd681b8d00dff5cb31a25217e2868861c3ae2235
5
5
  SHA512:
6
- metadata.gz: e36d1a38769f610ea78e4c8f742b6b9440145a685cca414bb19997b413a030fb3c19b7a2af548fd9bb729babb15bfa59969e3d5a7911bd0f40c9fa61bfccd809
7
- data.tar.gz: fc985cf781a91b2c6df694be3ef875eac1c20e16e51a196770a41e9c8ccec6e3fdcc9d2c40744907e49a4e93905867e02c90add0f0e0e2665035127fb580acdb
6
+ metadata.gz: a2a0dda2158f2a2c328c30bdaeac5d0e730781de5fc8d654937796fd681b9da24546aad6afa397052bfb35731c99addb0256f81a58334c687a9e17683c5f1004
7
+ data.tar.gz: dad53fa0a014e003153179ed8c67b9e56c9f1177c592bcde190dce0f93df41247db95627a184a0c219198d92a02ab65476ccf123b730858e5e4bad3552366a7c
data/README.md CHANGED
@@ -53,7 +53,7 @@ config.ru
53
53
 
54
54
  ```ruby
55
55
 
56
- require './bootstrap.rb'
56
+ require 'rack/app'
57
57
 
58
58
  class App < Rack::App
59
59
 
@@ -70,7 +70,7 @@ end
70
70
 
71
71
  ```ruby
72
72
 
73
- require './bootstrap.rb'
73
+ require 'rack/app'
74
74
 
75
75
  class App < Rack::App
76
76
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.19.0
1
+ 0.20.0
data/lib/rack/app.rb CHANGED
@@ -62,12 +62,14 @@ class Rack::App
62
62
  end
63
63
 
64
64
  def root(endpoint_path)
65
+ normalized_path = Rack::App::Utils.normalize_path(endpoint_path)
66
+
65
67
  options '/' do
66
- endpoint = self.class.router.fetch_endpoint('OPTIONS', Rack::App::Utils.normalize_path(endpoint_path))
68
+ endpoint = self.class.router.fetch_endpoint('OPTIONS', normalized_path)
67
69
  endpoint.get_response_body(request, response)
68
70
  end
69
71
  get '/' do
70
- endpoint = self.class.router.fetch_endpoint('GET', Rack::App::Utils.normalize_path(endpoint_path))
72
+ endpoint = self.class.router.fetch_endpoint('GET', normalized_path)
71
73
  endpoint.get_response_body(request, response)
72
74
  end
73
75
  end
@@ -96,7 +98,7 @@ class Rack::App
96
98
  )
97
99
 
98
100
  endpoint = Rack::App::Endpoint.new(properties)
99
- router.add_endpoint(request_method, request_path, endpoint)
101
+ router.register_endpoint!(request_method, request_path, @last_description, endpoint)
100
102
 
101
103
  @last_description = nil
102
104
  return endpoint
@@ -107,7 +109,8 @@ class Rack::App
107
109
  options.merge!(endpoint_properties)
108
110
  file_server = Rack::App::File::Server.new(relative_path, options)
109
111
  request_path = Rack::App::Utils.join(file_server.namespace, '**', '*')
110
- router.add_endpoint('GET', request_path, file_server)
112
+ router.register_endpoint!('GET', request_path, @last_description, file_server)
113
+ @last_description = nil
111
114
  end
112
115
 
113
116
  def mount(api_class)
@@ -171,7 +174,7 @@ class Rack::App
171
174
 
172
175
  payload = ''
173
176
  while chunk = @request.body.gets
174
- payload += chunk
177
+ payload << chunk
175
178
  end
176
179
  @request.body.rewind
177
180
 
@@ -38,6 +38,7 @@ class Rack::App::Endpoint
38
38
  end
39
39
 
40
40
  def register_path_params_matcher(params_matcher)
41
+ @path_params_matcher.clear
41
42
  @path_params_matcher.merge!(params_matcher)
42
43
  end
43
44
 
@@ -1,43 +1,47 @@
1
1
  class Rack::App::Router
2
2
 
3
+ require 'rack/app/router/base'
3
4
  require 'rack/app/router/static'
4
5
  require 'rack/app/router/dynamic'
5
6
 
7
+ def endpoints
8
+ (@static.endpoints + @dynamic.endpoints)
9
+ end
10
+
6
11
  def show_endpoints
7
- endpoints = (@static.show_endpoints + @dynamic.show_endpoints)
8
12
 
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
13
+ endpoints = self.endpoints
14
+ wd0 = endpoints.map { |endpoint| endpoint[:request_method].to_s.length }.max
15
+ wd1 = endpoints.map { |endpoint| endpoint[:request_path].to_s.length }.max
16
+ wd2 = endpoints.map { |endpoint| endpoint[:description].to_s.length }.max
12
17
 
13
- return endpoints.map do |row|
18
+ return endpoints.sort_by { |endpoint| [endpoint[:request_method], endpoint[:request_path]] }.map do |endpoint|
14
19
  [
15
- row[0].to_s.ljust(wd0),
16
- row[1].to_s.ljust(wd1),
17
- row[2].to_s.ljust(wd2)
20
+ endpoint[:request_method].to_s.ljust(wd0),
21
+ endpoint[:request_path].to_s.ljust(wd1),
22
+ endpoint[:description].to_s.ljust(wd2)
18
23
  ].join(' ')
19
24
  end
20
25
 
21
26
  end
22
27
 
23
- def add_endpoint(request_method, request_path, endpoint)
24
- if defined_path_is_dynamic?(Rack::App::Utils.normalize_path(request_path))
25
- @dynamic.add_endpoint(request_method, request_path, endpoint)
26
- else
27
- @static.add_endpoint(request_method, request_path, endpoint)
28
- end
28
+ def register_endpoint!(request_method, request_path, description, endpoint)
29
+ router = defined_path_is_dynamic?(request_path) ? @dynamic : @static
30
+ router.register_endpoint!(request_method, request_path, description, endpoint)
29
31
  end
30
32
 
31
33
  def fetch_endpoint(request_method, request_path)
32
34
  @static.fetch_endpoint(request_method, request_path) or
33
35
  @dynamic.fetch_endpoint(request_method, request_path) or
34
36
  Rack::App::Endpoint::NOT_FOUND
37
+
35
38
  end
36
39
 
37
40
  def merge!(router)
38
- raise(ArgumentError, "invalid router object, must be instance of #{self.class}") unless router.is_a?(self.class)
39
- @static.merge!(router.instance_variable_get(:@static))
40
- @dynamic.merge!(router.instance_variable_get(:@dynamic))
41
+ raise(ArgumentError, 'invalid router object, must implement :endpoints interface') unless router.respond_to?(:endpoints)
42
+ router.endpoints.each do |endpoint|
43
+ register_endpoint!(endpoint[:request_method], endpoint[:request_path], endpoint[:description], endpoint[:endpoint])
44
+ end
41
45
  nil
42
46
  end
43
47
 
@@ -49,6 +53,7 @@ class Rack::App::Router
49
53
  end
50
54
 
51
55
  def defined_path_is_dynamic?(path_str)
56
+ path_str = Rack::App::Utils.normalize_path(path_str)
52
57
  !!(path_str.to_s =~ /\/:\w+/i) or !!(path_str.to_s =~ /\/\*/i)
53
58
  end
54
59
 
@@ -0,0 +1,31 @@
1
+ class Rack::App::Router::Base
2
+
3
+ def endpoints
4
+ @endpoints ||= []
5
+ end
6
+
7
+ def register_endpoint!(request_method, request_path, description, endpoint)
8
+ endpoints.push(
9
+ {
10
+ :request_method => request_method.to_s.upcase,
11
+ :request_path => Rack::App::Utils.normalize_path(request_path),
12
+ :description => description,
13
+ :endpoint => endpoint
14
+ }
15
+ )
16
+
17
+ compile_registered_endpoints!
18
+ return endpoint
19
+ end
20
+
21
+ def fetch_endpoint(request_method, request_path)
22
+ raise('IMPLEMENTATION MISSING ERROR')
23
+ end
24
+
25
+ protected
26
+
27
+ def compile_registered_endpoints!
28
+ raise('IMPLEMENTATION MISSING ERROR')
29
+ end
30
+
31
+ end
@@ -1,44 +1,8 @@
1
- class Rack::App::Router::Dynamic
1
+ class Rack::App::Router::Dynamic < Rack::App::Router::Base
2
2
 
3
3
  PATH_PARAM = :"#{self}::PATH_PARAM".freeze
4
4
  PARTIAL = :"#{self}::PARTIAL".freeze
5
5
 
6
- # def endpoint_paths
7
- # end
8
-
9
- def add_endpoint(request_method, request_path, endpoint)
10
- request_path = Rack::App::Utils.normalize_path(request_path)
11
- @endpoint_paths << [request_method, request_path, endpoint.properties[:description]]
12
-
13
- current_cluster = main_cluster(request_method)
14
- path_params = {}
15
- break_build = false
16
-
17
- request_path.split('/').each.with_index do |path_part, index|
18
-
19
- new_cluster_name = if path_part_is_dynamic?(path_part)
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
-
27
- else
28
- path_part
29
- end
30
-
31
- current_cluster = (current_cluster[new_cluster_name] ||= {})
32
- break if break_build
33
-
34
- end
35
-
36
- current_cluster[:endpoint]= endpoint
37
- current_cluster[:endpoint].register_path_params_matcher(path_params)
38
-
39
- endpoint
40
- end
41
-
42
6
  def fetch_endpoint(request_method, request_path)
43
7
  normalized_request_path = Rack::App::Utils.normalize_path(request_path)
44
8
 
@@ -59,21 +23,10 @@ class Rack::App::Router::Dynamic
59
23
  current_cluster[:endpoint]
60
24
  end
61
25
 
62
- def merge!(router)
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))
65
- nil
66
- end
67
-
68
- def show_endpoints
69
- @endpoint_paths
70
- end
71
-
72
26
  protected
73
27
 
74
28
  def initialize
75
29
  @http_method_cluster = {}
76
- @endpoint_paths = []
77
30
  end
78
31
 
79
32
  def path_part_is_dynamic?(path_part_str)
@@ -103,4 +56,42 @@ class Rack::App::Router::Dynamic
103
56
  (path_part == '**' or path_part == '*')
104
57
  end
105
58
 
59
+ def compile_registered_endpoints!
60
+ @http_method_cluster.clear
61
+ endpoints.each do |endpoint_prop|
62
+ compile_endpoint(endpoint_prop[:request_method],endpoint_prop[:request_path],endpoint_prop[:endpoint])
63
+ end
64
+ end
65
+
66
+ def compile_endpoint(request_method, request_path, endpoint)
67
+
68
+ current_cluster = main_cluster(request_method)
69
+ path_params = {}
70
+ break_build = false
71
+
72
+ request_path.split('/').each.with_index do |path_part, index|
73
+
74
+ new_cluster_name = if path_part_is_dynamic?(path_part)
75
+ path_params[index]= path_part.sub(/^:/, '')
76
+ PATH_PARAM
77
+
78
+ elsif path_part_is_a_partial?(path_part)
79
+ break_build = true
80
+ PARTIAL
81
+
82
+ else
83
+ path_part
84
+ end
85
+
86
+ current_cluster = (current_cluster[new_cluster_name] ||= {})
87
+ break if break_build
88
+
89
+ end
90
+
91
+ current_cluster[:endpoint]= endpoint
92
+ current_cluster[:endpoint].register_path_params_matcher(path_params)
93
+
94
+ endpoint
95
+ end
96
+
106
97
  end
@@ -1,32 +1,21 @@
1
- class Rack::App::Router::Static
2
-
3
- #TD
4
- attr_reader :endpoints
5
-
6
- def add_endpoint(request_method, request_path, endpoint)
7
- @endpoints[[request_method.to_s.upcase, Rack::App::Utils.normalize_path(request_path)]]= endpoint
8
- end
1
+ class Rack::App::Router::Static < Rack::App::Router::Base
9
2
 
10
3
  def fetch_endpoint(request_method, request_path)
11
- @endpoints[[request_method, request_path]]
12
- end
13
-
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)
16
- @endpoints.merge!(static_router.instance_variable_get(:@endpoints))
17
- nil
4
+ mapped_endpoint_routes[[request_method, request_path]]
18
5
  end
19
6
 
20
- def show_endpoints
21
- @endpoints.map do |identifiers, endpoint|
22
- [identifiers, endpoint.properties[:description]].flatten
7
+ def compile_registered_endpoints!
8
+ mapped_endpoint_routes.clear
9
+ endpoints.each do |endpoint|
10
+ request_method, request_path, endpoint_object = endpoint[:request_method], endpoint[:request_path], endpoint[:endpoint]
11
+ mapped_endpoint_routes[[request_method.to_s.upcase, request_path]]= endpoint_object
23
12
  end
24
13
  end
25
14
 
26
15
  protected
27
16
 
28
- def initialize
29
- @endpoints = {}
17
+ def mapped_endpoint_routes
18
+ @mapped_endpoint_routes ||= {}
30
19
  end
31
20
 
32
21
  end
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.19.0
4
+ version: 0.20.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-02-14 00:00:00.000000000 Z
12
+ date: 2016-02-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -100,6 +100,7 @@ files:
100
100
  - lib/rack/app/params.rb
101
101
  - lib/rack/app/request_configurator.rb
102
102
  - lib/rack/app/router.rb
103
+ - lib/rack/app/router/base.rb
103
104
  - lib/rack/app/router/dynamic.rb
104
105
  - lib/rack/app/router/static.rb
105
106
  - lib/rack/app/serializer.rb