Module: HasChecksum::ClassMethods

Defined in:
lib/has_checksum.rb

Instance Method Summary collapse

Instance Method Details

#has_checksum(*config) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/has_checksum.rb', line 71

def has_checksum(*config)
  source, options = has_checksum_configure(config)

  if !options[:algorithm].respond_to?(:call)
    # TODO: use OpenSSL here too?
    begin
      options[:algorithm] = Digest.const_get(options[:algorithm].upcase)
    # Digest seems to only raise LoadError here but we add NameError for good measure
    rescue LoadError, NameError
      raise ArgumentError, "unknown algorithm '#{options[:algorithm]}'"
    end
  end

  options[:method] ||= "%s_checksum" % source.join("_")
  define_methods(:calculate_checksum, source, options)
end

#has_signature(*config) ⇒ Object

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/has_checksum.rb', line 55

def has_signature(*config)
  source, options = has_checksum_configure(config)
  raise ArgumentError, "key option required to calculate a signature" unless options[:key]

  if !options[:algorithm].respond_to?(:call)
    begin
      options[:algorithm] = OpenSSL::Digest.new(options[:algorithm])
    rescue RuntimeError
      raise ArgumentError, "unknown algorithm '#{options[:algorithm]}'"
    end
  end

  options[:method] ||= "%s_signature" % source.join("_")
  define_methods(:calculate_signature, source, options)
end