rack-way 0.0.1 → 0.0.2

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
  SHA256:
3
- metadata.gz: 016cf1e2d4ae57cf31b359733bf96c3510667bc9de887322c18a2118dcb54ba9
4
- data.tar.gz: b491da7388431e3d6b07a7b904af1d5d6b732b510c44904a0c93cd26978f0b07
3
+ metadata.gz: aec7c90f616cb7808ded767c3a85aa7c5859c127711769f5769a1251a2aa751d
4
+ data.tar.gz: 52c386fb62e85c43c7f1ef9a9d236c1736984fcaf89f041f3e521c3c6249750e
5
5
  SHA512:
6
- metadata.gz: c5948a9e5366dab3e86832df6f5fc66a9c837320689c3c6a2507c8f7b7a7c080272388159f44d464e07ba6ec1541377e9021ba83b33cb91958d0ff8005ba35de
7
- data.tar.gz: 1694ea321074055bdf57518e8dd6c16cf634b732c87561c4edd2f0a64af0c6ec714373acbfd9bdc59b6459ed9231a4c31f036b0151533aaaf26001ab79f9b538
6
+ metadata.gz: a920776a352b76981d86da5df95e48fdbd1017ec0a436e1c56e6d4205634fb4c9ebb0427766ef69645157d6d7f63c825345b54cb4efc3c26916604c27f16cb45
7
+ data.tar.gz: 70db0b57ac30337cccdc261d52129dc08dd151f1bb0b4159ccd29b2fadee3e4967eb5fca25d094e652fd400f320318f22e69d706c645e41ddeedcfc4339cea5e
@@ -1,60 +1,123 @@
1
- require 'erubis'
1
+ require 'erubi'
2
2
  require 'json'
3
3
  require 'rack'
4
4
 
5
5
  module Rack
6
6
  class Way
7
7
  module Action
8
- def render(content, headers: {"Content-Type" => "text/html"}, status: 200)
9
- Rack::Way::Action.render(content, headers: headers, status: status)
8
+ def html(content, status: 200)
9
+ Rack::Way::Action.html(content, status: status)
10
10
  end
11
11
 
12
- def render_erb(path, params = {}, status: 200)
13
- Rack::Way::Action.render_erb(path, params, status: status)
12
+ def html_response(content, status: 200)
13
+ Rack::Way::Action.html_response(content, status: status)
14
14
  end
15
15
 
16
- def render_json(content = {}, status: 200)
17
- Rack::Way::Action.render_json(content, status: status)
16
+ def view_response(path, view_params = {}, status: 200)
17
+ Rack::Way::Action.view_response(path, view_params, status: status)
18
18
  end
19
19
 
20
- def erb(path, params = {})
21
- Rack::Way::Action.erb(path, params)
20
+ def view(path, view_params = {}, status: 200, response_instance: false)
21
+ Rack::Way::Action.view(path, view_params, status: status, response_instance: response_instance)
22
+ end
23
+
24
+ def json(content = {}, status: 200)
25
+ Rack::Way::Action.json(content, status: status)
26
+ end
27
+
28
+ def json_response(content = {}, status: 200)
29
+ Rack::Way::Action.json_response(content, status: status)
30
+ end
31
+
32
+ def text(content, status: 200)
33
+ Rack::Way::Action.text(content, status: status)
34
+ end
35
+
36
+ def text_response(content, status: 200)
37
+ Rack::Way::Action.text_response(content, status: status)
38
+ end
39
+
40
+ def erb(path, view_params = {})
41
+ Rack::Way::Action.erb(path, view_params)
22
42
  end
23
43
 
24
44
  def redirect_to(url)
25
45
  Rack::Way::Action.redirect_to(url)
26
46
  end
27
47
 
48
+ def response(body = nil, status = 200, headers = {})
49
+ Rack::Response.new(body, status, headers)
50
+ end
51
+
28
52
  class << self
29
- def render(content, headers: {"Content-Type" => "text/html"}, status: 200)
30
- [status, headers, [content]]
53
+ def html(content, status: 200)
54
+ [status, { 'Content-Type' => 'text/html' }, [content]]
55
+ end
56
+
57
+ def html_response(content, status: 200)
58
+ Rack::Response.new(content, status, { 'Content-Type' => 'text/html' })
31
59
  end
32
60
 
33
- def render_erb(paths, params = {}, status: 200)
61
+ def view_response(paths, view_params = {}, status: 200)
62
+ view(paths, view_params, status: status, response_instance: true)
63
+ end
64
+
65
+ def view(paths, view_params = {}, status: 200, response_instance: false)
34
66
  if paths.kind_of?(Array)
35
- erb =
36
- paths.map { |path| erb(path, params) }.join
67
+ erb = paths.map { |path| erb("views/#{path}", view_params) }.join
37
68
  else
38
- erb = erb(paths, params)
69
+ erb = erb("views/#{paths}", view_params)
70
+ end
71
+
72
+ if response_instance
73
+ return Rack::Response.new(
74
+ erb,
75
+ status,
76
+ { 'Content-Type' => 'text/html' }
77
+ )
39
78
  end
40
79
 
41
- [status, {"Content-Type" => "text/html"}, [erb]]
80
+ [status, { 'Content-Type' => 'text/html' }, [erb]]
42
81
  end
43
82
 
44
- def render_json(content = {}, status: 200)
45
- [status, {"Content-Type" => "application/json"}, [content.to_json]]
83
+ def json(content = {}, status: 200)
84
+ [status, { 'Content-Type' => 'application/json' }, [content.to_json]]
46
85
  end
47
86
 
48
- def erb(path, params = {})
49
- Erubis::FastEruby
50
- .load_file("#{path}.html.erb")
51
- .result(params)
87
+ def json_response(content = {}, status: 200)
88
+ Rack::Response.new(
89
+ content.to_json,
90
+ status,
91
+ { 'Content-Type' => 'application/json' }
92
+ )
93
+ end
94
+
95
+ def text(content, status: 200)
96
+ [status, { 'Content-Type' => 'text/plain' }, [content]]
97
+ end
98
+
99
+ def text_response(content, status: 200)
100
+ Rack::Response.new(
101
+ content,
102
+ status,
103
+ { 'Content-Type' => 'text/plain' }
104
+ )
105
+ end
106
+
107
+ def erb(path, view_params = {})
108
+ @view = OpenStruct.new(view_params)
109
+
110
+ eval(Erubi::Engine.new(::File.read("#{path}.html.erb")).src)
52
111
  end
53
112
 
54
113
  def redirect_to(url)
55
- [302, {'Location' => url}, []]
114
+ [302, { 'Location' => url }, []]
115
+ end
116
+
117
+ def response(body = nil, status = 200, headers = {})
118
+ Rack::Response.new(body, status, headers)
56
119
  end
57
120
  end
58
121
  end
59
122
  end
60
- end
123
+ end
@@ -1,7 +1,7 @@
1
1
  module Rack
2
2
  class Way
3
3
  class Router
4
- class RequestBuilder
4
+ class BuildRequest
5
5
  def initialize(env)
6
6
  @env = env
7
7
  end
@@ -45,4 +45,4 @@ module Rack
45
45
  end
46
46
  end
47
47
  end
48
- end
48
+ end
@@ -12,9 +12,7 @@ module Rack
12
12
  end
13
13
 
14
14
  def match?(env)
15
- if has_params?
16
- return match_with_params?(env)
17
- end
15
+ return match_with_params?(env) if has_params?
18
16
 
19
17
  env['REQUEST_PATH'] == @path
20
18
  end
@@ -36,7 +34,7 @@ module Rack
36
34
  return false
37
35
  end
38
36
 
39
- matched =
37
+ matched_path_pieces =
40
38
  @splitted_path
41
39
  .map
42
40
  .with_index do |segment, i|
@@ -47,9 +45,9 @@ module Rack
47
45
  end
48
46
  end
49
47
 
50
- !matched.include?(false)
48
+ !matched_path_pieces.include?(false)
51
49
  end
52
50
  end
53
51
  end
54
52
  end
55
- end
53
+ end
@@ -1,5 +1,5 @@
1
1
  require_relative 'router/route.rb'
2
- require_relative 'router/request_builder.rb'
2
+ require_relative 'router/build_request.rb'
3
3
 
4
4
  module Rack
5
5
  class Way
@@ -7,23 +7,18 @@ module Rack
7
7
  attr_writer :not_found
8
8
 
9
9
  def initialize
10
- @routes =
11
- {
12
- 'GET' => [],
13
- 'POST' => [],
14
- 'DELETE' => [],
15
- 'PUT' => [],
16
- 'PATCH' => []
17
- }
18
-
19
- @namespaces = []
20
-
10
+ @routes = {}
11
+ %w[GET POST DELETE PUT TRACE OPTIONS PATCH].each do |method|
12
+ @routes[method] = { _instances: [] }
13
+ end
14
+ @scopes = []
15
+ @error = proc { |req, e| raise e }
21
16
  @not_found = proc { [404, {}, ['Not found']] }
22
17
  end
23
18
 
24
19
  def call(env)
20
+ request_builder = BuildRequest.new(env)
25
21
  route = match_route(env)
26
- request_builder = RequestBuilder.new(env)
27
22
 
28
23
  return render_not_found(request_builder.call) if route.nil?
29
24
 
@@ -32,33 +27,48 @@ module Rack
32
27
  end
33
28
 
34
29
  route.endpoint.new.call(request_builder.call(route))
30
+ rescue Exception => e
31
+ @error.call(request_builder.call, e)
35
32
  end
36
33
 
37
34
  def add(method, path, endpoint)
38
- route =
39
- Route.new("/" + @namespaces.join('/') + put_path_slash(path), endpoint)
40
-
41
- @routes[method.to_s.upcase].push route
35
+ joined_scopes = '/' << @scopes.join('/')
36
+
37
+ route = Route.new("#{joined_scopes}#{put_path_slash(path)}", endpoint)
38
+
39
+ if @scopes.size >= 1
40
+ first_level_scope = '/' << @scopes.first
41
+ if @routes[method.to_s.upcase][first_level_scope] == nil
42
+ @routes[method.to_s.upcase][first_level_scope] = { _instances: [] }
43
+ end
44
+ @routes[method.to_s.upcase][first_level_scope][:_instances].push(route)
45
+ else
46
+ @routes[method.to_s.upcase][:_instances].push(route)
47
+ end
42
48
  end
43
49
 
44
50
  def add_not_found(endpoint)
45
51
  @not_found = endpoint
46
52
  end
47
53
 
48
- def append_namespace(name)
49
- @namespaces.push(name)
54
+ def add_error(endpoint)
55
+ @error = endpoint
56
+ end
57
+
58
+ def append_scope(name)
59
+ @scopes.push(name)
50
60
  end
51
61
 
52
- def clear_last_namespace
53
- @namespaces =
54
- @namespaces.first(@namespaces.size - 1)
62
+ def clear_last_scope
63
+ @scopes = @scopes.first(@scopes.size - 1)
55
64
  end
56
65
 
57
66
  private
58
67
 
59
68
  def put_path_slash(path)
60
- return '' if (path == '/' || path == '') && @namespaces != []
61
- return '/' + path if @namespaces != []
69
+ return '' if (path == '/' || path == '') && @scopes != []
70
+ return '/' << path if @scopes != []
71
+
62
72
  path
63
73
  end
64
74
 
@@ -69,8 +79,22 @@ module Rack
69
79
  end
70
80
 
71
81
  def match_route(env)
72
- @routes[env["REQUEST_METHOD"]]
73
- .detect { |route| route.match?(env) }
82
+ matched_first_level_scope = nil
83
+
84
+ @routes[env['REQUEST_METHOD']].each do |first_level_scope, _v|
85
+ next if first_level_scope == :_instances
86
+
87
+ if env['REQUEST_PATH'].start_with?(first_level_scope) || first_level_scope.start_with?('/:')
88
+ matched_first_level_scope = first_level_scope
89
+ break
90
+ end
91
+ end
92
+
93
+ if matched_first_level_scope
94
+ return @routes[env['REQUEST_METHOD']][matched_first_level_scope][:_instances].detect { |route| route.match?(env) }
95
+ end
96
+
97
+ @routes[env['REQUEST_METHOD']][:_instances].detect { |route| route.match?(env) }
74
98
  end
75
99
  end
76
100
  end
data/lib/rack-way.rb CHANGED
@@ -1,53 +1,59 @@
1
- require 'rack-way/router'
2
- require 'rack-way/action'
1
+ require_relative 'rack-way/router'
2
+ require_relative 'rack-way/action'
3
3
 
4
4
  module Rack
5
5
  class Way
6
6
  include Action
7
7
 
8
- def initialize
9
- @router = Router.new
8
+ def initialize(router: Router.new)
9
+ @router = router
10
10
  end
11
11
 
12
- def draw_app(&block)
12
+ def app(&block)
13
13
  instance_eval(&block)
14
14
 
15
15
  @router
16
16
  end
17
17
 
18
- def namespace(name, &block)
19
- @router.append_namespace(name)
18
+ def scope(name, &block)
19
+ @router.append_scope(name)
20
20
  instance_eval(&block)
21
21
 
22
- @router.clear_last_namespace
22
+ @router.clear_last_scope
23
23
  end
24
24
 
25
- def root(endpoint)
26
- @router.add('GET', '', endpoint)
25
+ def root(endpoint = -> { }, &block)
26
+ if block_given?
27
+ @router.add('GET', '', block)
28
+ else
29
+ @router.add('GET', '', endpoint)
30
+ end
27
31
  end
28
32
 
29
- def not_found(endpoint)
30
- @router.add_not_found(endpoint)
33
+ def not_found(endpoint = -> { }, &block)
34
+ if block_given?
35
+ @router.add_not_found(block)
36
+ else
37
+ @router.add_not_found(endpoint)
38
+ end
31
39
  end
32
40
 
33
- def get(path, endpoint)
34
- @router.add('GET', path, endpoint)
41
+ def error(endpoint = -> { }, &block)
42
+ if block_given?
43
+ @router.add_error(block)
44
+ else
45
+ @router.add_error(endpoint)
46
+ end
35
47
  end
36
48
 
37
- def post(path, endpoint)
38
- @router.add('POST', path, endpoint)
39
- end
40
-
41
- def delete(path, endpoint)
42
- @router.add('DELETE', path, endpoint)
43
- end
44
-
45
- def put(path, endpoint)
46
- @router.add('PUT', path, endpoint)
47
- end
48
-
49
- def patch(path, endpoint)
50
- @router.add('PATCH', path, endpoint)
49
+ %w[GET POST DELETE PUT TRACE OPTIONS PATCH].each do |http_method|
50
+ define_method(http_method.downcase.to_sym) do |path, endpoint = -> { }, &block|
51
+ if block.respond_to?(:call)
52
+ @router.add(http_method, path, block)
53
+ else
54
+ @router.add(http_method, path, endpoint)
55
+ end
56
+ end
51
57
  end
52
58
  end
53
59
  end
metadata CHANGED
@@ -1,45 +1,44 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-way
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
- - Henrique Fernandez
7
+ - Henrique F. Teixeira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2019-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: erubis
14
+ name: erubi
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.7'
19
+ version: '1.12'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.7'
26
+ version: '1.12'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rack
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '2.0'
33
+ version: '3.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '2.0'
41
- description: A very little framework that encourages ruby developers to build 'pure
42
- Rack' applications when working in projects that need high performance.
40
+ version: '3.0'
41
+ description: A little web framework that composes well with Rack gems ecossystem
43
42
  email: hriqueft@gmail.com
44
43
  executables: []
45
44
  extensions: []
@@ -48,7 +47,7 @@ files:
48
47
  - lib/rack-way.rb
49
48
  - lib/rack-way/action.rb
50
49
  - lib/rack-way/router.rb
51
- - lib/rack-way/router/request_builder.rb
50
+ - lib/rack-way/router/build_request.rb
52
51
  - lib/rack-way/router/route.rb
53
52
  homepage: https://github.com/henriquefernandez/rack-way
54
53
  licenses:
@@ -69,8 +68,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
68
  - !ruby/object:Gem::Version
70
69
  version: '0'
71
70
  requirements: []
72
- rubygems_version: 3.0.3
71
+ rubygems_version: 3.4.3
73
72
  signing_key:
74
73
  specification_version: 4
75
- summary: '"rack-way" come with a router and helper functions to build pure rack projects.'
74
+ summary: '"rack-way" come with a router and helper functions to build pure Rack projects.'
76
75
  test_files: []