Last active
August 29, 2015 14:17
-
-
Save camertron/b3dbbba95e1e04a48477 to your computer and use it in GitHub Desktop.
Converts params that appear in a Rails log to HTTP GET/POST params
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'cgi' | |
def convert(data) | |
params = JSON.parse(data.gsub('=>', ':')) | |
[].tap do |out| | |
walk_hash(params, out, []) | |
end.join('&') | |
end | |
def walk_hash(hash, out, prefixes) | |
hash.each_pair do |key, val| | |
if val.is_a?(Hash) | |
walk_hash(val, out, prefixes + [key]) | |
elsif val.is_a?(Array) | |
val.each do |elem| | |
out << "#{format_prefixes(prefixes + [key])}[]=#{CGI.escape(val.to_s)}" | |
end | |
else | |
out << "#{format_prefixes(prefixes + [key])}=#{CGI.escape(val.to_s)}" | |
end | |
end | |
end | |
def format_prefixes(prefixes) | |
if prefixes.empty? | |
'' | |
else | |
"#{prefixes.first}#{prefixes[1..-1].map { |p| "[#{p}]" }.join}" | |
end | |
end | |
params = '{"user"=>{"first_name"=>"[FILTERED]", "referral"=>["1234"], "timezone_offset_minutes"=>-120, "locale"=>"en", "date_of_birth"=>"2000-1-1", "unique_name_is_unspecified"=>"[FILTERED]", "assignment_id"=>"abc123", "password"=>"[FILTERED]", "send_email"=>true, "email_address"=>"[email protected]"}, "assignment_id"=>"def456c", "client_secret"=>"fakefakefake", "client_id"=>"blarg"}' | |
puts convert(params) | |
# prints: | |
# user[first_name]=%5BFILTERED%5D&user[referral][]=%5B%221234%22%5D&user[timezone_offset_minutes]=-120&user[locale]=en&user[date_of_birth]=2000-1-1&user[unique_name_is_unspecified]=%5BFILTERED%5D&user[assignment_id]=abc123&user[password]=%5BFILTERED%5D&user[send_email]=true&user[email_address]=foo%40bar.com&assignment_id=def456c&client_secret=fakefakefake&client_id=blarg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment