tarquinn 0.4.0 → 0.5.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
  SHA256:
3
- metadata.gz: 472e75e26a2d619f0284c9eb797c5e47f717a9f9a6e52757fc1c5ad7272a174f
4
- data.tar.gz: efb31ef0021491a3b99c48cb8c3a34026493a10500636449195c1070d281dc91
3
+ metadata.gz: bedf2c09afd835b6b2b70a2f83a6f3e81c15822105b27a50f482bb81fbdd9d89
4
+ data.tar.gz: 7592837ef041e0b83e9ffa5cec438859cf7be9cc6c9d16819a50d6242d1ae27b
5
5
  SHA512:
6
- metadata.gz: 9391c038495ea739fee48b43a9c104bcb0dfb887aa87ecb75be3d1378ee2c11649cc68725fd8beea215036065b5deb1aeca08babea956d77b42628b887ead261
7
- data.tar.gz: 59ccde1cade0e2b76b82f89073409cf9ced274d7346214ae08f2d4e6f06e11269feb9b768c219064bcec0e748205c1ce6a5eb45515e21dbe404a53459cb6643b
6
+ metadata.gz: 998e736f046dee05cd716719d7bd6dd212840c3c6d4caaddf4178e3027ce9962a3ce9fc5ac4c9a480f0df8ebc7ac4c4dd689c58151ddf268f01ad1468b33fb19
7
+ data.tar.gz: 5175105a808ef43c98a54fc9b0a4e8831bef7bbe561b99db04f1a746bda0480cd871720173b61cf59505a47bc390b9567e655e2a2dccf7cac1d0e56dca1a2544
data/.circleci/config.yml CHANGED
@@ -33,6 +33,9 @@ jobs:
33
33
  - run:
34
34
  name: RSpec
35
35
  command: bundle exec rspec
36
+ - run:
37
+ name: Upload coverage to Codacy
38
+ command: bash <(curl -Ls https://coverage.codacy.com/get.sh) report -r coverage/lcov/project.lcov
36
39
  checks:
37
40
  docker:
38
41
  - image: darthjee/circleci_ruby_331:1.0.1
data/Gemfile CHANGED
@@ -2,17 +2,18 @@
2
2
 
3
3
  source 'https://rubygems.org'
4
4
 
5
- gem 'bundler', '~> 2.5.13'
6
- gem 'pry', '~> 0.14.2'
7
- gem 'pry-nav', '~> 1.0.0'
8
- gem 'rails', '7.2.3'
9
- gem 'rake', '~> 13.1.0'
10
- gem 'rspec', '~> 3.12.0'
11
- gem 'rspec-rails', '6.1.1'
12
- gem 'rubycritic', '4.9.1'
13
- gem 'simplecov', '~> 0.22.0'
14
- gem 'sqlite3', '1.4.2'
15
- gem 'yard', '0.9.38'
16
- gem 'yardstick', '0.9.9'
5
+ gem 'bundler', '~> 2.5.13'
6
+ gem 'pry', '~> 0.14.2'
7
+ gem 'pry-nav', '~> 1.0.0'
8
+ gem 'rails', '7.2.3'
9
+ gem 'rake', '~> 13.1.0'
10
+ gem 'rspec', '~> 3.12.0'
11
+ gem 'rspec-rails', '6.1.1'
12
+ gem 'rubycritic', '4.9.1'
13
+ gem 'simplecov', '~> 0.22.0'
14
+ gem 'simplecov-lcov', '0.9.0'
15
+ gem 'sqlite3', '1.4.2'
16
+ gem 'yard', '0.9.38'
17
+ gem 'yardstick', '0.9.9'
17
18
 
18
19
  gemspec
data/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  Tarquinn
2
2
  ========
3
3
  [![Build Status](https://circleci.com/gh/darthjee/tarquinn.svg?style=shield)](https://circleci.com/gh/darthjee/tarquinn)
4
- [![Code Climate](https://codeclimate.com/github/darthjee/tarquinn/badges/gpa.svg)](https://codeclimate.com/github/darthjee/tarquinn)
4
+ [![Codacy Badge](https://app.codacy.com/project/badge/Grade/4e950bac9e434e8db64448da42e78793)](https://app.codacy.com/gh/darthjee/tarquinn/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
5
+ [![Codacy Badge](https://app.codacy.com/project/badge/Coverage/4e950bac9e434e8db64448da42e78793)](https://app.codacy.com/gh/darthjee/tarquinn/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)
5
6
  [![Gem Version](https://badge.fury.io/rb/tarquinn.svg)](https://badge.fury.io/rb/tarquinn)
6
7
  [![Inline docs](http://inch-ci.org/github/darthjee/tarquinn.svg)](http://inch-ci.org/github/darthjee/tarquinn)
7
8
 
@@ -9,13 +10,13 @@ Tarquinn
9
10
 
10
11
  Yard Documentation
11
12
  -------------------
12
- [https://www.rubydoc.info/gems/tarquinn/0.4.0](https://www.rubydoc.info/gems/tarquinn/0.4.0)
13
+ [https://www.rubydoc.info/gems/tarquinn/0.5.0](https://www.rubydoc.info/gems/tarquinn/0.5.0)
13
14
 
14
15
  This gem makes easier to controll generic redirection
15
16
 
16
- Current Release: [0.4.0](https://github.com/darthjee/tarquinn/tree/0.4.0)
17
+ Current Release: [0.5.0](https://github.com/darthjee/tarquinn/tree/0.5.0)
17
18
 
18
- Next Version [0.4.1](https://github.com/darthjee/tarquinn/compare/0.4.0...main)
19
+ Next Version [0.5.1](https://github.com/darthjee/tarquinn/compare/0.45.0...main)
19
20
 
20
21
  Getting started
21
22
  ---------------
@@ -143,7 +144,9 @@ bundle exec rake verify_measurements
143
144
 
144
145
  You can specify a `domain` option in your `redirection_rule` to allow cross-domain redirection. When set, the redirection will be allowed to external hosts and the specified domain will be used for validation.
145
146
 
146
- **Example:**
147
+ The `domain:` option accepts either a **String** (static domain) or a **Symbol** (dynamic domain resolved at runtime by calling the named method on the controller).
148
+
149
+ **Static domain (String):**
147
150
 
148
151
  ```ruby
149
152
  class ExternalRedirectController < ApplicationController
@@ -161,5 +164,30 @@ class ExternalRedirectController < ApplicationController
161
164
  end
162
165
  ```
163
166
 
167
+ **Dynamic domain (Symbol):**
168
+
169
+ ```ruby
170
+ class ExternalRedirectController < ApplicationController
171
+ include Tarquinn
172
+
173
+ redirection_rule :redirect_external, domain: :current_domain do
174
+ params[:should_redirect]
175
+ end
176
+
177
+ private
178
+
179
+ def redirect_external
180
+ '/external_path'
181
+ end
182
+
183
+ def current_domain
184
+ # resolved at request time
185
+ request.subdomain == 'admin' ? 'admin.example.com' : 'example.com'
186
+ end
187
+ end
188
+ ```
189
+
164
190
  - If `domain` is not set, redirection is only allowed for the same host.
165
- - If `domain` is set, redirection to the specified domain is allowed.
191
+ - If `domain` is a String, it is used directly as the domain prefix.
192
+ - If `domain` is a Symbol and the controller responds to that method, the method is called and its return value is used as the domain.
193
+ - If `domain` is a Symbol but the controller does not respond to it, the Symbol's string representation is used as the domain.
@@ -52,6 +52,23 @@ module Tarquinn
52
52
  controller.respond_to?(method, true)
53
53
  end
54
54
 
55
+ # Resolves a value that may reference a controller method
56
+ #
57
+ # If the value is a Symbol and the controller responds to it, calls the
58
+ # method and returns its result. Otherwise, returns the original value.
59
+ #
60
+ # @overload resolve_callable(method_name)
61
+ # @param method_name [Symbol] method name
62
+ # @overload resolve_callable(value)
63
+ # @param value [String] value to be returned
64
+ #
65
+ # @return [Object] resolved method return or original value
66
+ def resolve_callable(value)
67
+ return value unless value.is_a?(Symbol) && method?(value)
68
+
69
+ call(value)
70
+ end
71
+
55
72
  private
56
73
 
57
74
  # @api private
@@ -19,40 +19,19 @@ module Tarquinn
19
19
  # and not allowed for external hosts.
20
20
  #
21
21
  # if set, the redirection will be allowed for external
22
- # hosts and the domain will be used for validation
22
+ # hosts and the domain will be used for validation.
23
+ # A Symbol value is resolved at runtime via the controller method.
23
24
  #
24
- # @return [String, nil] the domain for cross-domain redirection
25
+ # @return [String, Symbol, nil] the domain for cross-domain redirection
25
26
  # @see RedirectionHandler#redirect
26
- # @see #redirection_options
27
27
 
28
28
  # @method initialize(domain: nil)
29
29
  # @api private
30
30
  # Initializes a new Options object
31
- # @param domain [String, nil] the domain for cross-domain redirection
31
+ # @param domain [String, Symbol, nil] the domain for cross-domain redirection.
32
+ # A String is used as-is; a Symbol is resolved by calling the matching
33
+ # method on the controller at runtime (via {Tarquinn::RedirectionHandler}).
32
34
  # @return [Tarquinn::RedirectionConfig::Options]
33
-
34
- # Options to be passed for the controller on redirect_to
35
- # @return [Hash] the options for the redirection
36
- def redirection_options
37
- {
38
- allow_other_host:
39
- }.compact
40
- end
41
-
42
- # Checks if the domain option is set
43
- # @return [TrueClass] when the domain option is set
44
- # @return [FalseClass] when the domain option is not set
45
- def domain?
46
- domain.present?
47
- end
48
-
49
- private
50
-
51
- # Returns the value of the allow_other_host option
52
- # @return [TrueClass, nil] the value of the allow_other_host option
53
- def allow_other_host
54
- domain? || nil
55
- end
56
35
  end
57
36
  end
58
37
  end
@@ -76,16 +76,7 @@ module Tarquinn
76
76
  @skip_blocks ||= []
77
77
  end
78
78
 
79
- delegate :redirection_options, :domain, :domain?, to: :options
80
-
81
- # @method redirection_options
82
- # @api private
83
- # @private
84
- #
85
- # Options to be passed for the controller on redirect_to
86
- #
87
- # @return [Hash] the options for the redirection
88
- # @see Tarquinn::RedirectionConfig::Options#redirection_options
79
+ delegate :domain, to: :options
89
80
 
90
81
  # @method domain
91
82
  # @api private
@@ -96,18 +87,11 @@ module Tarquinn
96
87
  # if not set, the redirection be for the same host
97
88
  # and not allowed for external hosts.
98
89
  # if set, the redirection will be allowed for external
99
- # hosts and the domain will be used for validation
100
- # @return [String, nil] the domain for cross-domain redirection
90
+ # hosts and the domain will be used for validation.
91
+ # A Symbol value is resolved at runtime via the controller method.
92
+ # @return [String, Symbol, nil] the domain for cross-domain redirection
101
93
  # @see Tarquinn::RedirectionConfig::Options#domain
102
94
 
103
- # @method domain?
104
- # @api private
105
- # @private
106
- # Checks if the domain option is set
107
- # @return [TrueClass] when the domain option is set
108
- # @return [FalseClass] when the domain option is not set
109
- # @see Tarquinn::RedirectionConfig::Options#domain?
110
-
111
95
  private
112
96
 
113
97
  # @api private
@@ -33,14 +33,12 @@ module Tarquinn
33
33
  controller.call(:redirect_to, redirect_full_path, **redirection_options)
34
34
  end
35
35
 
36
- delegate :redirection_options, to: :config
37
-
38
- # @method redirection_options
39
- # @api private
40
- # @private
41
- # Options to be passed for the controller on {Tarquinn::Controller#call}(:redirect_to)
42
- # @return (see Tarquinn::RedirectionConfig::Options#redirection_options)
43
- # @see Tarquinn::RedirectionConfig::Options#redirection_options
36
+ # Options to be passed for the controller on redirect_to
37
+ #
38
+ # @return [Hash] the options for the redirection
39
+ def redirection_options
40
+ { allow_other_host: }.compact
41
+ end
44
42
 
45
43
  private
46
44
 
@@ -68,42 +66,64 @@ module Tarquinn
68
66
  # @return [Boolean]
69
67
  attr_reader :perform_redirect
70
68
 
71
- delegate :redirection_blocks, :domain, :domain?, :skip_blocks, to: :config
69
+ delegate :redirection_blocks, :skip_blocks, to: :config
70
+ delegate :domain, to: :config, prefix: true
71
+
72
+ # @method config_domain
73
+ # @api private
74
+ # @private
75
+ # The configured domain for cross-domain redirection
76
+ # @return [String, Symbol, nil] the configured domain for cross-domain redirection
72
77
 
73
78
  # @method redirection_blocks
74
79
  # @api private
75
80
  # @private
76
- # Options to be passed for the controller on redirect_to
81
+ # All blocks that indicate a redirection
77
82
  # @return [Array<Tarquinn::Condition>] all blocks that indicate a redirection
78
- # @see Tarquinn::RedirectionConfig::Options#redirection_options
79
83
 
80
- # @method domain
84
+ # @method skip_blocks
85
+ # @api private
86
+ # @private
87
+ # All blocks that indicate a redirection should be skipped
88
+ # @return (see Tarquinn::RedirectionConfig#skip_blocks)
89
+ # @see Tarquinn::RedirectionConfig#skip_blocks
90
+
81
91
  # @api private
82
92
  # @private
83
93
  #
84
- # The domain when a redirection is cross-domain
94
+ # Resolves the domain for cross-domain redirection
85
95
  #
86
- # if not set, the redirection be for the same host
87
- # and not allowed for external hosts.
88
- # if set, the redirection will be allowed for external
89
- # hosts and the domain will be used for validation
90
- # @return [String, nil] the domain for cross-domain redirection
91
- # @see Tarquinn::RedirectionConfig::Options#domain
96
+ # When the configured domain is a Symbol and the controller responds to
97
+ # that method, the method is called and its return value is used.
98
+ # Otherwise, the configured value is returned as-is (a String is used
99
+ # directly; a Symbol that the controller does not respond to is converted
100
+ # to a String via `#to_s`).
101
+ #
102
+ # @return [String, nil] the resolved domain
103
+ def domain
104
+ controller.resolve_callable(config_domain).to_s.presence
105
+ end
92
106
 
93
- # @method domain?
94
107
  # @api private
95
108
  # @private
96
- # Checks if the domain option is set
97
- # @return [TrueClass] when the domain option is set
98
- # @return [FalseClass] when the domain option is not set
99
- # @see Tarquinn::RedirectionConfig::Options#domain?
109
+ #
110
+ # Checks if the resolved domain is set
111
+ #
112
+ # @return [TrueClass] when the domain is set
113
+ # @return [FalseClass] when the domain is not set
114
+ def domain?
115
+ domain.present?
116
+ end
100
117
 
101
- # @method skip_blocks
102
118
  # @api private
103
119
  # @private
104
- # All blocks that indicate a redirection should be skipped
105
- # @return (see Tarquinn::RedirectionConfig#skip_blocks)
106
- # @see Tarquinn::RedirectionConfig#skip_blocks
120
+ #
121
+ # Returns the value of the allow_other_host option
122
+ #
123
+ # @return [TrueClass, nil] the value of the allow_other_host option
124
+ def allow_other_host
125
+ domain? || nil
126
+ end
107
127
 
108
128
  # @api private
109
129
  # @private
@@ -134,9 +154,7 @@ module Tarquinn
134
154
  #
135
155
  # @return [String]
136
156
  def redirect_path
137
- return redirect_method unless controller.method?(redirect_method)
138
-
139
- controller.call redirect_method
157
+ controller.resolve_callable(redirect_method)
140
158
  end
141
159
 
142
160
  # @api private
@@ -4,5 +4,5 @@ module Tarquinn
4
4
  # @api public
5
5
  #
6
6
  # Current version of the Tarquinn gem
7
- VERSION = '0.4.0'
7
+ VERSION = '0.5.0'
8
8
  end
@@ -39,5 +39,9 @@ module Tarquinn
39
39
  def should_skip_redirect?
40
40
  false
41
41
  end
42
+
43
+ def dynamic_domain
44
+ 'dynamic.example.com'
45
+ end
42
46
  end
43
47
  end
@@ -109,4 +109,24 @@ describe Tarquinn::Controller do
109
109
  end
110
110
  end
111
111
  end
112
+
113
+ describe '#resolve_callable' do
114
+ context 'when value is a symbol and method exists' do
115
+ it 'returns method call result' do
116
+ expect(subject.resolve_callable(:redirection_path)).to eq('/path')
117
+ end
118
+ end
119
+
120
+ context 'when value is a symbol and method does not exist' do
121
+ it 'returns the original symbol' do
122
+ expect(subject.resolve_callable(:non_existing)).to eq(:non_existing)
123
+ end
124
+ end
125
+
126
+ context 'when value is not a symbol' do
127
+ it 'returns the original value' do
128
+ expect(subject.resolve_callable('redirection_path')).to eq('redirection_path')
129
+ end
130
+ end
131
+ end
112
132
  end
@@ -3,42 +3,28 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe Tarquinn::RedirectionConfig::Options do
6
- describe '#redirection_options' do
7
- subject(:redirection_options) { options.redirection_options }
8
-
6
+ describe '#domain' do
9
7
  context 'when domain is not set' do
10
8
  subject(:options) { described_class.new }
11
9
 
12
- it 'returns an empty hash' do
13
- expect(redirection_options).to eq({})
10
+ it 'returns nil' do
11
+ expect(options.domain).to be_nil
14
12
  end
15
13
  end
16
14
 
17
- context 'when domain is set' do
15
+ context 'when domain is set to a string' do
18
16
  subject(:options) { described_class.new(domain: 'example.com') }
19
17
 
20
- it 'returns allow_other_host: true' do
21
- expect(redirection_options).to eq({ allow_other_host: true })
22
- end
23
- end
24
- end
25
-
26
- describe '#domain?' do
27
- subject(:options) { described_class.new(domain: domain_value) }
28
-
29
- context 'when domain is not set' do
30
- let(:domain_value) { nil }
31
-
32
- it 'returns false' do
33
- expect(options.domain?).to be false
18
+ it 'returns the configured string' do
19
+ expect(options.domain).to eq('example.com')
34
20
  end
35
21
  end
36
22
 
37
- context 'when domain is set' do
38
- let(:domain_value) { 'example.com' }
23
+ context 'when domain is set to a symbol' do
24
+ subject(:options) { described_class.new(domain: :some_domain_method) }
39
25
 
40
- it 'returns true' do
41
- expect(options.domain?).to be true
26
+ it 'returns the configured symbol' do
27
+ expect(options.domain).to eq(:some_domain_method)
42
28
  end
43
29
  end
44
30
  end
@@ -138,8 +138,43 @@ describe Tarquinn::RedirectionHandler do
138
138
  )
139
139
  end
140
140
 
141
- it 'calls for redirection using full path' do
141
+ it 'calls for redirection using full path with allow_other_host' do
142
142
  subject.redirect
143
+
144
+ expect(controller).to have_received(:redirect_to)
145
+ .with('example.com/path', allow_other_host: true)
146
+ end
147
+ end
148
+
149
+ context 'when configured with a symbol domain that the controller responds to' do
150
+ let(:config) do
151
+ Tarquinn::RedirectionConfig.new(
152
+ redirection: :redirection_path,
153
+ domain: :dynamic_domain
154
+ )
155
+ end
156
+
157
+ it 'resolves the domain by calling the controller method' do
158
+ subject.redirect
159
+
160
+ expect(controller).to have_received(:redirect_to)
161
+ .with('dynamic.example.com/path', allow_other_host: true)
162
+ end
163
+ end
164
+
165
+ context 'when configured with a symbol domain that the controller does not respond to' do
166
+ let(:config) do
167
+ Tarquinn::RedirectionConfig.new(
168
+ redirection: :redirection_path,
169
+ domain: :unknown_domain_method
170
+ )
171
+ end
172
+
173
+ it 'treats the symbol as a static domain string' do
174
+ subject.redirect
175
+
176
+ expect(controller).to have_received(:redirect_to)
177
+ .with('unknown_domain_method/path', allow_other_host: true)
143
178
  end
144
179
  end
145
180
  end
@@ -125,6 +125,36 @@ describe Tarquinn, type: :controller do
125
125
  end
126
126
  end
127
127
  end
128
+
129
+ context 'when passing domain option as a symbol' do
130
+ controller(Tarquinn::DummyRouteController) do
131
+ redirection_rule :redirection, domain: :dynamic_domain do
132
+ params[:should_redirect]
133
+ end
134
+
135
+ def dynamic_domain
136
+ 'dynamic.example.com'
137
+ end
138
+ end
139
+
140
+ before { get :index, params: parameters }
141
+
142
+ context 'when request indicates a redirection' do
143
+ let(:parameters) { { should_redirect: true } }
144
+
145
+ it 'performs a redirect using the value returned by the domain method' do
146
+ expect(response).to redirect_to('dynamic.example.com/path')
147
+ end
148
+ end
149
+
150
+ context 'when request does not indicate a redirection' do
151
+ let(:parameters) { {} }
152
+
153
+ it 'does not perform a redirect' do
154
+ expect(response).not_to redirect_to('/path')
155
+ end
156
+ end
157
+ end
128
158
  end
129
159
 
130
160
  describe '.skip_redirection' do
data/spec/spec_helper.rb CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  require 'simplecov'
4
4
 
5
+ if ENV['CI']
6
+ require 'simplecov-lcov'
7
+ SimpleCov::Formatter::LcovFormatter.config.report_with_single_file = true
8
+ SimpleCov.formatter = SimpleCov::Formatter::LcovFormatter
9
+ end
10
+
5
11
  SimpleCov.start do
6
12
  add_filter '/spec/'
7
13
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tarquinn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DarthJee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-02-27 00:00:00.000000000 Z
11
+ date: 2026-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport