/usr/lib/ruby/vendor_ruby/rack/typhoeus/middleware/params_decoder/helper.rb is in ruby-typhoeus 0.6.8-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | module Rack
module Typhoeus
module Middleware
class ParamsDecoder
module Helper
# Recursively decodes Typhoeus encoded arrays in given Hash.
#
# @example Use directly in a Rails controller.
# class ApplicationController
# before_filter :decode_typhoeus_arrays
# end
#
# @author Dwayne Macgowan
#
def decode_typhoeus_arrays
decode!(params)
end
# Recursively decodes Typhoeus encoded arrays in given Hash.
#
# @param hash [Hash]. This Hash will be modified!
#
# @return [Hash] Hash with properly decoded nested arrays.
def decode!(hash)
return hash unless hash.is_a?(Hash)
hash.each_pair do |key,value|
if value.is_a?(Hash)
decode!(value)
hash[key] = convert(value)
end
end
hash
end
def decode(hash)
decode!(hash.dup)
end
private
# Checks if Hash is an Array encoded as a Hash.
# Specifically will check for the Hash to have this
# form: {'0' => v0, '1' => v1, .., 'n' => vN }
#
# @param hash [Hash]
#
# @return [Boolean] True if its a encoded Array, else false.
def encoded?(hash)
return false if hash.empty?
if hash.keys.size > 1
keys = hash.keys.map{|i| i.to_i if i.respond_to?(:to_i)}.sort
keys == hash.keys.size.times.to_a
else
hash.keys.first =~ /0/
end
end
# If the Hash is an array encoded by typhoeus an array is returned
# else the self is returned
#
# @param hash [Hash] The Hash to convert into an Array.
#
# @return [Arraya/Hash]
def convert(hash)
if encoded?(hash)
Hash[hash.sort].values
else
hash
end
end
end
end
end
end
end
|