Sha256: 24b43c35174e4911f94a4f974d4d0d9ad0de105d055400c26906baf57baac5f5

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 KB

Contents

require 'base64'
require 'zlib'

class UrlStore
  class CompactEncoder
    def initialize(options={})
      @secret = options[:secret] || raise('i need a :secret !!')
      @hasher = options[:hasher] || 'SHA1'
      @serializer = options[:serializer] || :marshal
    end

    def encode(data)
      data = compress(serialize(data))
      data+digest(data)
    end

    def decode(data)
      hash = data[-hash_length..-1]
      data = data[0...-hash_length]

      if digest(data) == hash
        deserialize extract(data)
      else
        nil
      end
    end

    private

    def serialize(data)
      case @serializer.to_sym
      when :yaml then data.to_yaml
      when :marshal then Marshal.dump(data)  
      end
    end

    def deserialize(data)
      case @serializer.to_sym
      when :yaml then YAML.load(data)
      when :marshal then Marshal.load(data)
      end
    end

    def compress(data)
      Base64.encode64( Zlib::Deflate.deflate(data)).gsub("\n",'')
    end

    def extract(data)
      Zlib::Inflate.inflate Base64.decode64(data)
    end

    def hash_length
      digest('x').size
    end

    # stolen from ActiveSupport
    def digest(data)
      require 'openssl' unless defined?(OpenSSL)
      OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new(@hasher.to_s), @secret, data)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
url_store-0.3.4 lib/url_store/compact_encoder.rb
url_store-0.3.2 lib/url_store/compact_encoder.rb
url_store-0.3.1 lib/url_store/compact_encoder.rb
url_store-0.3.0 lib/url_store/compact_encoder.rb