Sha256: 901cd2273299d88dfce09490e688df2710b425a444e90965e11fda3e5c1ec02b

Contents?: true

Size: 1.29 KB

Versions: 10

Compression:

Stored size: 1.29 KB

Contents

#!/usr/bin/env ruby
$: << File.dirname(__FILE__)+'/../../lib'
require File.dirname(__FILE__)+'/rasterize'
require File.dirname(__FILE__)+'/../favicon'

require 'goliath'
require 'em-synchrony/em-http'
require 'postrank-uri'

#
# Aroundware: while the Rasterize API is processing, this uses http://is.gd to
# generate a shortened link, stuffing it in the header. Both requests happen
# simultaneously.
#
class ShortenURL
  include Goliath::Rack::BarrierAroundware
  SHORTENER_URL_BASE = 'http://is.gd/create.php'
  attr_accessor :shortened_url

  def pre_process
    target_url = PostRank::URI.clean(env.params['url'])
    shortener_request = EM::HttpRequest.new(SHORTENER_URL_BASE).aget(:query => { :format => 'simple', :url => target_url })
    enqueue :shortened_url, shortener_request
    return Goliath::Connection::AsyncResponse
  end

  def post_process
    if shortened_url
      headers['X-Shortened-URI'] = shortened_url.response
    end
    [status, headers, body]
  end
end

class RasterizeAndShorten < Rasterize
  use Goliath::Rack::Params
  use Favicon, File.expand_path(File.dirname(__FILE__)+"/../public/favicon.ico")
  use Goliath::Rack::Validation::RequestMethod, %w(GET)
  use Goliath::Rack::Validation::RequiredParam, {:key => 'url'}
  #
  use Goliath::Rack::BarrierAroundwareFactory, ShortenURL
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
goliath-1.0.7 examples/rasterize/rasterize_and_shorten.rb
goliath-1.0.6 examples/rasterize/rasterize_and_shorten.rb
goliath-1.0.5 examples/rasterize/rasterize_and_shorten.rb
goliath-1.0.4 examples/rasterize/rasterize_and_shorten.rb
goliath-1.0.3 examples/rasterize/rasterize_and_shorten.rb
goliath-1.0.2 examples/rasterize/rasterize_and_shorten.rb
goliath-1.0.1 examples/rasterize/rasterize_and_shorten.rb
goliath-1.0.0 examples/rasterize/rasterize_and_shorten.rb
goliath-1.0.0.beta.1 examples/rasterize/rasterize_and_shorten.rb
goliath-0.9.4 examples/rasterize/rasterize_and_shorten.rb