Sha256: 4ea6c65ea37c8913ec8e292364c1cca0e3c91d7c4b32af45bd091a006b976de8

Contents?: true

Size: 999 Bytes

Versions: 1

Compression:

Stored size: 999 Bytes

Contents

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

# Install phantomjs: http://code.google.com/p/phantomjs/wiki/QuickStart
# $> ruby rasterize.rb -sv
# $> curl http://localhost:9000/?url=http://www.google.com (or rather, visit in the browser!)

class Rasterize < Goliath::API
  use Goliath::Rack::Params

  use Goliath::Rack::Validation::RequestMethod, %w(GET)
  use Goliath::Rack::Validation::RequiredParam, {:key => 'url'}

  def response(env)
    url = PostRank::URI.clean(params['url'])
    hash = PostRank::URI.hash(url, :clean => false)

    if !File.exists? filename(hash)
      fiber = Fiber.current
      EM.system('phantomjs rasterize.js ' + url.to_s + ' ' + filename(hash)) do |output, status|
        env.logger.info "Phantom exit status: #{status}"
        fiber.resume
      end

      Fiber.yield
    end

    [202, {'X-Phantom' => 'Goliath'}, IO.read(filename(hash))]
  end

  def filename(hash)
    "thumb/#{hash}.png"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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