Class: Redirectr::ReferrerToken

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Defined in:
lib/redirectr/referrer_token.rb,
lib/redirectr/referrer_token/global_var_storage.rb,
lib/redirectr/referrer_token/active_record_storage.rb

Defined Under Namespace

Classes: ActiveRecordStorage, GlobalVarStorage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, token = nil) ⇒ ReferrerToken

Returns a new instance of ReferrerToken.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/redirectr/referrer_token.rb', line 10

def initialize(url, token=nil)
  @url   = url
  @token = token

  if Redirectr.config.use_referrer_token
    if Redirectr.config.storage_implementation.nil?
      raise "Missing storage implementation for referrer tokens! please define config.x.redirectr.storage_implementation"
    end

    if Redirectr.config.reuse_tokens
      @token ||= Redirectr.config.storage_implementation.token_for_url(url)
    end
    @token ||= SecureRandom.hex(16)
  elsif Redirectr.config.encrypt_referrer
    @token ||= self.class.cryptr.encrypt_and_sign url
  else
    @token ||= url
  end
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



8
9
10
# File 'lib/redirectr/referrer_token.rb', line 8

def token
  @token
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/redirectr/referrer_token.rb', line 8

def url
  @url
end

Class Method Details

.cryptrObject



68
69
70
# File 'lib/redirectr/referrer_token.rb', line 68

def self.cryptr
  @cryptr ||= ActiveSupport::MessageEncryptor.new(Rails.application.secrets.secret_key_base)
end

.from_param(param) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/redirectr/referrer_token.rb', line 52

def self.from_param(param)
  if Redirectr.config.encrypt_referrer
    ReferrerToken.new self.class.cryptr.decrypt_and_verify param
  elsif Redirectr.config.use_referrer_token
    self.lookup(param)
  else
    ReferrerToken.new param
  end
end

.lookup(token) ⇒ Object



62
63
64
# File 'lib/redirectr/referrer_token.rb', line 62

def self.lookup(token)
  Redirectr.config.storage_implementation.fetch token
end

Instance Method Details

#persisted?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/redirectr/referrer_token.rb', line 42

def persisted?
  true
end

#saveObject



46
47
48
49
50
# File 'lib/redirectr/referrer_token.rb', line 46

def save
  if Redirectr.config.use_referrer_token
    Redirectr.config.storage_implementation.store self
  end
end

#to_modelObject



34
35
36
# File 'lib/redirectr/referrer_token.rb', line 34

def to_model
  self
end

#to_paramObject



30
31
32
# File 'lib/redirectr/referrer_token.rb', line 30

def to_param
  @token
end

#to_sObject



38
39
40
# File 'lib/redirectr/referrer_token.rb', line 38

def to_s
  @url
end