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 = {})
  @metadata   = .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: components

Returns the value of attribute metadata.



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

def 
  @metadata
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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/linzer/signature.rb', line 41

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(&:value)
  parameters = input[label].parameters

  new(components, raw_signature, label, parameters)
end

Instance Method Details

#createdObject



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

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)


24
25
26
27
# File 'lib/linzer/signature.rb', line 24

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



29
30
31
32
33
34
35
36
# File 'lib/linzer/signature.rb', line 29

def to_h
  {
    "signature" => Starry.serialize({label => value}),
    "signature-input" =>
      Starry.serialize({label =>
        Starry::InnerList.new(components, parameters)})
  }
end