Skip to content

Instantly share code, notes, and snippets.

@diago
Created October 25, 2011 19:19
Show Gist options
  • Save diago/1313915 to your computer and use it in GitHub Desktop.
Save diago/1313915 to your computer and use it in GitHub Desktop.
# Converts a hash into a query string
#
# Hash keys can be underscored names
# @example
# { :service_type => 'rsemail' }
#
# Or the Rackspace API expected string
#
# @param [Hash, #to_hash] hash to convert
# @return [String] query param
def query_string_from_hash( hash )
hash.to_hash.collect do |k, v|
key, *keys = k.to_s.split( '_' )
key << keys.map( &:capitalize ).join( '' )
"%s=%s" % [ key, URI.escape( v.to_s ) ]
end.join( '&' )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment