Skip to content

Instantly share code, notes, and snippets.

@bscofield
Created May 18, 2009 15:01
Show Gist options
  • Save bscofield/113533 to your computer and use it in GitHub Desktop.
Save bscofield/113533 to your computer and use it in GitHub Desktop.
module Rack
class Embiggener
def initialize(app)
@app = app
end
def call(env)
status, headers, body = @app.call(env)
headers.delete('Content-Length')
response = Rack::Response.new(
Rack::Embiggener.embiggen_urls(body),
status,
headers
)
response.finish
return response.to_a
end
def self.embiggen_urls(original_body)

 new_body = []
original_body.each { |line|
tinys = line.scan(/(http:\/\/(?:www\.)?tinyurl\.com\/(.{6}))/)

 new_body << tinys.uniq.inject(line) do |body, tiny|

 original_tiny = tiny[0]

 preview_url = "http://preview.tinyurl.com/#{tiny[1]}"

 response = Net::HTTP.get_response(URI.parse(preview_url))

 embiggened_url = response.body.match(/redirecturl" href="(.*)">/)[1]


 body.gsub(original_tiny, embiggened_url)

 end

 }
new_body

 end
end

end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment