Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Aroundware example: shorten link and put into header while simultaneo…

…usly rasterizing
  • Loading branch information...
commit 79ddc2bdabd6aedb9ac58814d9a04cedc709c415 1 parent 93725bd
Philip (flip) Kromer authored
Showing with 41 additions and 0 deletions.
  1. +1 −0  .gitignore
  2. +40 −0 examples/rasterize/rasterize_and_shorten.rb
View
1  .gitignore
@@ -16,3 +16,4 @@ _site
examples/log
examples/goliath.log*
examples/goliath.pid
+examples/rasterize/thumb
View
40 examples/rasterize/rasterize_and_shorten.rb
@@ -0,0 +1,40 @@
+#!/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 shortener_req
+ target_url = PostRank::URI.clean(env.params['url'])
+ EM::HttpRequest.new(SHORTENER_URL_BASE).aget(:query => { :format => 'simple', :url => target_url })
+ end
+
+ def pre_process
+ add :shortener, shortener_req
+ end
+
+ def post_process
+ if responses[:callback][:shortener]
+ headers['X-Shortened-URI'] = responses[:callback][: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

0 comments on commit 79ddc2b

Please sign in to comment.
Something went wrong with that request. Please try again.