Skip to content

Instantly share code, notes, and snippets.

@granolocks
Created September 27, 2012 16:17
Show Gist options
  • Select an option

  • Save granolocks/3794899 to your computer and use it in GitHub Desktop.

Select an option

Save granolocks/3794899 to your computer and use it in GitHub Desktop.
shell words escape
require 'rubygems'
require 'sinatra'
require 'pry'
# run with:
# ruby params.rb
# test route example: http://localhost:4567/blah/blee!!;;;
get '/:test/:test2' do
sanitize_params
return @params.inspect
end
def sanitize_params
if @params.size > 0
@params = shell_escape_hash(@params)
end
end
def shell_escape_hash(input_hash={})
input_hash = input_hash.inject({}) do |c,(k,v)|
if v.class == Array
c[k] = v.map{|e| Shellwords.escape(e)}
elsif v.class == Hash
# Yay recursion!
c[k] = shell_escape_hash(v)
else
c[k] = Shellwords.escape(v)
end
c
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment