Created
October 25, 2012 03:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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