Class: Resync::Descriptor

Inherits:
Object
  • Object
show all
Includes:
XML::Mapping
Defined in:
lib/resync/shared/descriptor.rb

Overview

Base class for ResourceSync-specific elements describing a resource or link.

Direct Known Subclasses

Link, Metadata

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(modified_time: nil, length: nil, mime_type: nil, encoding: nil, hashes: nil, path: nil) ⇒ Descriptor

Creates a new Descriptor instance with the specified fields.

Parameters:

  • modified_time (Time) (defaults to: nil)

    The date and time when the referenced resource was last modified.

  • length (Integer) (defaults to: nil)

    The content length of the referenced resource.

  • mime_type (MIME::Type) (defaults to: nil)

    The media type of the referenced resource.

  • encoding (String) (defaults to: nil)

    Any content encoding (if any) applied to the data in the referenced resource (e.g. for compression)

  • hashes (Hash<String, String>) (defaults to: nil)

    Fixity information for the referenced resource, as a map from hash algorithm tokens (e.g. md5, sha-256) to hex-encoded digest values.

  • path (String) (defaults to: nil)

    For ResourceDumpManifests and ChangeDumpManifests, the path to the referenced resource within the dump ZIP file.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/resync/shared/descriptor.rb', line 52

def initialize( # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
    modified_time: nil,
    length: nil,
    mime_type: nil,
    encoding: nil,
    hashes: nil,
    path: nil
)
  self.modified_time = modified_time
  self.length = length
  self.mime_type = mime_type
  self.encoding = encoding
  self.hashes = hashes
  self.path = path
end

Instance Attribute Details

#encodingString

Returns the content encoding (if any) applied to the data in the referenced resource (e.g. for compression).

Returns:

  • (String)

    the content encoding (if any) applied to the data in the referenced resource (e.g. for compression)



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/resync/shared/descriptor.rb', line 24

class Descriptor
  include ::XML::Mapping

  # ------------------------------------------------------------
  # Attributes

  time_node :modified_time, '@modified', default_value: nil
  numeric_node :length, '@length', default_value: nil
  mime_type_node :mime_type, '@type', default_value: nil
  text_node :encoding, '@encoding', default_value: nil
  hash_codes_node :hashes, '@hash', default_value: nil
  text_node :path, '@path', default_value: nil

  # ------------------------------------------------------------
  # Initializer

  # Creates a new +Descriptor+ instance with the specified fields.
  #
  # @param modified_time [Time] The date and time when the referenced resource was last modified.
  # @param length [Integer] The content length of the referenced resource.
  # @param mime_type [MIME::Type] The media type of the referenced resource.
  # @param encoding [String] Any content encoding (if any) applied to the data in the
  #   referenced resource (e.g. for compression)
  # @param hashes [Hash<String, String>] Fixity information for the referenced
  #   resource, as a map from hash algorithm tokens (e.g. +md5+, +sha-256+)
  #   to hex-encoded digest values.
  # @param path [String] For +ResourceDumpManifests+ and +ChangeDumpManifests+,
  #   the path to the referenced resource within the dump ZIP file.
  def initialize( # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
      modified_time: nil,
      length: nil,
      mime_type: nil,
      encoding: nil,
      hashes: nil,
      path: nil
  )
    self.modified_time = modified_time
    self.length = length
    self.mime_type = mime_type
    self.encoding = encoding
    self.hashes = hashes
    self.path = path
  end

  # ------------------------------------------------------------
  # Custom setters

  def modified_time=(value)
    @modified_time = time_or_nil(value)
  end

  def length=(value)
    @length = natural_number_or_nil(value)
  end

  def mime_type=(value)
    @mime_type = mime_type_or_nil(value)
  end

  def hashes=(value)
    @hashes = Descriptor.hash_of_hashcodes(value)
  end

  # ------------------------------------------------------------
  # Public methods

  # Gets the hash value for the specified algorithm.
  #
  # @param algorithm [String] The token (e.g. +md5+, +sha-256+) for the hash algorithm.
  # @return [String] The hex-encoded digest value.
  def hash(algorithm)
    hashes[algorithm]
  end

  # ------------------------------
  # Conversions

  def self.hash_of_hashcodes(hashes)
    return {} unless hashes
    return hashes if hashes.is_a?(Hash)
    hashes.split(/[[:space:]]+/).map { |hash| hash.split(':') }.to_h
  end

  # ------------------------------------------------------------
  # Private methods

  private

  # ------------------------------
  # Parameter validators

  def time_or_nil(time)
    fail ArgumentError, "time #{time} is not a Time" if time && !time.is_a?(Time)
    time
  end

  def natural_number_or_nil(value)
    fail ArgumentError, "value #{value} must be a non-negative integer" if value && (!value.is_a?(Integer) || value < 0)
    value
  end

  def mime_type_or_nil(mime_type)
    return nil unless mime_type
    return mime_type if mime_type.is_a?(MIME::Type)

    mt = MIME::Types[mime_type].first
    return mt if mt

    MIME::Type.new(mime_type)
  end

end

#hashesHash<String, String>

Returns fixity information for the referenced resource, as a map from hash algorithm tokens (e.g. md5, sha-256) to hex-encoded digest values.

Returns:

  • (Hash<String, String>)

    fixity information for the referenced resource, as a map from hash algorithm tokens (e.g. md5, sha-256) to hex-encoded digest values.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/resync/shared/descriptor.rb', line 24

class Descriptor
  include ::XML::Mapping

  # ------------------------------------------------------------
  # Attributes

  time_node :modified_time, '@modified', default_value: nil
  numeric_node :length, '@length', default_value: nil
  mime_type_node :mime_type, '@type', default_value: nil
  text_node :encoding, '@encoding', default_value: nil
  hash_codes_node :hashes, '@hash', default_value: nil
  text_node :path, '@path', default_value: nil

  # ------------------------------------------------------------
  # Initializer

  # Creates a new +Descriptor+ instance with the specified fields.
  #
  # @param modified_time [Time] The date and time when the referenced resource was last modified.
  # @param length [Integer] The content length of the referenced resource.
  # @param mime_type [MIME::Type] The media type of the referenced resource.
  # @param encoding [String] Any content encoding (if any) applied to the data in the
  #   referenced resource (e.g. for compression)
  # @param hashes [Hash<String, String>] Fixity information for the referenced
  #   resource, as a map from hash algorithm tokens (e.g. +md5+, +sha-256+)
  #   to hex-encoded digest values.
  # @param path [String] For +ResourceDumpManifests+ and +ChangeDumpManifests+,
  #   the path to the referenced resource within the dump ZIP file.
  def initialize( # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
      modified_time: nil,
      length: nil,
      mime_type: nil,
      encoding: nil,
      hashes: nil,
      path: nil
  )
    self.modified_time = modified_time
    self.length = length
    self.mime_type = mime_type
    self.encoding = encoding
    self.hashes = hashes
    self.path = path
  end

  # ------------------------------------------------------------
  # Custom setters

  def modified_time=(value)
    @modified_time = time_or_nil(value)
  end

  def length=(value)
    @length = natural_number_or_nil(value)
  end

  def mime_type=(value)
    @mime_type = mime_type_or_nil(value)
  end

  def hashes=(value)
    @hashes = Descriptor.hash_of_hashcodes(value)
  end

  # ------------------------------------------------------------
  # Public methods

  # Gets the hash value for the specified algorithm.
  #
  # @param algorithm [String] The token (e.g. +md5+, +sha-256+) for the hash algorithm.
  # @return [String] The hex-encoded digest value.
  def hash(algorithm)
    hashes[algorithm]
  end

  # ------------------------------
  # Conversions

  def self.hash_of_hashcodes(hashes)
    return {} unless hashes
    return hashes if hashes.is_a?(Hash)
    hashes.split(/[[:space:]]+/).map { |hash| hash.split(':') }.to_h
  end

  # ------------------------------------------------------------
  # Private methods

  private

  # ------------------------------
  # Parameter validators

  def time_or_nil(time)
    fail ArgumentError, "time #{time} is not a Time" if time && !time.is_a?(Time)
    time
  end

  def natural_number_or_nil(value)
    fail ArgumentError, "value #{value} must be a non-negative integer" if value && (!value.is_a?(Integer) || value < 0)
    value
  end

  def mime_type_or_nil(mime_type)
    return nil unless mime_type
    return mime_type if mime_type.is_a?(MIME::Type)

    mt = MIME::Types[mime_type].first
    return mt if mt

    MIME::Type.new(mime_type)
  end

end

#lengthInteger

Returns the content length of the referenced resource.

Returns:

  • (Integer)

    the content length of the referenced resource.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/resync/shared/descriptor.rb', line 24

class Descriptor
  include ::XML::Mapping

  # ------------------------------------------------------------
  # Attributes

  time_node :modified_time, '@modified', default_value: nil
  numeric_node :length, '@length', default_value: nil
  mime_type_node :mime_type, '@type', default_value: nil
  text_node :encoding, '@encoding', default_value: nil
  hash_codes_node :hashes, '@hash', default_value: nil
  text_node :path, '@path', default_value: nil

  # ------------------------------------------------------------
  # Initializer

  # Creates a new +Descriptor+ instance with the specified fields.
  #
  # @param modified_time [Time] The date and time when the referenced resource was last modified.
  # @param length [Integer] The content length of the referenced resource.
  # @param mime_type [MIME::Type] The media type of the referenced resource.
  # @param encoding [String] Any content encoding (if any) applied to the data in the
  #   referenced resource (e.g. for compression)
  # @param hashes [Hash<String, String>] Fixity information for the referenced
  #   resource, as a map from hash algorithm tokens (e.g. +md5+, +sha-256+)
  #   to hex-encoded digest values.
  # @param path [String] For +ResourceDumpManifests+ and +ChangeDumpManifests+,
  #   the path to the referenced resource within the dump ZIP file.
  def initialize( # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
      modified_time: nil,
      length: nil,
      mime_type: nil,
      encoding: nil,
      hashes: nil,
      path: nil
  )
    self.modified_time = modified_time
    self.length = length
    self.mime_type = mime_type
    self.encoding = encoding
    self.hashes = hashes
    self.path = path
  end

  # ------------------------------------------------------------
  # Custom setters

  def modified_time=(value)
    @modified_time = time_or_nil(value)
  end

  def length=(value)
    @length = natural_number_or_nil(value)
  end

  def mime_type=(value)
    @mime_type = mime_type_or_nil(value)
  end

  def hashes=(value)
    @hashes = Descriptor.hash_of_hashcodes(value)
  end

  # ------------------------------------------------------------
  # Public methods

  # Gets the hash value for the specified algorithm.
  #
  # @param algorithm [String] The token (e.g. +md5+, +sha-256+) for the hash algorithm.
  # @return [String] The hex-encoded digest value.
  def hash(algorithm)
    hashes[algorithm]
  end

  # ------------------------------
  # Conversions

  def self.hash_of_hashcodes(hashes)
    return {} unless hashes
    return hashes if hashes.is_a?(Hash)
    hashes.split(/[[:space:]]+/).map { |hash| hash.split(':') }.to_h
  end

  # ------------------------------------------------------------
  # Private methods

  private

  # ------------------------------
  # Parameter validators

  def time_or_nil(time)
    fail ArgumentError, "time #{time} is not a Time" if time && !time.is_a?(Time)
    time
  end

  def natural_number_or_nil(value)
    fail ArgumentError, "value #{value} must be a non-negative integer" if value && (!value.is_a?(Integer) || value < 0)
    value
  end

  def mime_type_or_nil(mime_type)
    return nil unless mime_type
    return mime_type if mime_type.is_a?(MIME::Type)

    mt = MIME::Types[mime_type].first
    return mt if mt

    MIME::Type.new(mime_type)
  end

end

#mime_typeMIME::Type

Returns the media type of the referenced resource.

Returns:

  • (MIME::Type)

    the media type of the referenced resource.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/resync/shared/descriptor.rb', line 24

class Descriptor
  include ::XML::Mapping

  # ------------------------------------------------------------
  # Attributes

  time_node :modified_time, '@modified', default_value: nil
  numeric_node :length, '@length', default_value: nil
  mime_type_node :mime_type, '@type', default_value: nil
  text_node :encoding, '@encoding', default_value: nil
  hash_codes_node :hashes, '@hash', default_value: nil
  text_node :path, '@path', default_value: nil

  # ------------------------------------------------------------
  # Initializer

  # Creates a new +Descriptor+ instance with the specified fields.
  #
  # @param modified_time [Time] The date and time when the referenced resource was last modified.
  # @param length [Integer] The content length of the referenced resource.
  # @param mime_type [MIME::Type] The media type of the referenced resource.
  # @param encoding [String] Any content encoding (if any) applied to the data in the
  #   referenced resource (e.g. for compression)
  # @param hashes [Hash<String, String>] Fixity information for the referenced
  #   resource, as a map from hash algorithm tokens (e.g. +md5+, +sha-256+)
  #   to hex-encoded digest values.
  # @param path [String] For +ResourceDumpManifests+ and +ChangeDumpManifests+,
  #   the path to the referenced resource within the dump ZIP file.
  def initialize( # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
      modified_time: nil,
      length: nil,
      mime_type: nil,
      encoding: nil,
      hashes: nil,
      path: nil
  )
    self.modified_time = modified_time
    self.length = length
    self.mime_type = mime_type
    self.encoding = encoding
    self.hashes = hashes
    self.path = path
  end

  # ------------------------------------------------------------
  # Custom setters

  def modified_time=(value)
    @modified_time = time_or_nil(value)
  end

  def length=(value)
    @length = natural_number_or_nil(value)
  end

  def mime_type=(value)
    @mime_type = mime_type_or_nil(value)
  end

  def hashes=(value)
    @hashes = Descriptor.hash_of_hashcodes(value)
  end

  # ------------------------------------------------------------
  # Public methods

  # Gets the hash value for the specified algorithm.
  #
  # @param algorithm [String] The token (e.g. +md5+, +sha-256+) for the hash algorithm.
  # @return [String] The hex-encoded digest value.
  def hash(algorithm)
    hashes[algorithm]
  end

  # ------------------------------
  # Conversions

  def self.hash_of_hashcodes(hashes)
    return {} unless hashes
    return hashes if hashes.is_a?(Hash)
    hashes.split(/[[:space:]]+/).map { |hash| hash.split(':') }.to_h
  end

  # ------------------------------------------------------------
  # Private methods

  private

  # ------------------------------
  # Parameter validators

  def time_or_nil(time)
    fail ArgumentError, "time #{time} is not a Time" if time && !time.is_a?(Time)
    time
  end

  def natural_number_or_nil(value)
    fail ArgumentError, "value #{value} must be a non-negative integer" if value && (!value.is_a?(Integer) || value < 0)
    value
  end

  def mime_type_or_nil(mime_type)
    return nil unless mime_type
    return mime_type if mime_type.is_a?(MIME::Type)

    mt = MIME::Types[mime_type].first
    return mt if mt

    MIME::Type.new(mime_type)
  end

end

#modified_timeTime

Returns the date and time when the referenced resource was last modified.

Returns:

  • (Time)

    the date and time when the referenced resource was last modified.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/resync/shared/descriptor.rb', line 24

class Descriptor
  include ::XML::Mapping

  # ------------------------------------------------------------
  # Attributes

  time_node :modified_time, '@modified', default_value: nil
  numeric_node :length, '@length', default_value: nil
  mime_type_node :mime_type, '@type', default_value: nil
  text_node :encoding, '@encoding', default_value: nil
  hash_codes_node :hashes, '@hash', default_value: nil
  text_node :path, '@path', default_value: nil

  # ------------------------------------------------------------
  # Initializer

  # Creates a new +Descriptor+ instance with the specified fields.
  #
  # @param modified_time [Time] The date and time when the referenced resource was last modified.
  # @param length [Integer] The content length of the referenced resource.
  # @param mime_type [MIME::Type] The media type of the referenced resource.
  # @param encoding [String] Any content encoding (if any) applied to the data in the
  #   referenced resource (e.g. for compression)
  # @param hashes [Hash<String, String>] Fixity information for the referenced
  #   resource, as a map from hash algorithm tokens (e.g. +md5+, +sha-256+)
  #   to hex-encoded digest values.
  # @param path [String] For +ResourceDumpManifests+ and +ChangeDumpManifests+,
  #   the path to the referenced resource within the dump ZIP file.
  def initialize( # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
      modified_time: nil,
      length: nil,
      mime_type: nil,
      encoding: nil,
      hashes: nil,
      path: nil
  )
    self.modified_time = modified_time
    self.length = length
    self.mime_type = mime_type
    self.encoding = encoding
    self.hashes = hashes
    self.path = path
  end

  # ------------------------------------------------------------
  # Custom setters

  def modified_time=(value)
    @modified_time = time_or_nil(value)
  end

  def length=(value)
    @length = natural_number_or_nil(value)
  end

  def mime_type=(value)
    @mime_type = mime_type_or_nil(value)
  end

  def hashes=(value)
    @hashes = Descriptor.hash_of_hashcodes(value)
  end

  # ------------------------------------------------------------
  # Public methods

  # Gets the hash value for the specified algorithm.
  #
  # @param algorithm [String] The token (e.g. +md5+, +sha-256+) for the hash algorithm.
  # @return [String] The hex-encoded digest value.
  def hash(algorithm)
    hashes[algorithm]
  end

  # ------------------------------
  # Conversions

  def self.hash_of_hashcodes(hashes)
    return {} unless hashes
    return hashes if hashes.is_a?(Hash)
    hashes.split(/[[:space:]]+/).map { |hash| hash.split(':') }.to_h
  end

  # ------------------------------------------------------------
  # Private methods

  private

  # ------------------------------
  # Parameter validators

  def time_or_nil(time)
    fail ArgumentError, "time #{time} is not a Time" if time && !time.is_a?(Time)
    time
  end

  def natural_number_or_nil(value)
    fail ArgumentError, "value #{value} must be a non-negative integer" if value && (!value.is_a?(Integer) || value < 0)
    value
  end

  def mime_type_or_nil(mime_type)
    return nil unless mime_type
    return mime_type if mime_type.is_a?(MIME::Type)

    mt = MIME::Types[mime_type].first
    return mt if mt

    MIME::Type.new(mime_type)
  end

end

#pathString

Returns for ResourceDumpManifests and ChangeDumpManifests, the path to the referenced resource within the dump ZIP file.

Returns:

  • (String)

    for ResourceDumpManifests and ChangeDumpManifests, the path to the referenced resource within the dump ZIP file.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/resync/shared/descriptor.rb', line 24

class Descriptor
  include ::XML::Mapping

  # ------------------------------------------------------------
  # Attributes

  time_node :modified_time, '@modified', default_value: nil
  numeric_node :length, '@length', default_value: nil
  mime_type_node :mime_type, '@type', default_value: nil
  text_node :encoding, '@encoding', default_value: nil
  hash_codes_node :hashes, '@hash', default_value: nil
  text_node :path, '@path', default_value: nil

  # ------------------------------------------------------------
  # Initializer

  # Creates a new +Descriptor+ instance with the specified fields.
  #
  # @param modified_time [Time] The date and time when the referenced resource was last modified.
  # @param length [Integer] The content length of the referenced resource.
  # @param mime_type [MIME::Type] The media type of the referenced resource.
  # @param encoding [String] Any content encoding (if any) applied to the data in the
  #   referenced resource (e.g. for compression)
  # @param hashes [Hash<String, String>] Fixity information for the referenced
  #   resource, as a map from hash algorithm tokens (e.g. +md5+, +sha-256+)
  #   to hex-encoded digest values.
  # @param path [String] For +ResourceDumpManifests+ and +ChangeDumpManifests+,
  #   the path to the referenced resource within the dump ZIP file.
  def initialize( # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
      modified_time: nil,
      length: nil,
      mime_type: nil,
      encoding: nil,
      hashes: nil,
      path: nil
  )
    self.modified_time = modified_time
    self.length = length
    self.mime_type = mime_type
    self.encoding = encoding
    self.hashes = hashes
    self.path = path
  end

  # ------------------------------------------------------------
  # Custom setters

  def modified_time=(value)
    @modified_time = time_or_nil(value)
  end

  def length=(value)
    @length = natural_number_or_nil(value)
  end

  def mime_type=(value)
    @mime_type = mime_type_or_nil(value)
  end

  def hashes=(value)
    @hashes = Descriptor.hash_of_hashcodes(value)
  end

  # ------------------------------------------------------------
  # Public methods

  # Gets the hash value for the specified algorithm.
  #
  # @param algorithm [String] The token (e.g. +md5+, +sha-256+) for the hash algorithm.
  # @return [String] The hex-encoded digest value.
  def hash(algorithm)
    hashes[algorithm]
  end

  # ------------------------------
  # Conversions

  def self.hash_of_hashcodes(hashes)
    return {} unless hashes
    return hashes if hashes.is_a?(Hash)
    hashes.split(/[[:space:]]+/).map { |hash| hash.split(':') }.to_h
  end

  # ------------------------------------------------------------
  # Private methods

  private

  # ------------------------------
  # Parameter validators

  def time_or_nil(time)
    fail ArgumentError, "time #{time} is not a Time" if time && !time.is_a?(Time)
    time
  end

  def natural_number_or_nil(value)
    fail ArgumentError, "value #{value} must be a non-negative integer" if value && (!value.is_a?(Integer) || value < 0)
    value
  end

  def mime_type_or_nil(mime_type)
    return nil unless mime_type
    return mime_type if mime_type.is_a?(MIME::Type)

    mt = MIME::Types[mime_type].first
    return mt if mt

    MIME::Type.new(mime_type)
  end

end

Class Method Details

.hash_of_hashcodes(hashes) ⇒ Object


Conversions



101
102
103
104
105
# File 'lib/resync/shared/descriptor.rb', line 101

def self.hash_of_hashcodes(hashes)
  return {} unless hashes
  return hashes if hashes.is_a?(Hash)
  hashes.split(/[[:space:]]+/).map { |hash| hash.split(':') }.to_h
end

Instance Method Details

#hash(algorithm) ⇒ String

Gets the hash value for the specified algorithm.

Parameters:

  • algorithm (String)

    The token (e.g. md5, sha-256) for the hash algorithm.

Returns:

  • (String)

    The hex-encoded digest value.



94
95
96
# File 'lib/resync/shared/descriptor.rb', line 94

def hash(algorithm)
  hashes[algorithm]
end