Class: MagicLinks::Template
- Inherits:
-
Object
- Object
- MagicLinks::Template
- Defined in:
- lib/magic_links/template.rb
Constant Summary collapse
- VALID_TEMPLATE_PATTERN =
%r{^/([A-Za-z0-9_\-]+)/(:token)$}.freeze
Instance Attribute Summary collapse
-
#action_scope ⇒ Object
readonly
Returns the value of attribute action_scope.
-
#expiry ⇒ Object
readonly
Returns the value of attribute expiry.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
-
#strength ⇒ Object
readonly
Returns the value of attribute strength.
Instance Method Summary collapse
-
#initialize(pattern:, action_scope:, strength:, expiry:) ⇒ Template
constructor
A new instance of Template.
- #magic_link_for(user, path, expiry = nil) ⇒ Object
- #magic_url_for(user, path, expiry = nil) ⇒ Object
- #match?(path) ⇒ Boolean
- #token_for(path) ⇒ Object
Constructor Details
#initialize(pattern:, action_scope:, strength:, expiry:) ⇒ Template
Returns a new instance of Template.
7 8 9 10 11 12 13 14 |
# File 'lib/magic_links/template.rb', line 7 def initialize(pattern:, action_scope:, strength:, expiry:) raise ArgumentError, "Pattern must be of the form '/xyz/:token'" unless VALID_TEMPLATE_PATTERN.match?(pattern) @pattern = pattern @action_scope = action_scope @strength = strength @expiry = expiry end |
Instance Attribute Details
#action_scope ⇒ Object (readonly)
Returns the value of attribute action_scope.
5 6 7 |
# File 'lib/magic_links/template.rb', line 5 def action_scope @action_scope end |
#expiry ⇒ Object (readonly)
Returns the value of attribute expiry.
5 6 7 |
# File 'lib/magic_links/template.rb', line 5 def expiry @expiry end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/magic_links/template.rb', line 5 def name @name end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
5 6 7 |
# File 'lib/magic_links/template.rb', line 5 def pattern @pattern end |
#strength ⇒ Object (readonly)
Returns the value of attribute strength.
5 6 7 |
# File 'lib/magic_links/template.rb', line 5 def strength @strength end |
Instance Method Details
#magic_link_for(user, path, expiry = nil) ⇒ Object
24 25 26 27 28 |
# File 'lib/magic_links/template.rb', line 24 def magic_link_for(user, path, expiry = nil) expiry ||= self.expiry magic_token = magic_token_for(user, path, expiry) pattern.sub(':token', magic_token.token) end |
#magic_url_for(user, path, expiry = nil) ⇒ Object
30 31 32 |
# File 'lib/magic_links/template.rb', line 30 def magic_url_for(user, path, expiry = nil) url_for(magic_link_for(user, path, expiry)) end |
#match?(path) ⇒ Boolean
16 17 18 |
# File 'lib/magic_links/template.rb', line 16 def match?(path) matcher.match?(path) end |
#token_for(path) ⇒ Object
20 21 22 |
# File 'lib/magic_links/template.rb', line 20 def token_for(path) matcher.match(path)&.captures&.first end |