Class: Rack::TradeTracker::Cookie

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/trade_tracker/cookie.rb

Constant Summary collapse

NAME =
'TT2_%{campaign_id}'.freeze
DIGEST_PARAMS =
[:campaign_id, :material_id, :affiliate_id, :reference].freeze
VALUE_PARAMS =
[:material_id, :affiliate_id, :reference].freeze
PATH =
'/'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(domain, parameters) ⇒ Cookie

Returns a new instance of Cookie.



10
11
12
# File 'lib/rack/trade_tracker/cookie.rb', line 10

def initialize(domain, parameters)
  @domain, @parameters = domain, parameters
end

Instance Method Details

#as_hashObject



33
34
35
36
37
38
39
40
# File 'lib/rack/trade_tracker/cookie.rb', line 33

def as_hash
  {
      value: value,
      domain: ".#{domain}",
      path: PATH,
      expires: 1.year.from_now
  }
end

#checksumObject



18
19
20
21
# File 'lib/rack/trade_tracker/cookie.rb', line 18

def checksum
  source = "CHK_#{DIGEST_PARAMS.map { |param| params_hash[param]  }.join('::')}"
  Digest::MD5.hexdigest(source)
end

#nameObject



14
15
16
# File 'lib/rack/trade_tracker/cookie.rb', line 14

def name
  NAME % params_hash
end

#timestampObject



23
24
25
# File 'lib/rack/trade_tracker/cookie.rb', line 23

def timestamp
  Time.now.to_i
end

#valueObject



27
28
29
30
31
# File 'lib/rack/trade_tracker/cookie.rb', line 27

def value
  VALUE_PARAMS.map { |param| params_hash[param] }.tap do |attributes|
    attributes << checksum << timestamp
  end.join('::')
end