Skip to content

Instantly share code, notes, and snippets.

@petewarden
Created October 25, 2012 03:28
Show Gist options
  • Save petewarden/3950261 to your computer and use it in GitHub Desktop.
Save petewarden/3950261 to your computer and use it in GitHub Desktop.
An example of proxying a Tumblr blog as a subdirectory in Ruby and Sinatra
get '/blog*' do
path = params[:splat][0]
source_url = 'http://yourblog.tumblr.com' + path.gsub(/ /, '+')
source_content_type = ''
source_body = open(source_url) do |f|
source_content_type = f.content_type # "text/html"
f.read
end
if source_content_type == 'text/html'
output_base = request.base_url + '/blog'
output_body = source_body.gsub(/\b(href|src|rel)="\/\//, '\1="https:\/\/')
output_body = output_body.gsub(/\b(href|src|rel)="\//, '\1="' + output_base + '/')
output_body = output_body.gsub(/\b(href|src|rel)="http:\/\/yourblog\.tumblr\.com/, '\1="/blog')
else
output_body = source_body
end
content_type source_content_type
output_body
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment