Sha256: fea6289e0271e68db71cd8ac5c8b62b683def051296053b7178b53202ca04fbc

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

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

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 < Goliath::Synchrony::MultiReceiver
  SHORTENER_URL_BASE = 'http://is.gd/create.php'

  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 :shortener, shortener_request
  end

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

class RasterizeAndShorten < Rasterize
  use Goliath::Rack::Params
  use Goliath::Rack::Validation::RequestMethod, %w(GET)
  use Goliath::Rack::Validation::RequiredParam, {:key => 'url'}
  #
  use Goliath::Rack::AsyncAroundware, ShortenURL
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
goliath-0.9.2 examples/rasterize/rasterize_and_shorten.rb