Skip to content

Instantly share code, notes, and snippets.

@felixrabe
Created February 28, 2012 12:50
Show Gist options
  • Save felixrabe/1932335 to your computer and use it in GitHub Desktop.
Save felixrabe/1932335 to your computer and use it in GitHub Desktop.
Simplify Google search URL
#!/usr/bin/env ruby
require 'cgi'
require 'uri'
PREFIX = "https://www.google.com/search?q="
# Examples:
# http://www.google.ch/webhp?sourceid=chrome-instant&ix=sea&ie=UTF-8&ion=1#sclient=psy-ab&hl=de&site=webhp&source=hp&q=ruby%20parse%20url%20query&pbx=1&oq=&aq=&aqi=&aql=&gs_sm=&gs_upl=&fp=6c2025d738093ae5&ix=sea&ion=1&bav=on.2,or.r_gc.r_pw.,cf.osb&biw=1920&bih=1114
# => https://www.google.com/search?q=ruby+parse+url+query
# http://www.google.ch/search?q=twitter+bootstrap+1.4+2.0+guide
# => https://www.google.com/search?q=twitter+bootstrap+1.4+2.0+guide
u = ARGF.read
uri = URI.parse u
q = CGI.parse(uri.fragment)["q"][0] if uri.fragment
q ||= CGI.parse(uri.query)["q"][0]
print PREFIX + CGI.escape(q)
# Google: ruby decode url query
# http://snippets.dzone.com/posts/show/1260
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment