Sha256: fef7ab99e15af6f609d70814c006f8affcb67565ec1950de032d09fbca11dcec

Contents?: true

Size: 769 Bytes

Versions: 1

Compression:

Stored size: 769 Bytes

Contents

module Rack
  module Reducer
    # Convert params from Sinatra, Rails, Roda, etc into a symbol hash.
    module Parser
      def self.call(data)
        data.is_a?(Hash) ? symbolize(data) : hashify(data)
      end

      def self.symbolize(data)
        data.each_with_object({}) do |(key, val), hash|
          hash[key.to_sym] = val.is_a?(Hash) ? symbolize(val) : val
        end
      end

      # Turns out a Rails params hash is not really a hash.
      # It's safe to call .to_unsafe_hash here, because params
      # are automatically sanitized by the lambda keywords.
      def self.hashify(data)
        fn = %i[to_unsafe_h to_h].find { |name| data.respond_to?(name) }
        symbolize(data.send(fn))
      end
    end

    private_constant :Parser
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-reducer-1.0.1 lib/rack/reducer/parser.rb