tarquinn 0.2.0 → 0.3.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 +4 -4
- data/.circleci/config.yml +15 -3
- data/.gitignore +9 -0
- data/.rubocop.yml +29 -0
- data/.rubocop_todo.yml +13 -0
- data/Dockerfile +2 -2
- data/Gemfile +15 -0
- data/README.md +5 -1
- data/Rakefile +5 -0
- data/config/check_specs.yml +3 -0
- data/config/rubycritc.rb +12 -0
- data/config/yardstick.rb +13 -0
- data/config/yardstick.yml +33 -0
- data/lib/tarquinn/class_methods.rb +43 -12
- data/lib/tarquinn/condition/action_checker.rb +24 -7
- data/lib/tarquinn/condition/method_caller.rb +25 -8
- data/lib/tarquinn/condition/proc_runner.rb +18 -7
- data/lib/tarquinn/condition.rb +57 -4
- data/lib/tarquinn/controller.rb +56 -13
- data/lib/tarquinn/redirection_config.rb +92 -0
- data/lib/tarquinn/redirection_handler.rb +97 -0
- data/lib/tarquinn/request_handler.rb +61 -0
- data/lib/tarquinn/request_handler_builder.rb +84 -0
- data/lib/tarquinn/version.rb +3 -1
- data/lib/tarquinn.rb +74 -8
- data/spec/dummy/Rakefile +8 -0
- data/spec/dummy/app/assets/images/.keep +0 -0
- data/spec/dummy/app/assets/stylesheets/application.css +1 -0
- data/spec/dummy/app/channels/application_cable/channel.rb +6 -0
- data/spec/dummy/app/channels/application_cable/connection.rb +6 -0
- data/spec/dummy/app/controllers/application_controller.rb +4 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/controllers/tarquinn/dummy_controller.rb +43 -0
- data/spec/dummy/app/controllers/tarquinn/dummy_route_controller.rb +37 -0
- data/spec/dummy/app/helpers/application_helper.rb +4 -0
- data/spec/dummy/app/jobs/application_job.rb +9 -0
- data/spec/dummy/app/mailers/application_mailer.rb +6 -0
- data/spec/dummy/app/models/application_record.rb +5 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/views/layouts/application.html.erb +15 -0
- data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/spec/dummy/app/views/tarquinn/dummy_route/index.html +0 -0
- data/spec/dummy/app/views/tarquinn/dummy_route/new.html +0 -0
- data/spec/dummy/bin/rails +6 -0
- data/spec/dummy/bin/rake +6 -0
- data/spec/dummy/bin/setup +35 -0
- data/spec/dummy/config/application.rb +24 -0
- data/spec/dummy/config/boot.rb +7 -0
- data/spec/dummy/config/cable.yml +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +7 -0
- data/spec/dummy/config/environments/development.rb +69 -0
- data/spec/dummy/config/environments/production.rb +89 -0
- data/spec/dummy/config/environments/test.rb +62 -0
- data/spec/dummy/config/initializers/content_security_policy.rb +26 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +10 -0
- data/spec/dummy/config/initializers/inflections.rb +17 -0
- data/spec/dummy/config/initializers/permissions_policy.rb +12 -0
- data/spec/dummy/config/locales/en.yml +33 -0
- data/spec/dummy/config/puma.rb +45 -0
- data/spec/dummy/config/routes.rb +8 -0
- data/spec/dummy/config/storage.yml +34 -0
- data/spec/dummy/config.ru +8 -0
- data/spec/dummy/db/schema.rb +16 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/log/.keep +0 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/public/apple-touch-icon.png +0 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/storage/.keep +0 -0
- data/spec/dummy/tmp/.keep +0 -0
- data/spec/dummy/tmp/pids/.keep +0 -0
- data/spec/dummy/tmp/storage/.keep +0 -0
- data/spec/lib/tarquinn/condition/action_checker_spec.rb +4 -2
- data/spec/lib/tarquinn/condition/method_caller_spec.rb +5 -3
- data/spec/lib/tarquinn/condition/proc_runner_spec.rb +56 -4
- data/spec/lib/tarquinn/condition_spec.rb +53 -0
- data/spec/lib/tarquinn/controller_spec.rb +83 -10
- data/spec/lib/tarquinn/{config_spec.rb → redirection_config_spec.rb} +3 -1
- data/spec/lib/tarquinn/{handler_spec.rb → redirection_handler_spec.rb} +21 -3
- data/spec/lib/tarquinn/request_handler_builder_spec.rb +15 -0
- data/spec/lib/tarquinn/{engine_spec.rb → request_handler_spec.rb} +5 -3
- data/spec/lib/tarquinn_spec.rb +176 -13
- data/spec/spec_helper.rb +16 -6
- data/spec/support/shared_examples/config.rb +4 -2
- data/tarquinn.gemspec +8 -12
- metadata +73 -100
- data/lib/tarquinn/builder.rb +0 -28
- data/lib/tarquinn/concern.rb +0 -17
- data/lib/tarquinn/config.rb +0 -39
- data/lib/tarquinn/engine.rb +0 -31
- data/lib/tarquinn/handler.rb +0 -49
- data/spec/lib/tarquinn/builder_spec.rb +0 -13
- data/spec/support/models/tarquinn/dummy_controller.rb +0 -43
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 40a2cd722ae376e627aba7f4d4e10b69df45b74593d89dbafd68b9f6e4cb635a
|
4
|
+
data.tar.gz: 7c29d8a9763a3f0ae3e71945ea16b690628b0a3fef4f3603d891dbe791a4a0ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b63bfd5ba62e690b145bc4394e1913f28e5756b325b8301fb1b467d1386b282950095d36638ee407c2404e30d098a79de7868285a390b643fb2e84b275f16c6
|
7
|
+
data.tar.gz: '0286cd856bbaa74643cf9174952aff220ed75e3b81ff22f07c451b95bf226a78919ac8e306bbde78b9ab2f0577cd7b707bf4b0f4d25da69830b5340d58bfc5d9'
|
data/.circleci/config.yml
CHANGED
@@ -22,7 +22,7 @@ workflows:
|
|
22
22
|
jobs:
|
23
23
|
test:
|
24
24
|
docker:
|
25
|
-
- image: darthjee/
|
25
|
+
- image: darthjee/circleci_ruby_331:1.0.1
|
26
26
|
environment:
|
27
27
|
PROJECT: tarquinn
|
28
28
|
steps:
|
@@ -41,7 +41,7 @@ jobs:
|
|
41
41
|
command: cc-test-reporter after-build --exit-code $?
|
42
42
|
checks:
|
43
43
|
docker:
|
44
|
-
- image: darthjee/
|
44
|
+
- image: darthjee/circleci_ruby_331:1.0.1
|
45
45
|
environment:
|
46
46
|
PROJECT: tarquinn
|
47
47
|
steps:
|
@@ -49,12 +49,24 @@ jobs:
|
|
49
49
|
- run:
|
50
50
|
name: Bundle Install
|
51
51
|
command: bundle install
|
52
|
+
- run:
|
53
|
+
name: Rubocop
|
54
|
+
command: rubocop
|
55
|
+
- run:
|
56
|
+
name: Yardstick coverage check
|
57
|
+
command: bundle exec rake verify_measurements
|
52
58
|
- run:
|
53
59
|
name: Check version documentation
|
54
60
|
command: check_readme.sh
|
61
|
+
- run:
|
62
|
+
name: Rubycritcs check
|
63
|
+
command: rubycritic.sh
|
64
|
+
- run:
|
65
|
+
name: Check unit tests
|
66
|
+
command: check_specs
|
55
67
|
build-and-release:
|
56
68
|
docker:
|
57
|
-
- image: darthjee/
|
69
|
+
- image: darthjee/circleci_ruby_331:1.0.1
|
58
70
|
environment:
|
59
71
|
PROJECT: tarquinn
|
60
72
|
steps:
|
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
TargetRubyVersion: 3.3
|
5
|
+
NewCops: enable
|
6
|
+
|
7
|
+
Lint/BooleanSymbol:
|
8
|
+
Enabled: false
|
9
|
+
|
10
|
+
Lint/EmptyBlock:
|
11
|
+
Exclude:
|
12
|
+
- spec/dummy/db/schema.rb
|
13
|
+
|
14
|
+
Metrics/BlockLength:
|
15
|
+
Exclude:
|
16
|
+
- spec/**/*_spec.rb
|
17
|
+
|
18
|
+
Lint/MissingSuper:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Lint/ScriptPermission:
|
22
|
+
Exclude:
|
23
|
+
- spec/dummy/bin/*
|
24
|
+
|
25
|
+
Naming/BlockForwarding:
|
26
|
+
Enabled: false
|
27
|
+
|
28
|
+
Style/ArgumentsForwarding:
|
29
|
+
Enabled: false
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2024-07-10 16:10:57 UTC using RuboCop version 1.59.0.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 9
|
10
|
+
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
|
11
|
+
# AllowedMethods: refine
|
12
|
+
Metrics/BlockLength:
|
13
|
+
Max: 87
|
data/Dockerfile
CHANGED
data/Gemfile
CHANGED
@@ -1,3 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
source 'https://rubygems.org'
|
2
4
|
|
5
|
+
gem 'bundler', '~> 2.5.13'
|
6
|
+
gem 'pry', '~> 0.14.2'
|
7
|
+
gem 'pry-nav', '~> 1.0.0'
|
8
|
+
gem 'rails', '7.0.4.3'
|
9
|
+
gem 'rake', '~> 13.1.0'
|
10
|
+
gem 'rspec', '~> 3.12.0'
|
11
|
+
gem 'rspec-rails', '6.0.2'
|
12
|
+
gem 'rubycritic', '4.9.1'
|
13
|
+
gem 'simplecov', '~> 0.22.0'
|
14
|
+
gem 'sqlite3', '1.4.2'
|
15
|
+
gem 'yard', '0.9.36'
|
16
|
+
gem 'yardstick', '0.9.9'
|
17
|
+
|
3
18
|
gemspec
|
data/README.md
CHANGED
@@ -11,10 +11,14 @@ Tarquinn
|
|
11
11
|
|
12
12
|
Yard Documentation
|
13
13
|
-------------------
|
14
|
-
[https://www.rubydoc.info/gems/tarquinn/0.
|
14
|
+
[https://www.rubydoc.info/gems/tarquinn/0.3.0](https://www.rubydoc.info/gems/tarquinn/0.3.0)
|
15
15
|
|
16
16
|
This gem makes easier to controll generic redirection
|
17
17
|
|
18
|
+
Current Release: [0.3.0](https://github.com/darthjee/tarquinn/tree/0.3.0)
|
19
|
+
|
20
|
+
[Next release](https://github.com/darthjee/tarquinn/compare/0.3.0...master)
|
21
|
+
|
18
22
|
Getting started
|
19
23
|
---------------
|
20
24
|
1. Add Tarquinn to your `Gemfile` and `bundle install`:
|
data/Rakefile
CHANGED
data/config/rubycritc.rb
ADDED
data/config/yardstick.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'yardstick/rake/measurement'
|
4
|
+
require 'yardstick/rake/verify'
|
5
|
+
require 'yaml'
|
6
|
+
|
7
|
+
options = YAML.load_file('config/yardstick.yml')
|
8
|
+
|
9
|
+
Yardstick::Rake::Measurement.new(:yardstick_measure, options) do |measurement|
|
10
|
+
measurement.output = 'measurement/report.txt'
|
11
|
+
end
|
12
|
+
|
13
|
+
Yardstick::Rake::Verify.new(:verify_measurements, options)
|
@@ -0,0 +1,33 @@
|
|
1
|
+
threshold: 88.1
|
2
|
+
require_exact_threshold: false
|
3
|
+
rules:
|
4
|
+
ApiTag::Presence:
|
5
|
+
enabled: true
|
6
|
+
exclude: []
|
7
|
+
ApiTag::Inclusion:
|
8
|
+
enabled: true
|
9
|
+
exclude: []
|
10
|
+
ApiTag::ProtectedMethod:
|
11
|
+
enabled: true
|
12
|
+
exclude: []
|
13
|
+
ApiTag::PrivateMethod:
|
14
|
+
enabled: true
|
15
|
+
exclude: []
|
16
|
+
ExampleTag:
|
17
|
+
enabled: true
|
18
|
+
exclude: []
|
19
|
+
ReturnTag:
|
20
|
+
enabled: true
|
21
|
+
exclude: []
|
22
|
+
Summary::Presence:
|
23
|
+
enabled: true
|
24
|
+
exclude: []
|
25
|
+
Summary::Length:
|
26
|
+
enabled: true
|
27
|
+
exclude: []
|
28
|
+
Summary::Delimiter:
|
29
|
+
enabled: true
|
30
|
+
exclude: []
|
31
|
+
Summary::SingleLine:
|
32
|
+
enabled: true
|
33
|
+
exclude: []
|
@@ -1,17 +1,48 @@
|
|
1
|
-
|
2
|
-
def skip_redirection(redirection, *actions)
|
3
|
-
redirector_builder.add_skip_action(redirection, *actions)
|
4
|
-
end
|
1
|
+
# frozen_string_literal: true
|
5
2
|
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
module Tarquinn
|
4
|
+
# @api public
|
5
|
+
#
|
6
|
+
# Methods added by Tarquinn
|
7
|
+
module ClassMethods
|
8
|
+
# Creates a redirection rule
|
9
|
+
#
|
10
|
+
# The rule name defines which method will be called when checking the path of redirection
|
11
|
+
#
|
12
|
+
# @param (see Tarquinn::RequestHandlerBuilder#add_redirection_config)
|
13
|
+
# @return (see Tarquinn::RequestHandlerBuilder#add_redirection_config)
|
14
|
+
def redirection_rule(redirection, *methods, &)
|
15
|
+
redirector_builder.add_redirection_config(redirection, *methods, &)
|
16
|
+
end
|
9
17
|
|
10
|
-
|
11
|
-
|
12
|
-
|
18
|
+
# Attaches a condition to skip a redirection based on route (controller action)
|
19
|
+
#
|
20
|
+
# When any of the skip rules is met the redirection is skipped
|
21
|
+
#
|
22
|
+
# @param (see Tarquinn::RequestHandlerBuilder#add_skip_action)
|
23
|
+
# @return (see Tarquinn::RequestHandlerBuilder#add_skip_action)
|
24
|
+
def skip_redirection(redirection, *actions)
|
25
|
+
redirector_builder.add_skip_action(redirection, *actions)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Attaches conditions to skip a redirection
|
29
|
+
#
|
30
|
+
# Methods and blocks are ran and if any returns true, the redirec is skipped
|
31
|
+
#
|
32
|
+
# @param (see Tarquinn::RequestHandlerBuilder#add_skip_config)
|
33
|
+
# @return (see Tarquinn::RequestHandlerBuilder#add_skip_config)
|
34
|
+
def skip_redirection_rule(redirection, *methods, &block)
|
35
|
+
redirector_builder.add_skip_config(redirection, *methods, block)
|
36
|
+
end
|
13
37
|
|
14
|
-
|
15
|
-
|
38
|
+
# Retruns the RequestHandlerBuilder
|
39
|
+
#
|
40
|
+
# RequestHandlerBuilder will Carry all the configurations and will create
|
41
|
+
# one {RequestHandler} for each request
|
42
|
+
#
|
43
|
+
# @return [RequestHandlerBuilder]
|
44
|
+
def redirector_builder
|
45
|
+
@redirector_builder ||= Tarquinn::RequestHandlerBuilder.new
|
46
|
+
end
|
16
47
|
end
|
17
48
|
end
|
@@ -1,11 +1,28 @@
|
|
1
|
-
|
2
|
-
attr_accessor :routes
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module Tarquinn
|
4
|
+
class Condition
|
5
|
+
# @api private
|
6
|
+
#
|
7
|
+
# Checks condition based on route action
|
8
|
+
class ActionChecker < Tarquinn::Condition
|
9
|
+
def initialize(routes)
|
10
|
+
@routes = [routes].flatten.map(&:to_s)
|
11
|
+
end
|
12
|
+
|
13
|
+
def check?(controller)
|
14
|
+
routes.include? controller.params[:action]
|
15
|
+
end
|
16
|
+
|
17
|
+
def ==(other)
|
18
|
+
return false unless other.class == self.class
|
19
|
+
|
20
|
+
other.routes == routes
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
7
24
|
|
8
|
-
|
9
|
-
|
25
|
+
attr_reader :routes
|
26
|
+
end
|
10
27
|
end
|
11
28
|
end
|
@@ -1,13 +1,30 @@
|
|
1
|
-
|
2
|
-
attr_accessor :methods
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module Tarquinn
|
4
|
+
class Condition
|
5
|
+
# @api private
|
6
|
+
#
|
7
|
+
# Checks condition based on result of method call from controller
|
8
|
+
class MethodCaller < Tarquinn::Condition
|
9
|
+
def initialize(methods)
|
10
|
+
@methods = [methods].flatten
|
11
|
+
end
|
12
|
+
|
13
|
+
def check?(controller)
|
14
|
+
methods.any? do |method|
|
15
|
+
controller.call(method)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def ==(other)
|
20
|
+
return false unless other.class == self.class
|
21
|
+
|
22
|
+
other.methods == methods
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
7
26
|
|
8
|
-
|
9
|
-
methods.any? do |method|
|
10
|
-
controller.call(method)
|
27
|
+
attr_reader :methods
|
11
28
|
end
|
12
29
|
end
|
13
30
|
end
|
@@ -1,11 +1,22 @@
|
|
1
|
-
|
2
|
-
attr_reader :block
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module Tarquinn
|
4
|
+
class Condition
|
5
|
+
# @api private
|
6
|
+
#
|
7
|
+
# Checks condition based on a given block
|
8
|
+
class ProcRunner < Tarquinn::Condition
|
9
|
+
def initialize(&block)
|
10
|
+
@block = block
|
11
|
+
end
|
12
|
+
|
13
|
+
def check?(controller)
|
14
|
+
controller.run(&block)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
7
18
|
|
8
|
-
|
9
|
-
|
19
|
+
attr_reader :block
|
20
|
+
end
|
10
21
|
end
|
11
22
|
end
|
data/lib/tarquinn/condition.rb
CHANGED
@@ -1,5 +1,58 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Tarquinn
|
4
|
+
# @api private
|
5
|
+
# @abstract
|
6
|
+
#
|
7
|
+
# Redirection condition
|
8
|
+
class Condition
|
9
|
+
autoload :ActionChecker, 'tarquinn/condition/action_checker'
|
10
|
+
autoload :MethodCaller, 'tarquinn/condition/method_caller'
|
11
|
+
autoload :ProcRunner, 'tarquinn/condition/proc_runner'
|
12
|
+
|
13
|
+
class << self
|
14
|
+
# Creates a method caller condition
|
15
|
+
#
|
16
|
+
# @param methods [Array<Symbol>] list of methods to be called for condition
|
17
|
+
#
|
18
|
+
# @return [Tarquinn::Condition::MethodCaller] when methods are given
|
19
|
+
# @return [NilClass] when no methods are given
|
20
|
+
def method_caller(methods)
|
21
|
+
return if methods.empty?
|
22
|
+
|
23
|
+
Tarquinn::Condition::MethodCaller.new(methods)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Creates an action checker condition
|
27
|
+
#
|
28
|
+
# @param routes [Array<Symbol>] controller actions that will match the condition
|
29
|
+
#
|
30
|
+
# @return [Tarquinn::Condition::ActionChecker]
|
31
|
+
def action_checker(routes)
|
32
|
+
Tarquinn::Condition::ActionChecker.new(routes)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Cretes a proc checker
|
36
|
+
#
|
37
|
+
# @param block [Proc] block to be ran when condition is checked
|
38
|
+
#
|
39
|
+
# @return [Tarquinn::Condition::ProcRunner] When a block is given
|
40
|
+
# @return [NilClass] when no block is given
|
41
|
+
def proc_runner(&block)
|
42
|
+
return unless block
|
43
|
+
|
44
|
+
Tarquinn::Condition::ProcRunner.new(&block)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Checks if a condition is matched
|
49
|
+
#
|
50
|
+
# @param _controller [Tarquinn::Controller] the controller with the request params
|
51
|
+
#
|
52
|
+
# @return [TrueClass] When it is a match
|
53
|
+
# @return [FalseClass] When it is not a match
|
54
|
+
def check?(_controller)
|
55
|
+
raise NotImplementedError, 'Needs to be implemented in child class'
|
56
|
+
end
|
57
|
+
end
|
5
58
|
end
|
data/lib/tarquinn/controller.rb
CHANGED
@@ -1,19 +1,62 @@
|
|
1
|
-
|
2
|
-
attr_reader :controller
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module Tarquinn
|
4
|
+
# @api private
|
5
|
+
#
|
6
|
+
# Controller interface
|
7
|
+
class Controller
|
8
|
+
def initialize(controller)
|
9
|
+
@controller = controller
|
10
|
+
end
|
7
11
|
|
8
|
-
|
9
|
-
|
10
|
-
|
12
|
+
# Returns request parameters
|
13
|
+
#
|
14
|
+
# @return [ActionController::Parameters]
|
15
|
+
def params
|
16
|
+
@params ||= controller.send(:params)
|
17
|
+
end
|
11
18
|
|
12
|
-
|
13
|
-
|
14
|
-
|
19
|
+
# Calls a method from the controller
|
20
|
+
#
|
21
|
+
# @param method [Symbol] method name to be called
|
22
|
+
# @param args [Array<Symbol>] Method arguments
|
23
|
+
# @param opts [Hash<Symbol,Object>] Method arguments
|
24
|
+
# @param block [Proc] block to be given to the method
|
25
|
+
#
|
26
|
+
# @return [Object]
|
27
|
+
def call(method, *args, **opts, &block)
|
28
|
+
controller.send(method, *args, **opts, &block)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Run a block in teh context of the controller
|
32
|
+
#
|
33
|
+
# @param block [Proc] block to be ran
|
34
|
+
#
|
35
|
+
# @return [Object] the return of the block
|
36
|
+
def run(&block)
|
37
|
+
controller.send(:instance_eval, &block)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Checks if the controller responds to a method
|
41
|
+
#
|
42
|
+
# @param method [Symbol] method name
|
43
|
+
#
|
44
|
+
# @return [TrueClass] the controller responds to this method
|
45
|
+
# @return [FalseClass] the controller does not responds to this method
|
46
|
+
def method?(method)
|
47
|
+
controller.respond_to?(method, true)
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
attr_reader :controller
|
15
53
|
|
16
|
-
|
17
|
-
|
54
|
+
# @method controller
|
55
|
+
# @api private
|
56
|
+
# @private
|
57
|
+
#
|
58
|
+
# Returns the controller handling the request
|
59
|
+
#
|
60
|
+
# @return [ActionController::Base]
|
18
61
|
end
|
19
62
|
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Tarquinn
|
4
|
+
# @api private
|
5
|
+
#
|
6
|
+
# Redirection configuration
|
7
|
+
#
|
8
|
+
# @see Tarquinn::RequestHandler
|
9
|
+
class RedirectionConfig
|
10
|
+
attr_reader :redirect
|
11
|
+
|
12
|
+
# @param redirect [Symbol] redirection name and redirection method
|
13
|
+
def initialize(redirect)
|
14
|
+
@redirect = redirect
|
15
|
+
end
|
16
|
+
|
17
|
+
# Adds conditions to the rule
|
18
|
+
#
|
19
|
+
# The rule name defines which method will be called when checking the path of redirection
|
20
|
+
#
|
21
|
+
# @param methods [Array<Symbol>] Methods that tell that a redirection should be applied
|
22
|
+
# @param block [Proc] block that tells if a the redirection should be applied
|
23
|
+
#
|
24
|
+
# @return [Array<Tarquinn::Condition>] Current registered conditions
|
25
|
+
def add_redirection_rules(*methods, &block)
|
26
|
+
redirect_on method_caller(methods)
|
27
|
+
redirect_on proc_runner(&block)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Add rule for skipping on some actions / routes
|
31
|
+
#
|
32
|
+
# @param actions [Array<Symbol>] actions / routes to be skipped by redirection rule
|
33
|
+
#
|
34
|
+
# @return [Array<Tarquinn::Condition>]
|
35
|
+
def add_skip_action(*actions)
|
36
|
+
skip action_checker(actions)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Attaches conditions to skip a redirection
|
40
|
+
#
|
41
|
+
# Methods and blocks are ran and if any returns true, the redirec is skipped
|
42
|
+
#
|
43
|
+
# @param methods [Array<Symbol>] Methods that tell that a redirection should be skipped
|
44
|
+
# @param block [Proc] block that tells if a the redirection should be skipped
|
45
|
+
#
|
46
|
+
# @return [Array] Current registered conditions
|
47
|
+
def add_skip_rules(*methods, &block)
|
48
|
+
skip method_caller(methods)
|
49
|
+
skip proc_runner(&block)
|
50
|
+
end
|
51
|
+
|
52
|
+
# All blocks that indicate a redirection
|
53
|
+
#
|
54
|
+
# @return [Array<Tarquinn::Condition>]
|
55
|
+
def redirection_blocks
|
56
|
+
@redirection_blocks ||= []
|
57
|
+
end
|
58
|
+
|
59
|
+
# All blocks that indicate a redirection should be skipped
|
60
|
+
#
|
61
|
+
# @return [Array<Tarquinn::Condition>]
|
62
|
+
def skip_blocks
|
63
|
+
@skip_blocks ||= []
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
delegate :method_caller, :action_checker, :proc_runner, to: Tarquinn::Condition
|
69
|
+
|
70
|
+
# @private
|
71
|
+
#
|
72
|
+
# Adds a condition to skip a redirection
|
73
|
+
#
|
74
|
+
# @return (see #skip_blocks)
|
75
|
+
def skip(condition)
|
76
|
+
return skip_blocks unless condition
|
77
|
+
|
78
|
+
skip_blocks << condition
|
79
|
+
end
|
80
|
+
|
81
|
+
# @private
|
82
|
+
#
|
83
|
+
# Adds a condition to a redirection
|
84
|
+
#
|
85
|
+
# @return (see #redirection_blocks)
|
86
|
+
def redirect_on(condition)
|
87
|
+
return redirection_blocks unless condition
|
88
|
+
|
89
|
+
redirection_blocks << condition
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|