Sha256: 813226eb325b78fa3b98d69409882edddfa095c703132c58d2e91a87609d76d6

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

require 'digest/md5'
require 'murmurhash3'
# see below for requiring either cgi (on JRuby) or escape_utils (if available)

require "hash_digest/version"

module HashDigest
  # CURRENT

  def self.as_digest2(obj)
    obj.to_hash_digest_query
  end

  def self.digest2(obj)
    ::MurmurHash3::V32.str_hash(as_digest2(obj)).to_s 36
  end

  # LEGACY

  def self.as_digest1(obj)
    ordered_list = case obj
    when ::Hash
      obj.map do |k, v|
        obj[k].to_hash_digest_query k
      end.sort
    when ::Array
      ary = []
      obj.each_with_index do |v, i|
        ary << v.to_hash_digest_query(i)
      end
      ary
    else
      raise ::ArgumentError, "[hash_digest gem] Can only digest Hashes and Arrays, not #{obj.class}"
    end
    ordered_list.join '&'
  end

  def self.hexdigest(obj)
    ::Digest::MD5.hexdigest as_digest1(obj)
  end

  # CGI escaping engine

  if RUBY_PLATFORM == 'java'
    require 'cgi'
    def self.escape_url(str);     ::CGI.escape(str) end
  else
    begin
      # MUCH faster than stdlib's cgi
      require 'escape_utils'
      def self.escape_url(str);   ::EscapeUtils.escape_url(str) end
    rescue LoadError
      require 'cgi'
      def self.escape_url(str);   ::CGI.escape(str) end
    end
  end
end

# EVERYTHING BELOW IS COPIED FROM ACTIVESUPPORT 4.0.2

class Object
  def to_hash_digest_param
    to_s
  end

  def to_hash_digest_query(key)
    "#{::HashDigest.escape_url(key.to_hash_digest_param)}=#{::HashDigest.escape_url(to_hash_digest_param)}"
  end
end

class Array
  def to_hash_digest_param
    map { |e| e.to_hash_digest_param }.join '/'
  end

  def to_hash_digest_query(key = nil)
    prefix = "#{key}[]"
    map { |value| value.to_hash_digest_query(prefix) }.join '&'
  end
end

class Hash
  def to_hash_digest_param(namespace = nil)
    map do |key, value|
      value.to_hash_digest_query(namespace ? "#{namespace}[#{key}]" : key)
    end.sort.join '&'
  end
  alias_method :to_hash_digest_query, :to_hash_digest_param
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hash_digest-1.1.2 lib/hash_digest.rb