Sha256: d5d2b6629349e16cffdb06188b8e658436faf03f63ac2911937a127c46f1534d

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

# encoding: utf-8

module Hyperdrive
  module Utils
    def self.sanitize_keys(keys_to_keep, hash)
      Hash[hash.select do |key, value|
        keys_to_keep.include? key
      end]
    end

    def self.symbolize_keys(hash)
      hash.inject({}) do |result, (key, value)|
       result.merge!(Hash[
          case key
          when String then key.to_sym
          else key
          end,
          case value
          when Hash then symbolize_keys(value)
          when Array then value.map! { |v| v.is_a?(Hash) ? symbolize_keys(v) : v } ; value
          else value
          end
        ])
      end
    end

    def self.enforce_charset!(charset, params)
      encoding = charset.value == '*' ? 'UTF-8' : find_encoding(charset.value)
      params.each_value do |value|
        value.encode!(encoding) if value.is_a? String
      end
    end

    private

    def self.find_encoding(charset_value)
      begin
        Encoding.find(charset_value)
      rescue
        raise Hyperdrive::Errors::NotAcceptable.new(charset_value)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hyperdrive-0.0.22 lib/hyperdrive/utils.rb