Sha256: 1cd6a2b57d92493077d0422591dee1011edccc54d5ebd2a5aefce661460cc6d9

Contents?: true

Size: 1.32 KB

Versions: 61

Compression:

Stored size: 1.32 KB

Contents

# Intended to extend the Net::HTTP response object
# and adds support for decoding gzip and deflate encoded pages
#
# Author: Jason Stirk <http://griffin.oobleyboo.com>
# Home: http://griffin.oobleyboo.com/projects/http_encoding_helper
# Created: 5 September 2007
# Last Updated: 23 November 2007
#
# Usage:
#
# require 'net/http'
# require 'http_encoding_helper'
# headers={'Accept-Encoding' => 'gzip, deflate' }
# http = Net::HTTP.new('griffin.oobleyboo.com', 80)
# http.start do |h|
#   request = Net::HTTP::Get.new('/', headers)
#   response = http.request(request)
#   content=response.plain_body     # Method from our library
#   puts "Transferred: #{response.body.length} bytes"
#   puts "Compression: #{response['content-encoding']}"
#   puts "Extracted: #{response.plain_body.length} bytes"  
# end
#	

require 'zlib'
require 'stringio'

class Net::HTTPResponse
  # Return the uncompressed content
  def plain_body
    encoding=self['content-encoding']
    content=nil
    if encoding then
      case encoding
        when 'gzip'
          i=Zlib::GzipReader.new(StringIO.new(self.body))
          content=i.read
        when 'deflate'
          i=Zlib::Inflate.new
          content=i.inflate(self.body)
        else
          raise "Unknown encoding - #{encoding}"
      end
    else
      content=self.body
    end
    return content
  end
end

Version data entries

61 entries across 61 versions & 4 rubygems

Version Path
xeroizer-3.0.1 lib/xeroizer/http_encoding_helper.rb
xeroizer-3.0.0 lib/xeroizer/http_encoding_helper.rb
xeroizer-3-pre-beta-3.0.0.pre.beta lib/xeroizer/http_encoding_helper.rb
xeroizer-2.20.0 lib/xeroizer/http_encoding_helper.rb
xeroizer-2.19.0 lib/xeroizer/http_encoding_helper.rb
xeroizer-2.18.1 lib/xeroizer/http_encoding_helper.rb
xeroizer-2.17.1 lib/xeroizer/http_encoding_helper.rb
xeroizer-2.16.5 lib/xeroizer/http_encoding_helper.rb
xeroizer-2.16.4 lib/xeroizer/http_encoding_helper.rb
xeroizer-2.16.3 lib/xeroizer/http_encoding_helper.rb
xeroizer-2.16.1 lib/xeroizer/http_encoding_helper.rb
xeroizer-2.16.0 lib/xeroizer/http_encoding_helper.rb
xeroizer-2.15.9 lib/xeroizer/http_encoding_helper.rb
xeroizer-2.15.8 lib/xeroizer/http_encoding_helper.rb
xeroizer-2.15.7 lib/xeroizer/http_encoding_helper.rb
xeroizer-2.15.6 lib/xeroizer/http_encoding_helper.rb
quickbooks-ruby-0.0.2 lib/quickbooks/http_encoding_helper.rb
quickbooks-ruby-0.0.1 lib/quickbooks/http_encoding_helper.rb
xeroizer-float-2.15.5.2 lib/xeroizer/http_encoding_helper.rb
xeroizer-float-2.15.5.1 lib/xeroizer/http_encoding_helper.rb