Skip to content

Instantly share code, notes, and snippets.

@ocean
Created June 16, 2009 09:09
Show Gist options
  • Save ocean/130611 to your computer and use it in GitHub Desktop.
Save ocean/130611 to your computer and use it in GitHub Desktop.
The below Sinatra code works with URLs like:
/c/blahblah
and
/c/http://blah.com/something.php
or
/c/google.com
But doesn't work with:
/c/http://google.com?id=47&q=blahblah
all you get shown is:
"http://google.com" in the above case.
How can I get all of the values given in the URL that I'm giving to the app ??
require 'rubygems'
require 'sinatra'
require 'haml'
set :haml, {:format => :html5 }
get '/' do
# haml "%p Hello whirled, it's: #{Time.now} on this server."
@foo = Time.now
haml :index
end
# straight grabbing what's after the slash - breaks if "http://" is present
get '/c/:url' do
haml "%p You entered the URL #{params[:url]}."
end
# grabbing using regex to strip out the protocol string
get %r{/c/(http:\/\/)(.*)} do
haml "%p You entered the URL #{params[:captures][1]}."
end
# grabbing what's after the slash with a * operator - still doesn't get parameterised URLs (something.com?id=47)
get '/c/*' do
haml "%p You entered the URL #{params[:splat].to_s}."
end
# straight to the about page
get '/about' do
haml :about
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment