Class: MagicLinks::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/magic_links/template.rb

Constant Summary collapse

VALID_TEMPLATE_PATTERN =
%r{^/([A-Za-z0-9_\-]+)/(:token)$}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern:, action_scope:, strength:, expiry:) ⇒ Template

Returns a new instance of Template.

Raises:

  • (ArgumentError)


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_scopeObject (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

#expiryObject (readonly)

Returns the value of attribute expiry.



5
6
7
# File 'lib/magic_links/template.rb', line 5

def expiry
  @expiry
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/magic_links/template.rb', line 5

def name
  @name
end

#patternObject (readonly)

Returns the value of attribute pattern.



5
6
7
# File 'lib/magic_links/template.rb', line 5

def pattern
  @pattern
end

#strengthObject (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



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

Returns:

  • (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