class JSONHasher::Parser
Public Class Methods
new(json_string)
click to toggle source
# File lib/json_hasher.rb, line 7 def initialize(json_string) raise 'JSONHasher only takes a String' unless json_string.is_a?(String) @parsed_json = JSON.parse(json_string) end
Public Instance Methods
to_sha2()
click to toggle source
# File lib/json_hasher.rb, line 12 def to_sha2 hash_parsed_json(@parsed_json) end
Private Instance Methods
hash_array(array)
click to toggle source
# File lib/json_hasher.rb, line 29 def hash_array(array) mapped = array.map do |elm| hash_parsed_json(elm) end hash_other(mapped) end
hash_object(obj)
click to toggle source
# File lib/json_hasher.rb, line 36 def hash_object(obj) pairs = obj.to_a.sort_by(&:first) hash_array(pairs) end
hash_other(value)
click to toggle source
# File lib/json_hasher.rb, line 41 def hash_other(value) value_type = value.class.to_s value_string = value.to_s hashed = Digest::SHA2.new << "#{value_type}\"\"#{value_string}" hashed.to_s end
hash_parsed_json(parsed_json)
click to toggle source
# File lib/json_hasher.rb, line 18 def hash_parsed_json(parsed_json) case parsed_json.class.name when 'Array' hash_array(parsed_json) when 'Hash' hash_object(parsed_json) else hash_other(parsed_json) end end