bscofield (owner)

Revisions

gist: 113533 Download_button fork
public
Public Clone URL: git://gist.github.com/113533.git
Ruby
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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