Class: PiiScrubber::Vault::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/pii_scrubber/vault/session.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text:, mappings:, id: nil, created_at: nil) ⇒ Session

Returns a new instance of Session.



11
12
13
14
15
16
# File 'lib/pii_scrubber/vault/session.rb', line 11

def initialize(text:, mappings:, id: nil, created_at: nil)
  @id = id || SecureRandom.hex(8)
  @text = text
  @mappings = mappings || {}
  @created_at = created_at || Time.now.utc
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



9
10
11
# File 'lib/pii_scrubber/vault/session.rb', line 9

def created_at
  @created_at
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/pii_scrubber/vault/session.rb', line 9

def id
  @id
end

#mappingsObject (readonly)

Returns the value of attribute mappings.



9
10
11
# File 'lib/pii_scrubber/vault/session.rb', line 9

def mappings
  @mappings
end

#textObject (readonly)

Returns the value of attribute text.



9
10
11
# File 'lib/pii_scrubber/vault/session.rb', line 9

def text
  @text
end

Class Method Details

.from_h(hash) ⇒ Object

Deserializes session from Hash



43
44
45
46
47
48
49
50
51
52
# File 'lib/pii_scrubber/vault/session.rb', line 43

def self.from_h(hash)
  return nil if hash.nil?

  new(
    id: hash["id"] || hash[:id],
    text: hash["text"] || hash[:text],
    mappings: hash["mappings"] || hash[:mappings] || {},
    created_at: hash["created_at"] ? Time.parse(hash["created_at"].to_s) : Time.now.utc
  )
end

Instance Method Details

#restore(data) ⇒ Object

Restores original values in text, hashes, or arrays containing vault placeholders



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pii_scrubber/vault/session.rb', line 19

def restore(data)
  case data
  when String
    restore_string(data)
  when Hash
    restore_hash(data)
  when Array
    data.map { |item| restore(item) }
  else
    data
  end
end

#to_hObject

Serializes session for storing in Redis, Memcached, or Rails sessions



33
34
35
36
37
38
39
40
# File 'lib/pii_scrubber/vault/session.rb', line 33

def to_h
  {
    "id" => id,
    "text" => text,
    "mappings" => mappings,
    "created_at" => created_at.iso8601
  }
end