Class: PedicelPay::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/pedicel-pay/token.rb

Constant Summary collapse

Error =
Class.new(PedicelPay::Error)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unencrypted_data: nil, encrypted_data: nil, header: nil, signature: nil, version: 'EC_v1') ⇒ Token

Returns a new instance of Token.



16
17
18
19
20
21
22
# File 'lib/pedicel-pay/token.rb', line 16

def initialize(unencrypted_data: nil, encrypted_data: nil, header: nil, signature: nil, version: 'EC_v1')
  @unencrypted_data = unencrypted_data
  @encrypted_data   = encrypted_data
  @header           = header
  @signature        = signature
  @version          = version
end

Instance Attribute Details

#encrypted_dataObject

Returns the value of attribute encrypted_data.



9
10
11
# File 'lib/pedicel-pay/token.rb', line 9

def encrypted_data
  @encrypted_data
end

#headerObject

Returns the value of attribute header.



9
10
11
# File 'lib/pedicel-pay/token.rb', line 9

def header
  @header
end

#signatureObject

Returns the value of attribute signature.



9
10
11
# File 'lib/pedicel-pay/token.rb', line 9

def signature
  @signature
end

#unencrypted_dataObject

Returns the value of attribute unencrypted_data.



9
10
11
# File 'lib/pedicel-pay/token.rb', line 9

def unencrypted_data
  @unencrypted_data
end

#versionObject

Returns the value of attribute version.



9
10
11
# File 'lib/pedicel-pay/token.rb', line 9

def version
  @version
end

Instance Method Details

#sampleObject



45
46
47
48
49
50
# File 'lib/pedicel-pay/token.rb', line 45

def sample
  sample_data
  sample_header

  self
end

#sample_dataObject



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pedicel-pay/token.rb', line 52

def sample_data
  return if encrypted_data

  if unencrypted_data
    unencrypted_data.sample
  else
    self.unencrypted_data = TokenData.new.sample
  end

  self
end

#sample_headerObject



64
65
66
67
68
69
70
71
72
# File 'lib/pedicel-pay/token.rb', line 64

def sample_header
  if header
    header.sample
  else
    self.header = TokenHeader.new.sample
  end

  self
end

#to_hashObject

Raises:



34
35
36
37
38
39
40
41
42
43
# File 'lib/pedicel-pay/token.rb', line 34

def to_hash
  raise Error, 'no encrypted data' unless encrypted_data

  {
    'data'      => Base64.strict_encode64(encrypted_data),
    'header'    => header.to_hash,
    'signature' => signature,
    'version'   => version,
  }
end

#to_jsonObject



30
31
32
# File 'lib/pedicel-pay/token.rb', line 30

def to_json
  to_hash.to_json
end

#update_pubkey_hash(recipient:) ⇒ Object



24
25
26
27
28
# File 'lib/pedicel-pay/token.rb', line 24

def update_pubkey_hash(recipient:)
  pubkey = Helper.recipient_certificate(recipient: recipient)

  header.pubkey_hash = Digest::SHA256.base64digest(pubkey.to_der)
end