Skip to content

Commit

Permalink
API change: #contacts method no longer returns nested arrays, but an …
Browse files Browse the repository at this point in the history
…array of Contacts::Contact instances
  • Loading branch information
mislav committed Aug 22, 2008
1 parent b099995 commit c55f2fa
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 22 deletions.
4 changes: 4 additions & 0 deletions Rakefile
Expand Up @@ -8,13 +8,15 @@ spec_glob = FileList['spec/**/*_spec.rb']

desc 'Run all specs in spec directory'
Spec::Rake::SpecTask.new(:spec) do |t|
t.libs = ['lib', 'spec']
t.spec_opts = ['--options', spec_opts]
t.spec_files = spec_glob
end

namespace :spec do
desc 'Analyze spec coverage with RCov'
Spec::Rake::SpecTask.new(:rcov) do |t|
t.libs = ['lib', 'spec']
t.spec_files = spec_glob
t.spec_opts = ['--options', spec_opts]
t.rcov = true
Expand All @@ -26,12 +28,14 @@ namespace :spec do

desc 'Print Specdoc for all specs'
Spec::Rake::SpecTask.new(:doc) do |t|
t.libs = ['lib', 'spec']
t.spec_opts = ['--format', 'specdoc', '--dry-run']
t.spec_files = spec_glob
end

desc 'Generate HTML report'
Spec::Rake::SpecTask.new(:html) do |t|
t.libs = ['lib', 'spec']
t.spec_opts = ['--format', 'html:doc/spec_results.html', '--diff']
t.spec_files = spec_glob
t.fail_on_error = false
Expand Down
23 changes: 19 additions & 4 deletions lib/contacts.rb
@@ -1,4 +1,23 @@
module Contacts
class Contact
attr_reader :name, :username, :emails

def initialize(email, name = nil, username = nil)
@emails = []
@emails << email if email
@name = name
@username = username
end

def email
@emails.first
end

def inspect
%!#<Contacts::Contact "#{name}" (#{email})>!
end
end

def self.verbose?
'irb' == $0
end
Expand All @@ -17,8 +36,4 @@ def initialize(response)
super "exceeded maximum of #{MAX_REDIRECTS} redirects (Location: #{location})"
end
end

class FetchingError < RuntimeError

end
end
12 changes: 5 additions & 7 deletions lib/contacts/google.rb
Expand Up @@ -167,7 +167,7 @@ def response_body(response)

def parse_contacts(body)
doc = Hpricot::XML body
entries = []
contacts_found = []

if updated_node = doc.at('/feed/updated')
@updated_string = updated_node.inner_text
Expand All @@ -179,15 +179,13 @@ def parse_contacts(body)
unless email_nodes.empty?
title_node = entry.at('/title')
name = title_node ? title_node.inner_text : nil

person = email_nodes.inject [name] do |p, e|
p << e['address'].to_s
end
entries << person
contact = Contact.new(nil, name)
contact.emails.concat email_nodes.map { |e| e['address'].to_s }
contacts_found << contact
end
end

entries
contacts_found
end

# Constructs a query string from a Hash object
Expand Down
38 changes: 38 additions & 0 deletions spec/contact_spec.rb
@@ -0,0 +1,38 @@
require 'spec_helper'
require 'contacts'

describe Contacts::Contact do
describe 'instance' do
before do
@contact = Contacts::Contact.new('max@example.com', 'Max Power', 'maxpower')
end

it "should have email" do
@contact.email.should == 'max@example.com'
end

it "should have name" do
@contact.name.should == 'Max Power'
end

it "should support multiple emails" do
@contact.emails << 'maxpower@example.com'
@contact.email.should == 'max@example.com'
@contact.emails.should == ['max@example.com', 'maxpower@example.com']
end

it "should have username" do
@contact.username.should == 'maxpower'
end

it "should have nice inspect" do
@contact.inspect.should == '#<Contacts::Contact "Max Power" (max@example.com)>'
end
end

it "should fail without first argument (email)" do
lambda {
Contacts::Contact.new()
}.should raise_error(ArgumentError)
end
end
2 changes: 1 addition & 1 deletion spec/gmail/auth_spec.rb
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/../spec_helper'
require 'spec_helper'
require 'contacts/google'
require 'uri'

Expand Down
21 changes: 14 additions & 7 deletions spec/gmail/fetching_spec.rb
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/../spec_helper'
require 'spec_helper'
require 'contacts/google'

describe Contacts::Google do
Expand Down Expand Up @@ -78,18 +78,25 @@
@gmail.stubs(:get)
@gmail.expects(:response_body).returns(sample_xml('google-single'))

@gmail.contacts.should == [['Fitzgerald', 'fubar@gmail.com']]
found = @gmail.contacts
found.size.should == 1
contact = found.first
contact.name.should == 'Fitzgerald'
contact.emails.should == ['fubar@gmail.com']
end

it 'parses a complex feed into name/email pairs' do
@gmail.stubs(:get)
@gmail.expects(:response_body).returns(sample_xml('google-many'))

@gmail.contacts.should == [
['Elizabeth Bennet', 'liz@gmail.com', 'liz@example.org'],
['William Paginate', 'will_paginate@googlegroups.com'],
[nil, 'anonymous@example.com']
]
found = @gmail.contacts
found.size.should == 3
found[0].name.should == 'Elizabeth Bennet'
found[0].emails.should == ['liz@gmail.com', 'liz@example.org']
found[1].name.should == 'William Paginate'
found[1].emails.should == ['will_paginate@googlegroups.com']
found[2].name.should be_nil
found[2].emails.should == ['anonymous@example.com']
end

it 'makes modification time available after parsing' do
Expand Down
3 changes: 0 additions & 3 deletions spec/spec_helper.rb
Expand Up @@ -5,9 +5,6 @@
gem 'mocha', '~> 0.9.0'
require 'mocha'

# add library's lib directory
$:.unshift File.dirname(__FILE__) + '/../lib'

module SampleFeeds
FEED_DIR = File.dirname(__FILE__) + '/feeds/'

Expand Down

0 comments on commit c55f2fa

Please sign in to comment.