From 2dc0cda50d5ed11a1fb7baa79f6ded072b8e688d Mon Sep 17 00:00:00 2001 From: Erik Ogan Date: Tue, 31 Jan 2012 14:51:31 -0800 Subject: [PATCH] A first pass at 1.9.3: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 1.9.3’s Net::HTTP methods drop the second return value. Use response.body instead * Tweak CSV support, as 1.9.3’s CSV == FasterCSV --- Rakefile | 4 ++-- lib/contacts/aol.rb | 4 +++- lib/contacts/base.rb | 14 +++++++------- lib/contacts/hotmail.rb | 2 +- lib/contacts/yahoo.rb | 10 ++++++---- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/Rakefile b/Rakefile index 2b74dc8..2ecaaa8 100644 --- a/Rakefile +++ b/Rakefile @@ -4,7 +4,7 @@ require 'rake/testtask' require 'rake/rdoctask' require 'rake/gempackagetask' require 'rake/contrib/rubyforgepublisher' -require 'lib/contacts' +require './lib/contacts' PKG_VERSION = Contacts::VERSION @@ -88,4 +88,4 @@ task :stats do ["Library", "lib"], ["Units", "test"] ).to_s -end \ No newline at end of file +end diff --git a/lib/contacts/aol.rb b/lib/contacts/aol.rb index c64280b..64922e2 100644 --- a/lib/contacts/aol.rb +++ b/lib/contacts/aol.rb @@ -137,7 +137,9 @@ def contacts def parse(data, options={}) begin @contacts = [] - FasterCSV.parse(data) do |person| + # Ruby 1.9 CSV is FasterCSV + csv_class = (RUBY_VERSION.split(/\./)[1] == '8') ? FasterCSV : CSV + csv_class.parse(data) do |person| @contacts << ["#{person[0]} #{person[1]}", person[4]] if person[4] && !person[4].empty? end @contacts diff --git a/lib/contacts/base.rb b/lib/contacts/base.rb index 3089dc2..1cf3a95 100644 --- a/lib/contacts/base.rb +++ b/lib/contacts/base.rb @@ -35,7 +35,7 @@ def contacts(options = {}) if connected? url = URI.parse(contact_list_url) http = open_http(url) - resp, data = http.get("#{url.path}?#{url.query}", + resp = http.get("#{url.path}?#{url.query}", "Cookie" => @cookies ) @@ -43,7 +43,7 @@ def contacts(options = {}) raise ConnectionError, self.class.const_get(:PROTOCOL_ERROR) end - parse(data, options) + parse(resp.body, options) end end @@ -143,8 +143,8 @@ def post(url, postdata, cookies="", referer="") "Content-Type" => 'application/x-www-form-urlencoded' } http_header.reject!{|k, v| k == 'Accept-Encoding'} if skip_gzip? - resp, data = http.post(url.path, postdata, http_header) - data = uncompress(resp, data) + resp = http.post(url.path, postdata, http_header) + data = uncompress(resp) cookies = parse_cookies(resp.response['set-cookie'], cookies) forward = resp.response['Location'] forward ||= (data =~ / "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0", "Accept-Encoding" => "gzip", "Cookie" => cookies, "Referer" => referer ) - data = uncompress(resp, data) + data = uncompress(resp) cookies = parse_cookies(resp.response['set-cookie'], cookies) forward = resp.response['Location'] if (not forward.nil?) && URI.parse(forward).host.nil? @@ -172,7 +172,7 @@ def get(url, cookies="", referer="") return data, resp, cookies, forward end - def uncompress(resp, data) + def uncompress(resp, data = resp.body) case resp.response['content-encoding'] when 'gzip' gz = Zlib::GzipReader.new(StringIO.new(data)) diff --git a/lib/contacts/hotmail.rb b/lib/contacts/hotmail.rb index b3c41b4..7f3ae8b 100644 --- a/lib/contacts/hotmail.rb +++ b/lib/contacts/hotmail.rb @@ -74,7 +74,7 @@ def contacts(options = {}) go = false url = URI.parse(get_contact_list_url(index)) http = open_http(url) - resp, data = http.get(get_contact_list_url(index), "Cookie" => @cookies) + resp = http.get(get_contact_list_url(index), "Cookie" => @cookies) email_match_text_beginning = Regexp.escape("http://m.mail.live.com/?rru=compose&to=") email_match_text_end = Regexp.escape("&ru=") diff --git a/lib/contacts/yahoo.rb b/lib/contacts/yahoo.rb index 7f90f0e..31203a9 100644 --- a/lib/contacts/yahoo.rb +++ b/lib/contacts/yahoo.rb @@ -46,9 +46,10 @@ def contacts # first, get the addressbook site with the new crumb parameter url = URI.parse(address_book_url) http = open_http(url) - resp, data = http.get("#{url.path}?#{url.query}", + resp = http.get("#{url.path}?#{url.query}", "Cookie" => @cookies ) + data = resp.body if resp.code_type != Net::HTTPOK raise ConnectionError, self.class.const_get(:PROTOCOL_ERROR) @@ -59,11 +60,12 @@ def contacts # now proceed with the new ".crumb" parameter to get the csv data url = URI.parse(contact_list_url.sub("_crumb=crumb","_crumb=#{crumb}").sub("time", Time.now.to_f.to_s.sub(".","")[0...-2])) http = open_http(url) - resp, more_data = http.get("#{url.path}?#{url.query}", + resp = http.get("#{url.path}?#{url.query}", "Cookie" => @cookies, "X-Requested-With" => "XMLHttpRequest", "Referer" => address_book_url ) + more_data = resp.body if resp.code_type != Net::HTTPOK raise ConnectionError, self.class.const_get(:PROTOCOL_ERROR) @@ -79,7 +81,7 @@ def contacts # now proceed with the new ".crumb" parameter to get the csv data url = URI.parse(contact_list_url.sub("bucket=1","bucket=#{i+1}").sub("_crumb=crumb","_crumb=#{crumb}").sub("time", Time.now.to_f.to_s.sub(".","")[0...-2])) http = open_http(url) - resp, more_data = http.get("#{url.path}?#{url.query}", + resp = http.get("#{url.path}?#{url.query}", "Cookie" => @cookies, "X-Requested-With" => "XMLHttpRequest", "Referer" => address_book_url @@ -89,7 +91,7 @@ def contacts raise ConnectionError, self.class.const_get(:PROTOCOL_ERROR) end - parse more_data + parse resp.body end end