Class: Linzer::Signature

Inherits:
Object
  • Object
show all
Defined in:
lib/linzer/signature.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata, value, label, parameters = {}) ⇒ Signature

Returns a new instance of Signature.



5
6
7
8
9
10
11
# File 'lib/linzer/signature.rb', line 5

def initialize(, value, label, parameters = {})
     = .clone.freeze
  @value      = value.clone.freeze
  @parameters = parameters.clone.freeze
  @label      = label.clone.freeze
  freeze
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



13
14
15
# File 'lib/linzer/signature.rb', line 13

def label
  @label
end

#metadataObject (readonly) Also known as: serialized_components

Returns the value of attribute metadata.



13
14
15
# File 'lib/linzer/signature.rb', line 13

def 
  
end

#parametersObject (readonly)

Returns the value of attribute parameters.



13
14
15
# File 'lib/linzer/signature.rb', line 13

def parameters
  @parameters
end

#valueObject (readonly) Also known as: bytes

Returns the value of attribute value.



13
14
15
# File 'lib/linzer/signature.rb', line 13

def value
  @value
end

Class Method Details

.build(headers, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/linzer/signature.rb', line 48

def build(headers, options = {})
  basic_validate headers
  headers.transform_keys!(&:downcase)
  validate headers

  input = parse_structured_field(headers, "signature-input")
  reject_multiple_signatures if input.size > 1 && options[:label].nil?
  label = options[:label] || input.keys.first

  signature = parse_structured_field(headers, "signature")
  fail_with_signature_not_found label unless signature.key?(label)

  raw_signature =
    signature[label].value
      .force_encoding(Encoding::ASCII_8BIT)

  fail_due_invalid_components unless input[label].value.respond_to?(:each)

  components = input[label].value.map { |c| Starry.serialize_item(c) }
  parameters = input[label].parameters

  new(components, raw_signature, label, parameters)
end

Instance Method Details

#componentsObject



17
18
19
# File 'lib/linzer/signature.rb', line 17

def components
  FieldId.deserialize_components(serialized_components)
end

#createdObject



21
22
23
24
25
26
# File 'lib/linzer/signature.rb', line 21

def created
  Integer(parameters["created"])
rescue
  return nil if parameters["created"].nil?
  raise Error.new "Signature has a non-integer `created` parameter"
end

#older_than?(seconds) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/linzer/signature.rb', line 28

def older_than?(seconds)
  raise Error.new "Signature is missing the `created` parameter" if created.nil?
  (Time.now.to_i - created) > seconds
end

#to_hObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/linzer/signature.rb', line 33

def to_h
  {
    "signature"       => Starry.serialize({label => value}),
    "signature-input" => Starry.serialize({
      label => Starry::InnerList.new(
        serialized_components.map { |c| Starry.parse_item(c) },
        parameters
      )
    })
  }
end