Skip to content

Instantly share code, notes, and snippets.

@dolarsrg
Created February 29, 2012 16:12
Show Gist options
  • Save dolarsrg/1942070 to your computer and use it in GitHub Desktop.
Save dolarsrg/1942070 to your computer and use it in GitHub Desktop.
Takes an url, detects its params and adds to it the vars from the hash
# Takes an url, detects its params and adds to it the vars from the hash
# Example of use:
# > add_url_vars("http://www.example.com/signup?token=HIWORLD", {:s => 'HIAGAIN', :s32 => 'BYE'})
# => "http://www.example.com/signup?token=HIWORLD&s=HIAGAIN&s32=BYE"
def add_url_vars(url, vars={})
uri = URI.parse(url)
pre_vars = get_url_vars(url)
vars_total = []
for var in pre_vars
vars_total << "#{var[0]}=#{var[1][0]}"
end
for var in vars
vars_total << "#{var[0]}=#{var[1]}"
end
return "#{uri.scheme}://#{uri.host}#{uri.path}?#{vars_total.join('&')}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment