From ea74004d4e85c3ccfcc09f33fa444a537a83a1c2 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 25 Jul 2011 22:09:21 -0400 Subject: [PATCH] working through 14.3 --- cookbook/014/03.rb | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 cookbook/014/03.rb diff --git a/cookbook/014/03.rb b/cookbook/014/03.rb new file mode 100644 index 0000000..a125699 --- /dev/null +++ b/cookbook/014/03.rb @@ -0,0 +1,64 @@ +require 'net/http' +require 'uri' + + +# A simple wrapper method that accepts either strings or URI objects +# and performs an HTTP GET. + +module Net + class HTTP + def HTTP.get_with_headers(uri, headers=nil) + uri = URI.parse(uri) if uri.respond_to? :to_str + start(uri.host, uri.port) do |http| + return http.get(uri.path, headers) + end + end + end +end + +# Let's get a web page in German. + +res = Net::HTTP.get_with_headers('http://www.google.com/', + {'Accept-Language' => 'de'}) + +# Check a bit of the body to make sure it's really in German +s = res.body.size +puts 'part of body in german=' + res.body[s-3670..s-3470].to_s + + +Net::HTTP.get_with_headers('http://www.google.com/', + {'User-Agent' => 'Ruby Web Browser v1.0'}) + + +uncompressed = Net::HTTP.get_with_headers('http://www.cnn.com/') +puts 'uncompressed body size=' + uncompressed.body.size.to_s + +gzipped = Net::HTTP.get_with_headers('http://www.cnn.com/', + {'Accept-Encoding' => 'gzip'}) +puts 'gzipped Content-Encoding=' + gzipped['Content-Encoding'] +puts 'gzipped body size=' + gzipped.body.size.to_s + +require 'zlib' +require 'stringio' + +body_io = StringIO.new(gzipped.body) +unzipped_body = Zlib::GzipReader.new(body_io).read() + +puts 'unzipped body size=' + unzipped_body.size.to_s + + +uri = URI.parse('http://www.google.com/') +request = Net::HTTP::Get.new(uri.path) +['en_us', 'en', 'en_gb', 'ja'].each do |language| + request.add_field('Accept-Language', language) +end +puts 'request[\'Accept-Language\']=' + request['Accept-Language'].to_s + +Net::HTTP.start(uri.host, uri.port) do |http| + response = http.request(request) + puts 'response[\'Content-Type\']=' + response['Content-Type'] + puts 'response headers:' + response.each_key do |key| + puts " #{key}" + end +end