Sha256: 312a976a58bce27e1981521dbc9c176c910389f812171a89d030b68629f49a65

Contents?: true

Size: 1.37 KB

Versions: 34

Compression:

Stored size: 1.37 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

34 entries across 34 versions & 3 rubygems

Version Path
xero_gateway-2.7.0 lib/xero_gateway/http_encoding_helper.rb
xero_gateway-2.6.0 lib/xero_gateway/http_encoding_helper.rb
xero_gateway-2.5.0 lib/xero_gateway/http_encoding_helper.rb
xero_gateway-2.4.0 lib/xero_gateway/http_encoding_helper.rb
xero_gateway-2.3.0 lib/xero_gateway/http_encoding_helper.rb
xero_gateway-float-2.1.7 lib/xero_gateway/http_encoding_helper.rb
xero_gateway-float-2.1.6 lib/xero_gateway/http_encoding_helper.rb
xero_gateway-float-2.1.4 lib/xero_gateway/http_encoding_helper.rb
xero_gateway-float-2.1.3 lib/xero_gateway/http_encoding_helper.rb
xero_gateway-float-2.1.1 lib/xero_gateway/http_encoding_helper.rb
xero_gateway-float-2.0.18 lib/xero_gateway/http_encoding_helper.rb
xero_gateway-float-2.0.17 lib/xero_gateway/http_encoding_helper.rb
xero_gateway-float-2.0.16 lib/xero_gateway/http_encoding_helper.rb
xero_gateway-float-2.0.15 lib/xero_gateway/http_encoding_helper.rb
xero_gateway-2.1.0 lib/xero_gateway/http_encoding_helper.rb
xero_gateway-n8vision-2.0.20 lib/xero_gateway/http_encoding_helper.rb
xero_gateway-2.0.19 lib/xero_gateway/http_encoding_helper.rb
xero_gateway-2.0.18 lib/xero_gateway/http_encoding_helper.rb
xero_gateway-2.0.17 lib/xero_gateway/http_encoding_helper.rb
xero_gateway-2.0.16 lib/xero_gateway/http_encoding_helper.rb