class Mindee::Input::LocalResponse
Response loaded locally.
def self.process_secret_key(secret_key)
-
(String)
-
Parameters:
-
secret_key
(String
) -- the secret key as plain text.
def self.process_secret_key(secret_key) secret_key.is_a?(String) ? secret_key.encode('utf-8') : secret_key end
def as_hash
-
(Hash)
-
def as_hash @file.rewind file_str = @file.read JSON.parse(file_str, object_class: Hash) rescue JSON::ParserError raise Errors::MindeeInputError, "File is not a valid dict. #{file_str}" end
def get_hmac_signature(secret_key)
-
(String)
-
Parameters:
-
secret_key
(String
) -- [String] Secret key, either a string or a byte/byte array.
def get_hmac_signature(secret_key) algorithm = OpenSSL::Digest.new('sha256') begin @file.rewind mac = OpenSSL::HMAC.hexdigest(algorithm, self.class.process_secret_key(secret_key), @file.read) rescue StandardError raise Errors::MindeeInputError, 'Could not get HMAC signature from payload.' end mac end
def initialize(input_file)
-
input_file
(File, Tempfile, IO, StringIO, String, Pathname
) -- The input file, which can be a StringIO.
def initialize(input_file) case input_file when IO, StringIO, File, Tempfile str_stripped = input_file.read.to_s.gsub(%r{[\r\n]}, '') @file = StringIO.new(str_stripped) @file.rewind when Pathname, String @file = if Pathname(input_file.to_s).exist? StringIO.new(File.read(input_file.to_s, encoding: 'utf-8').gsub(%r{[\r\n]}, '')) else StringIO.new(input_file.to_s.gsub(%r{[\r\n]}, '')) end @file.rewind else raise Errors::MindeeInputError, "Incompatible type for input '#{input_file.class}'." end end
def valid_hmac_signature?(secret_key, signature)
-
(bool)
-
Parameters:
-
signature
(String
) -- -
secret_key
(String
) -- Secret key, either a string or a byte/byte array.
def valid_hmac_signature?(secret_key, signature) signature == get_hmac_signature(secret_key) end