Net::HTTP Content-Encoding Helper
Created: 05Sep2007 [Permalink]
Last Updated: 04Nov2007
Ruby library to extend the standard Ruby's Net::HTTP library in order to enable handling encoded content (Content-Encoding), such as content that has been compressed by gzip or deflate.
RELEASE POST AND COMMENTS
Release post (and comments)
DOWNLOAD
http_encoding_helper.rb
USAGE
This library extends the Net::HTTPResponse class (such as HTTPOK), and adds a single method plain_body. Whereas the normal body method will return the encoded (compressed) content, plain_body will work out whether the content has been compressed with gzip or deflate, and will return the uncompressed body.
Used in conjunction with the Accept-Encoding header, this allows Ruby's Net::HTTP library to handle requesting compressed pages.
Confused? A code snippet will probably explain best.
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
puts "Transferred: #{response.body.length} bytes"
puts "Compression: #{response['content-encoding']}"
puts "Extracted: #{response.plain_body.length} bytes"
end
As this blog supports gzip encoding, you should get back something like :
Transferred: 7675 bytes
Compression: gzip
Extracted: 21168 bytes