Sha256: 7bc6eec66e3f813c0e9f95d8c4b8931792d6040ced2e6ddde286c084b26c2c06

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

#!/usr/bin/env ruby
$:<< '../lib' << 'lib'

#
# Example of using the rack/deflater middleware to automatically GZIP your response
# if the client indicated that it will accept gzipped data.
#
# Note that we're also using Rack::Rewrite to alter incoming request prior to it
# being parsed by the Rack::Params middleware. This allows us to transparently
# rewrite incoming requests before any processing is done on it.
#
# curl -s -H "Accept-Encoding: gzip,deflate" -H "Connection: close" localhost:9000?gziped=test | gunzip
#

require 'rack/deflater'
require 'rack/rewrite'
require 'goliath'
require 'yajl'

class Gziped < Goliath::API
  # if client requested, compress the response
  use ::Rack::Deflater

  # example of using rack rewriting to rewrite the param gziped to echo
  use ::Rack::Rewrite do
    rewrite %r{^(.*?)\??gziped=(.*)$}, lambda { |match, env| "#{match[1]}?echo=#{match[2]}" }
  end

  use Goliath::Rack::Params             # parse & merge query and body parameters
  use Goliath::Rack::Render, 'json'     # auto-negotiate response format

  use Goliath::Rack::Validation::RequestMethod, %w(GET)           # allow GET requests only
  use Goliath::Rack::Validation::RequiredParam, {:key => 'echo'}  # must provide ?echo= query or body param

  def response(env)
    [200, {}, {response: env.params['echo']}]
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
goliath-0.9.4 examples/gziped.rb
goliath-0.9.2 examples/gziped.rb