rage-rb 0.1.2 → 0.2.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
  SHA256:
3
- metadata.gz: 3aacbd2e65fda6a0e7a7f7a24e67f718e55fd66bd3e0a6c6d3a1a319294be44b
4
- data.tar.gz: 40d3d2225cf40bf0f125884afd99a86b47b34f6dddce5fdeea0a9b5239fdfd0a
3
+ metadata.gz: 4570bfdf4f86125be1d6c35b9ccba08a956fe8a20dac392e5ae914db347ad396
4
+ data.tar.gz: '019e919339f97e641e65f9aecc91c02740875ebcbe71edf14a07144950e47f07'
5
5
  SHA512:
6
- metadata.gz: 288d8f707d78901e9c209cc3af075bcf3c37c7ef0ff4c24355702bd3898ab2e041afc5ab028e4cbe09e26a95f7297e01af03430ac6f126fd5d6cfa0b772f0e5e
7
- data.tar.gz: 5c70306e1901176e5da774dcb153e21c382359825a2527a7de97ce75772dfdf6a510ac3ffba05486b684b55634a4173596d9469306571acf58ffff274a6185c9
6
+ metadata.gz: 9ad7b89eb46407831ae723c1c15b87ea12b898ae1784989374b07fcffcc3afcff2a237861c94565afe4c5f486b99edb15022b539ab9cad142778e4b2718b92ea
7
+ data.tar.gz: 471cb6bcbf294d9a4eb3e5fd69827f26661effafc1df20447b8e7eddd0816aff411266d22104988978c46318dc08c4fa753f1e1a4fa1327268a5f24d8851ac5a
data/.yardopts CHANGED
@@ -1 +1 @@
1
- --exclude lib/rage/templates --markup markdown --no-private
1
+ --exclude lib/rage/templates --markup markdown --no-private -o doc
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2023-09-27
4
+
5
+ ### Added
6
+
7
+ - Gem configuration by env.
8
+ - Add `skip_before_action`.
9
+ - Add `rescue_from`.
10
+ - Add `Fiber.await`.
11
+ - Support the `defaults` route option.
12
+
13
+ ### Fixed
14
+
15
+ - Ignore trailing slashes in the URLs.
16
+ - Support constraints in routes with optional params.
17
+ - Make the `root` routes helper work correctly with scopes.
18
+ - Convert objects to string when rendering text.
19
+
3
20
  ## [0.1.0] - 2023-09-15
4
21
 
5
22
  - Initial release
data/Gemfile CHANGED
@@ -11,3 +11,6 @@ gem "rspec", "~> 3.0"
11
11
 
12
12
  gem "pg"
13
13
  gem "mysql2"
14
+
15
+ gem "benchmark-ips"
16
+ gem "mustache"
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- <p align="center"><img height="200" src="https://github.com/rage-rb/rage/assets/2270393/d0f0834f-50e4-4b1b-a564-1f241c4ec149" /></p>
1
+ <p align="center"><img height="200" src="https://github.com/rage-rb/rage/assets/2270393/9d06e0a4-5c20-49c7-b51d-e16ce8f1e1b7" /></p>
2
2
 
3
3
  # Rage
4
4
 
@@ -141,7 +141,7 @@ end
141
141
 
142
142
  Version | Changes
143
143
  ------- |------------
144
- 0.2 | Gem configuration by env.<br>Add `skip_before_action`.<br>Add `rescue_from`.<br>Router updates:<br>&emsp;• make the `root` helper work correctly with `scope`;<br>&emsp;• support the `defaults` option;
144
+ 0.2 :white_check_mark: | ~~Gem configuration by env.<br>Add `skip_before_action`.<br>Add `rescue_from`.<br>Router updates:<br>&emsp;• make the `root` helper work correctly with `scope`;<br>&emsp;• support the `defaults` option;~~
145
145
  0.3 | CLI updates:<br>&emsp;• `routes` task;<br>&emsp;• `console` task;<br>Support the `:if` and `:unless` options in `before_action`.<br>Allow to set response headers.
146
146
  0.4 | Expose the `params` object.<br>Support header authentication with `authenticate_with_http_token`.<br>Router updates:<br>&emsp;• add the `resources` route helper;<br>&emsp;• add the `namespace` route helper;<br>&emsp;• support regexp constraints;
147
147
  0.5 | Implement Iodine-based equivalent of `ActionController::Live`.<br>Use `ActionDispatch::RemoteIp`.
@@ -19,7 +19,7 @@ class Rage::Application
19
19
  end
20
20
 
21
21
  rescue => e
22
- [500, {}, ["#{e.class}:#{e.message}\n\n#{e.backtrace.join("\n")}"]]
22
+ [500, {}, ["#{e.class}:#{e.message}\n\n#{e.backtrace.join("\n")}"]] # TODO: check Rage.env
23
23
 
24
24
  ensure
25
25
  # notify Iodine the request can now be served
@@ -193,7 +193,7 @@ class RageController::API
193
193
  json.is_a?(String) ? json : json.to_json
194
194
  else
195
195
  __set_header("content-type", "text/plain; charset=utf-8")
196
- plain
196
+ plain.to_s
197
197
  end
198
198
 
199
199
  @__status = 200
@@ -12,7 +12,7 @@ class Rage::Router::Backend
12
12
  @constrainer = Rage::Router::Constrainer.new({})
13
13
  end
14
14
 
15
- def on(method, path, handler, constraints: {})
15
+ def on(method, path, handler, constraints: {}, defaults: nil)
16
16
  raise "Path could not be empty" if path&.empty?
17
17
 
18
18
  if match_index = (path =~ OPTIONAL_PARAM_REGEXP)
@@ -21,8 +21,8 @@ class Rage::Router::Backend
21
21
  path_full = path.sub(OPTIONAL_PARAM_REGEXP, "/#{$1}")
22
22
  path_optional = path.sub(OPTIONAL_PARAM_REGEXP, "")
23
23
 
24
- on(method, path_full, handler, constraints: constraints)
25
- on(method, path_optional, handler, constraints: constraints)
24
+ on(method, path_full, handler, constraints: constraints, defaults: defaults)
25
+ on(method, path_optional, handler, constraints: constraints, defaults: defaults)
26
26
  return
27
27
  end
28
28
 
@@ -42,7 +42,7 @@ class Rage::Router::Backend
42
42
  handler = ->(env, _params) { orig_handler.call(env) }
43
43
  end
44
44
 
45
- __on(method, path, handler, constraints)
45
+ __on(method, path, handler, constraints, defaults)
46
46
  end
47
47
 
48
48
  def lookup(env)
@@ -52,7 +52,7 @@ class Rage::Router::Backend
52
52
 
53
53
  private
54
54
 
55
- def __on(method, path, handler, constraints)
55
+ def __on(method, path, handler, constraints, defaults)
56
56
  @constrainer.validate_constraints(constraints)
57
57
  # Let the constrainer know if any constraints are being used now
58
58
  @constrainer.note_usage(constraints)
@@ -159,7 +159,7 @@ class Rage::Router::Backend
159
159
  end
160
160
  end
161
161
 
162
- route = { method: method, path: path, pattern: pattern, params: params, constraints: constraints, handler: handler }
162
+ route = { method: method, path: path, pattern: pattern, params: params, constraints: constraints, handler: handler, defaults: defaults }
163
163
  @routes << route
164
164
  current_node.add_route(route, @constrainer)
165
165
  end
@@ -16,6 +16,7 @@ class Rage::Router::DSL
16
16
 
17
17
  @path_prefixes = []
18
18
  @module_prefixes = []
19
+ @defaults = []
19
20
  end
20
21
 
21
22
  # Register a new GET route.
@@ -23,10 +24,13 @@ class Rage::Router::DSL
23
24
  # @param path [String] the path for the route handler
24
25
  # @param to [String] the route handler in the format of "controller#action"
25
26
  # @param constraints [Hash] a hash of constraints for the route
27
+ # @param defaults [Hash] a hash of default parameters for the route
26
28
  # @example
27
29
  # get "/photos/:id", to: "photos#show", constraints: { host: /myhost/ }
28
- def get(path, to:, constraints: nil)
29
- __on("GET", path, to, constraints)
30
+ # @example
31
+ # get "/photos(/:id)", to: "photos#show", defaults: { id: "-1" }
32
+ def get(path, to:, constraints: nil, defaults: nil)
33
+ __on("GET", path, to, constraints, defaults)
30
34
  end
31
35
 
32
36
  # Register a new POST route.
@@ -34,10 +38,13 @@ class Rage::Router::DSL
34
38
  # @param path [String] the path for the route handler
35
39
  # @param to [String] the route handler in the format of "controller#action"
36
40
  # @param constraints [Hash] a hash of constraints for the route
41
+ # @param defaults [Hash] a hash of default parameters for the route
37
42
  # @example
38
43
  # post "/photos", to: "photos#create", constraints: { host: /myhost/ }
39
- def post(path, to:, constraints: nil)
40
- __on("POST", path, to, constraints)
44
+ # @example
45
+ # post "/photos", to: "photos#create", defaults: { format: "jpg" }
46
+ def post(path, to:, constraints: nil, defaults: nil)
47
+ __on("POST", path, to, constraints, defaults)
41
48
  end
42
49
 
43
50
  # Register a new PUT route.
@@ -45,10 +52,13 @@ class Rage::Router::DSL
45
52
  # @param path [String] the path for the route handler
46
53
  # @param to [String] the route handler in the format of "controller#action"
47
54
  # @param constraints [Hash] a hash of constraints for the route
55
+ # @param defaults [Hash] a hash of default parameters for the route
48
56
  # @example
49
57
  # put "/photos/:id", to: "photos#update", constraints: { host: /myhost/ }
50
- def put(path, to:, constraints: nil)
51
- __on("PUT", path, to, constraints)
58
+ # @example
59
+ # put "/photos(/:id)", to: "photos#update", defaults: { id: "-1" }
60
+ def put(path, to:, constraints: nil, defaults: nil)
61
+ __on("PUT", path, to, constraints, defaults)
52
62
  end
53
63
 
54
64
  # Register a new PATCH route.
@@ -56,10 +66,13 @@ class Rage::Router::DSL
56
66
  # @param path [String] the path for the route handler
57
67
  # @param to [String] the route handler in the format of "controller#action"
58
68
  # @param constraints [Hash] a hash of constraints for the route
69
+ # @param defaults [Hash] a hash of default parameters for the route
59
70
  # @example
60
71
  # patch "/photos/:id", to: "photos#update", constraints: { host: /myhost/ }
61
- def patch(path, to:, constraints: nil)
62
- __on("PATCH", path, to, constraints)
72
+ # @example
73
+ # patch "/photos(/:id)", to: "photos#update", defaults: { id: "-1" }
74
+ def patch(path, to:, constraints: nil, defaults: nil)
75
+ __on("PATCH", path, to, constraints, defaults)
63
76
  end
64
77
 
65
78
  # Register a new DELETE route.
@@ -67,10 +80,13 @@ class Rage::Router::DSL
67
80
  # @param path [String] the path for the route handler
68
81
  # @param to [String] the route handler in the format of "controller#action"
69
82
  # @param constraints [Hash] a hash of constraints for the route
83
+ # @param defaults [Hash] a hash of default parameters for the route
70
84
  # @example
71
85
  # delete "/photos/:id", to: "photos#destroy", constraints: { host: /myhost/ }
72
- def delete(path, to:, constraints: nil)
73
- __on("DELETE", path, to, constraints)
86
+ # @example
87
+ # delete "/photos(/:id)", to: "photos#destroy", defaults: { id: "-1" }
88
+ def delete(path, to:, constraints: nil, defaults: nil)
89
+ __on("DELETE", path, to, constraints, defaults)
74
90
  end
75
91
 
76
92
  # Register a new route pointing to '/'.
@@ -79,7 +95,7 @@ class Rage::Router::DSL
79
95
  # @example
80
96
  # root to: "photos#index"
81
97
  def root(to:)
82
- __on("GET", "/", to, nil)
98
+ __on("GET", "/", to, nil, nil)
83
99
  end
84
100
 
85
101
  # Scopes a set of routes to the given default options.
@@ -115,9 +131,22 @@ class Rage::Router::DSL
115
131
  @module_prefixes.pop if opts[:module]
116
132
  end
117
133
 
134
+ # Specify default parameters for a set of routes.
135
+ #
136
+ # @param defaults [Hash] a hash of default parameters
137
+ # @example
138
+ # defaults id: "-1", format: "jpg" do
139
+ # get "photos/(:id)", to: "photos#index"
140
+ # end
141
+ def defaults(defaults, &block)
142
+ @defaults << defaults
143
+ instance_eval &block
144
+ @defaults.pop
145
+ end
146
+
118
147
  private
119
148
 
120
- def __on(method, path, to, constraints)
149
+ def __on(method, path, to, constraints, defaults)
121
150
  if path != "/"
122
151
  path = "/#{path}" unless path.start_with?("/")
123
152
  path = path.delete_suffix("/") if path.end_with?("/")
@@ -129,11 +158,12 @@ class Rage::Router::DSL
129
158
 
130
159
  path_prefix = @path_prefixes.any? ? "/#{@path_prefixes.join("/")}" : nil
131
160
  module_prefix = @module_prefixes.any? ? "#{@module_prefixes.join("/")}/" : nil
161
+ defaults = (defaults ? @defaults + [defaults] : @defaults).reduce(&:merge)
132
162
 
133
163
  if to.is_a?(String)
134
- @router.on(method, "#{path_prefix}#{path}", "#{module_prefix}#{to}", constraints: constraints || {})
164
+ @router.on(method, "#{path_prefix}#{path}", "#{module_prefix}#{to}", constraints: constraints || {}, defaults: defaults)
135
165
  else
136
- @router.on(method, "#{path_prefix}#{path}", to, constraints: constraints || {})
166
+ @router.on(method, "#{path_prefix}#{path}", to, constraints: constraints || {}, defaults: defaults)
137
167
  end
138
168
  end
139
169
  end
@@ -22,7 +22,7 @@ class Rage::Router::HandlerStorage
22
22
  params: params,
23
23
  constraints: constraints,
24
24
  handler: route[:handler],
25
- create_params_object: compile_create_params_object(params)
25
+ create_params_object: compile_create_params_object(params, route[:defaults])
26
26
  }
27
27
 
28
28
  constraints_keys = constraints.keys
@@ -47,13 +47,19 @@ class Rage::Router::HandlerStorage
47
47
 
48
48
  private
49
49
 
50
- def compile_create_params_object(param_keys)
50
+ def compile_create_params_object(param_keys, defaults)
51
51
  lines = []
52
52
 
53
53
  param_keys.each_with_index do |key, i|
54
54
  lines << "'#{key}' => param_values[#{i}]"
55
55
  end
56
56
 
57
+ if defaults
58
+ defaults.except(*param_keys.map(&:to_sym)).each do |key, value|
59
+ lines << "'#{key}' => '#{value}'"
60
+ end
61
+ end
62
+
57
63
  eval "->(param_values) { { #{lines.join(',')} } }"
58
64
  end
59
65
 
data/lib/rage/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rage
4
- VERSION = "0.1.2"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rage-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Samoilov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-25 00:00:00.000000000 Z
11
+ date: 2023-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor