Class: Altcha::V2::Challenge

Inherits:
Object
  • Object
show all
Defined in:
lib/altcha/v2.rb

Overview

A v2 challenge as returned by V2.create_challenge.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters:, signature: nil) ⇒ Challenge

Returns a new instance of Challenge.



65
66
67
68
# File 'lib/altcha/v2.rb', line 65

def initialize(parameters:, signature: nil)
  @parameters = parameters
  @signature  = signature
end

Instance Attribute Details

#parametersObject

Returns the value of attribute parameters.



63
64
65
# File 'lib/altcha/v2.rb', line 63

def parameters
  @parameters
end

#signatureObject

Returns the value of attribute signature.



63
64
65
# File 'lib/altcha/v2.rb', line 63

def signature
  @signature
end

Class Method Details

.from_h(data) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/altcha/v2.rb', line 80

def self.from_h(data)
  p = data['parameters']
  new(
    parameters: ChallengeParameters.new(
      algorithm:    p['algorithm'],
      nonce:        p['nonce'],
      salt:         p['salt'],
      cost:         p['cost'],
      key_length:   p.fetch('keyLength',  DEFAULT_KEY_LENGTH),
      key_prefix:   p.fetch('keyPrefix',  DEFAULT_KEY_PREFIX),
      key_signature: p['keySignature'],
      memory_cost:  p['memoryCost'],
      parallelism:  p['parallelism'],
      expires_at:   p['expiresAt'],
      data:         p['data']
    ),
    signature: data['signature']
  )
end

.from_json(string) ⇒ Object



100
101
102
# File 'lib/altcha/v2.rb', line 100

def self.from_json(string)
  from_h(JSON.parse(string))
end

Instance Method Details

#to_hObject



70
71
72
73
74
# File 'lib/altcha/v2.rb', line 70

def to_h
  h = { 'parameters' => parameters.to_h }
  h['signature'] = signature unless signature.nil?
  h
end

#to_json(options = {}) ⇒ Object



76
77
78
# File 'lib/altcha/v2.rb', line 76

def to_json(options = {})
  to_h.to_json(options)
end