Sha256: ba0de182403546e22121cad62d601bb757ab1857e9cde9f047f4c0b8525ca20f

Contents?: true

Size: 1.54 KB

Versions: 12

Compression:

Stored size: 1.54 KB

Contents

require 'jellyfish'
require 'rack/request'


module Jellyfish
  module NormalizedParams
    attr_reader :params
    def block_call argument, block
      initialize_params(argument)
      super
    end

    private
    def initialize_params argument
      @params = force_encoding(indifferent_params(
      if argument.kind_of?(MatchData)
        # merge captured data from matcher into params as sinatra
        request.params.merge(Hash[argument.names.zip(argument.captures)])
      else
        request.params
      end))
    end

    # stolen from sinatra
    # Enable string or symbol key access to the nested params hash.
    def indifferent_params(params)
      params = indifferent_hash.merge(params)
      params.each do |key, value|
        next unless value.is_a?(Hash)
        params[key] = indifferent_params(value)
      end
    end

    # stolen from sinatra
    # Creates a Hash with indifferent access.
    def indifferent_hash
      Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
    end

    # stolen from sinatra
    # Fixes encoding issues by casting params to Encoding.default_external
    def force_encoding(data, encoding=Encoding.default_external)
      return data if data.respond_to?(:rewind) # e.g. Tempfile, File, etc
      if data.respond_to?(:force_encoding)
        data.force_encoding(encoding).encode!
      elsif data.respond_to?(:each_value)
        data.each_value{ |v| force_encoding(v, encoding) }
      elsif data.respond_to?(:each)
        data.each{ |v| force_encoding(v, encoding) }
      end
      data
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
jellyfish-1.2.2 lib/jellyfish/normalized_params.rb
jellyfish-1.2.1 lib/jellyfish/normalized_params.rb
jellyfish-1.2.0 lib/jellyfish/normalized_params.rb
jellyfish-1.1.1 lib/jellyfish/normalized_params.rb
jellyfish-1.1.0 lib/jellyfish/normalized_params.rb
jellyfish-1.0.2 lib/jellyfish/normalized_params.rb
jellyfish-1.0.1 lib/jellyfish/normalized_params.rb
jellyfish-1.0.0 lib/jellyfish/normalized_params.rb
jellyfish-0.9.2 lib/jellyfish/normalized_params.rb
jellyfish-0.9.1 lib/jellyfish/normalized_params.rb
jellyfish-0.9.0 lib/jellyfish/normalized_params.rb
jellyfish-0.8.0 lib/jellyfish/normalized_params.rb