From 02aa6b77cbeb38b2c21dc792371cb83cd163082d Mon Sep 17 00:00:00 2001 From: Michal Prawda Date: Fri, 11 Feb 2011 04:04:52 +0100 Subject: [PATCH] WP webmail integration --- contacts.gemspec | 2 +- lib/contacts.rb | 1 + lib/contacts/wp.rb | 41 +++++++++++++++++++++++++++ test/example_accounts.yml | 18 +++++++++--- test/unit/wp_contact_importer_test.rb | 39 +++++++++++++++++++++++++ 5 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 lib/contacts/wp.rb create mode 100644 test/unit/wp_contact_importer_test.rb diff --git a/contacts.gemspec b/contacts.gemspec index 13ef4e7..837b026 100644 --- a/contacts.gemspec +++ b/contacts.gemspec @@ -8,7 +8,7 @@ Gem::Specification.new do |s| s.description = "A universal interface to grab contact list information from various providers including Yahoo, AOL, Gmail, Hotmail, and Plaxo." s.has_rdoc = false s.authors = ["Lucas Carlson"] - s.files = ["LICENSE", "Rakefile", "README", "examples/grab_contacts.rb", "lib/contacts.rb", "lib/contacts/base.rb", "lib/contacts/json_picker.rb", "lib/contacts/gmail.rb", "lib/contacts/aol.rb", "lib/contacts/hotmail.rb", "lib/contacts/plaxo.rb", "lib/contacts/yahoo.rb"] + s.files = ["LICENSE", "Rakefile", "README", "examples/grab_contacts.rb", "lib/contacts.rb", "lib/contacts/base.rb", "lib/contacts/json_picker.rb", "lib/contacts/gmail.rb", "lib/contacts/aol.rb", "lib/contacts/hotmail.rb", "lib/contacts/plaxo.rb", "lib/contacts/yahoo.rb", "lib/contacts/mailru.rb", "lib/contacts/wp.rb"] s.add_dependency("json", ">= 1.1.1") s.add_dependency('gdata', '>= 1.1.1') end diff --git a/lib/contacts.rb b/lib/contacts.rb index 974b4ff..52be9e5 100644 --- a/lib/contacts.rb +++ b/lib/contacts.rb @@ -10,3 +10,4 @@ require 'plaxo' require 'aol' require 'mailru' +require 'wp' diff --git a/lib/contacts/wp.rb b/lib/contacts/wp.rb new file mode 100644 index 0000000..0be42bb --- /dev/null +++ b/lib/contacts/wp.rb @@ -0,0 +1,41 @@ +require 'csv' +require 'mechanize' + +class Contacts + class WP < Base + + LOGIN_URL = "https://poczta.wp.pl" + AUTH_ERROR_URL = "http://profil.wp.pl/login_poczta.html" + ADDRESS_BOOK_URL = "http://ksiazka-adresowa.wp.pl/import-export.html" + + def real_connect + begin + @agent = Mechanize.new + page = @agent.get(LOGIN_URL) + form = page.forms.first + form.login_username = @login + form.login_password = @password + @page = form.submit + if page.uri.to_s == AUTH_ERROR_URL + raise AuthenticationError, "Username and password do not match" + end + end + end + + def contacts + page = @agent.get(ADDRESS_BOOK_URL) + form = page.forms.last + form.gr_id = 0 + form.program = 'gm' + con = CSV.parse(form.submit.body) + @contacts = [] + con[1..-1].each do |row| + @contacts << [row[0], row[1]] + end + @contacts + end + + end + + TYPES[:wp] = WP +end diff --git a/test/example_accounts.yml b/test/example_accounts.yml index ee39295..e7d9082 100644 --- a/test/example_accounts.yml +++ b/test/example_accounts.yml @@ -1,7 +1,7 @@ gmail: username: password: - contacts: + contacts: - name: "FirstName1 LastName1" email_address: "firstname1@example.com" @@ -11,7 +11,7 @@ gmail: yahoo: username: password: - contacts: + contacts: - name: "FirstName1 LastName1" email_address: "firstname1@example.com" @@ -21,7 +21,7 @@ yahoo: hotmail: username: password: - contacts: + contacts: - name: "FirstName1 LastName1" email_address: "firstname1@example.com" @@ -31,7 +31,7 @@ hotmail: aol: username: password: - contacts: + contacts: - name: "FirstName1 LastName1" email_address: "firstname1@example.com" @@ -48,3 +48,13 @@ mailru: - name: "FirstName2 LastName2" email_address: "firstname2@example.com" +wp: + username: + password: + contacts: + - + name: "FirstName1 LastName1" + email_address: "firstname1@example.com" + - + name: "FirstName2 LastName2" + email_address: "firstname2@example.com" diff --git a/test/unit/wp_contact_importer_test.rb b/test/unit/wp_contact_importer_test.rb new file mode 100644 index 0000000..de06b7e --- /dev/null +++ b/test/unit/wp_contact_importer_test.rb @@ -0,0 +1,39 @@ +dir = File.dirname(__FILE__) +require "#{dir}/../test_helper" +require 'contacts' + +class WPContactImporterTest < ContactImporterTestCase + def setup + super + @account = TestAccounts[:wp] + end + + def test_successful_login + Contacts.new(:wp, @account.username, @account.password) + end + + def test_importer_fails_with_invalid_password + assert_raise(Contacts::AuthenticationError) do + Contacts.new(:wp, @account.username, "wrong_password") + end + end + + def test_importer_fails_with_blank_password + assert_raise(Contacts::AuthenticationError) do + Contacts.new(:wp, @account.username, "") + end + end + + def test_importer_fails_with_blank_username + assert_raise(Contacts::AuthenticationError) do + Contacts.new(:wp, "", @account.password) + end + end + + def test_fetch_contacts + contacts = Contacts.new(:wp, @account.username, @account.password).contacts + @account.contacts.each do |contact| + assert contacts.include?(contact), "Could not find: #{contact.inspect} in #{contacts.inspect}" + end + end +end