googlecontacts 0.1.8 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +9 -0
- data/LICENSE +1 -1
- data/Rakefile +4 -47
- data/googlecontacts.gemspec +23 -0
- data/lib/google_contacts.rb +13 -15
- data/lib/google_contacts/auth.rb +5 -5
- data/lib/google_contacts/base.rb +38 -30
- data/lib/google_contacts/contact.rb +16 -5
- data/lib/google_contacts/group.rb +4 -2
- data/lib/google_contacts/proxies/emails.rb +15 -8
- data/lib/google_contacts/proxies/hash.rb +2 -2
- data/lib/google_contacts/proxies/tag.rb +1 -1
- data/lib/google_contacts/version.rb +3 -0
- data/lib/google_contacts/wrapper.rb +16 -16
- data/lib/googlecontacts.rb +1 -1
- data/spec/base_spec.rb +35 -35
- data/spec/contact_spec.rb +30 -30
- data/spec/group_spec.rb +5 -5
- data/spec/proxies/array_spec.rb +19 -19
- data/spec/proxies/emails_spec.rb +54 -52
- data/spec/proxies/hash_spec.rb +14 -14
- data/spec/proxies/tag_spec.rb +9 -8
- data/spec/spec_helper.rb +8 -15
- data/spec/wrapper_spec.rb +25 -25
- metadata +43 -88
- data/VERSION +0 -1
- data/spec/spec.opts +0 -2
data/spec/proxies/emails_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe GoogleContacts::Proxies::Emails do
|
4
4
|
describe "with existing entries" do
|
@@ -14,21 +14,21 @@ describe GoogleContacts::Proxies::Emails do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should initialize on creation" do
|
17
|
-
@proxy[
|
18
|
-
@proxy[
|
19
|
-
@proxy[
|
20
|
-
@proxy[
|
21
|
-
@proxy[
|
17
|
+
@proxy["foo@bar.example.com"].address .should == "foo@bar.example.com"
|
18
|
+
@proxy["foo@bar.example.com"].name .should == "Foo Bar"
|
19
|
+
@proxy["foo@bar.example.com"].label .should == "Personal"
|
20
|
+
@proxy["foo@bar.example.com"].rel .should be_nil
|
21
|
+
@proxy["foo@bar.example.com"] .should_not be_primary
|
22
22
|
|
23
|
-
@proxy[
|
24
|
-
@proxy[
|
25
|
-
@proxy[
|
26
|
-
@proxy[
|
27
|
-
@proxy[
|
23
|
+
@proxy["fubar@gmail.com" ].address .should == "fubar@gmail.com"
|
24
|
+
@proxy["fubar@gmail.com" ].name .should be_nil
|
25
|
+
@proxy["fubar@gmail.com" ].label .should be_nil
|
26
|
+
@proxy["fubar@gmail.com" ].rel .should == "http://schemas.google.com/g/2005#home"
|
27
|
+
@proxy["fubar@gmail.com" ] .should be_primary
|
28
28
|
end
|
29
29
|
|
30
30
|
it "should be able to return the primary address" do
|
31
|
-
@proxy.primary.should == @proxy[
|
31
|
+
@proxy.primary.should == @proxy["fubar@gmail.com"]
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should initially be unchanged" do
|
@@ -41,23 +41,23 @@ describe GoogleContacts::Proxies::Emails do
|
|
41
41
|
end
|
42
42
|
|
43
43
|
it "when switching primary" do
|
44
|
-
@proxy[
|
44
|
+
@proxy["foo@bar.example.com"].primary!
|
45
45
|
end
|
46
46
|
|
47
47
|
it "when modifying name" do
|
48
|
-
@proxy[
|
48
|
+
@proxy["foo@bar.example.com"].name = "Quux"
|
49
49
|
end
|
50
50
|
|
51
51
|
it "when modifying rel" do
|
52
|
-
@proxy[
|
52
|
+
@proxy["foo@bar.example.com"].rel = "http://some.rel"
|
53
53
|
end
|
54
54
|
|
55
55
|
it "when adding a new address" do
|
56
|
-
@proxy <<
|
56
|
+
@proxy << "john@doe.com"
|
57
57
|
end
|
58
58
|
|
59
59
|
it "when removing an address" do
|
60
|
-
@proxy.delete
|
60
|
+
@proxy.delete "foo@bar.example.com"
|
61
61
|
end
|
62
62
|
end
|
63
63
|
end
|
@@ -72,59 +72,59 @@ describe GoogleContacts::Proxies::Emails do
|
|
72
72
|
|
73
73
|
it "should be possible to add email address" do
|
74
74
|
lambda {
|
75
|
-
@proxy[
|
75
|
+
@proxy["foo@bar.com"]
|
76
76
|
}.should change(@proxy, :size).by(1)
|
77
77
|
|
78
78
|
lambda {
|
79
|
-
@proxy <<
|
79
|
+
@proxy << "quux@bar.com"
|
80
80
|
}.should change(@proxy, :size).by(1)
|
81
81
|
end
|
82
82
|
|
83
83
|
it "should raise when adding a duplicate" do
|
84
|
-
@proxy <<
|
84
|
+
@proxy << "quux@bar.com"
|
85
85
|
lambda {
|
86
|
-
@proxy <<
|
86
|
+
@proxy << "quux@bar.com"
|
87
87
|
}.should raise_error
|
88
88
|
end
|
89
89
|
|
90
90
|
it "should provide sensible defaults for new addresses" do
|
91
|
-
@proxy[
|
92
|
-
@proxy[
|
93
|
-
@proxy[
|
91
|
+
@proxy["john@doe.com"].address.should == "john@doe.com"
|
92
|
+
@proxy["john@doe.com"].rel.should == "http://schemas.google.com/g/2005#home"
|
93
|
+
@proxy["john@doe.com"].label.should be_nil
|
94
94
|
end
|
95
95
|
|
96
96
|
it "should set the first created entry to be primary" do
|
97
|
-
@proxy[
|
97
|
+
@proxy["john@doe.com"].should be_primary
|
98
98
|
end
|
99
99
|
|
100
100
|
it "should only allow one entry to be primary" do
|
101
|
-
@proxy[
|
102
|
-
@proxy[
|
103
|
-
@proxy[
|
104
|
-
@proxy[
|
105
|
-
@proxy[
|
101
|
+
@proxy["john@doe.com"].should be_primary
|
102
|
+
@proxy["jane@doe.com"].should_not be_primary
|
103
|
+
@proxy["jane@doe.com"].primary!
|
104
|
+
@proxy["john@doe.com"].should_not be_primary
|
105
|
+
@proxy["jane@doe.com"].should be_primary
|
106
106
|
end
|
107
107
|
|
108
108
|
it "should only allow either rel or label to be set" do
|
109
|
-
@proxy[
|
110
|
-
@proxy[
|
111
|
-
@proxy[
|
109
|
+
@proxy["john@doe.com"].rel = "foo"
|
110
|
+
@proxy["john@doe.com"].label = "foo"
|
111
|
+
@proxy["john@doe.com"].rel.should be_nil
|
112
112
|
|
113
|
-
@proxy[
|
114
|
-
@proxy[
|
115
|
-
@proxy[
|
113
|
+
@proxy["john@doe.com"].label = "foo"
|
114
|
+
@proxy["john@doe.com"].rel = "foo"
|
115
|
+
@proxy["john@doe.com"].label.should be_nil
|
116
116
|
end
|
117
117
|
|
118
118
|
it "should raise when attempting to modify the address" do
|
119
119
|
lambda {
|
120
|
-
@proxy[
|
120
|
+
@proxy["john@doe.com"].address = "foo"
|
121
121
|
}.should raise_error(/cannot modify/i)
|
122
122
|
end
|
123
123
|
|
124
124
|
it "should allow email addresses to be removed" do
|
125
|
-
@proxy <<
|
125
|
+
@proxy << "john@doe.com"
|
126
126
|
lambda {
|
127
|
-
@proxy.delete(
|
127
|
+
@proxy.delete("john@doe.com")
|
128
128
|
}.should change(@proxy, :size).from(1).to(0)
|
129
129
|
end
|
130
130
|
end
|
@@ -138,28 +138,30 @@ describe GoogleContacts::Proxies::Emails do
|
|
138
138
|
end
|
139
139
|
|
140
140
|
it "should clear existing email tags" do
|
141
|
-
@proxy <<
|
142
|
-
@parent.
|
143
|
-
@parent.
|
141
|
+
@proxy << "john@doe.com"
|
142
|
+
@parent.should_receive(:remove_xml).with("./gd:email")
|
143
|
+
@parent.stub(:insert_xml)
|
144
144
|
@proxy.synchronize
|
145
145
|
end
|
146
146
|
|
147
147
|
it "should add every email address" do
|
148
|
-
@proxy <<
|
149
|
-
@proxy <<
|
150
|
-
|
151
|
-
@parent.
|
152
|
-
@parent.
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
148
|
+
@proxy << "john@doe.com"
|
149
|
+
@proxy << "jane@doe.com"
|
150
|
+
|
151
|
+
@parent.stub(:remove_xml)
|
152
|
+
@parent.should_receive(:insert_xml).
|
153
|
+
with("gd:email", include(
|
154
|
+
"address" => "john@doe.com",
|
155
|
+
"primary" => "true"))
|
156
|
+
@parent.should_receive(:insert_xml).
|
157
|
+
with("gd:email", include(
|
158
|
+
"address" => "jane@doe.com"))
|
157
159
|
@proxy.synchronize
|
158
160
|
end
|
159
161
|
end
|
160
162
|
|
161
163
|
def create_proxy_from_xml(str)
|
162
|
-
@parent = stub(
|
164
|
+
@parent = stub("parent", :xml => Nokogiri::XML.parse(str).root)
|
163
165
|
@proxy = GoogleContacts::Proxies::Emails.new(@parent)
|
164
166
|
end
|
165
167
|
end
|
data/spec/proxies/hash_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe GoogleContacts::Proxies::Hash do
|
4
4
|
describe "with existing entries" do
|
@@ -11,7 +11,7 @@ describe GoogleContacts::Proxies::Hash do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should initialize on creation" do
|
14
|
-
@proxy[:foo].should ==
|
14
|
+
@proxy[:foo].should == "bar"
|
15
15
|
@proxy.should have(1).entry
|
16
16
|
end
|
17
17
|
end
|
@@ -22,13 +22,13 @@ describe GoogleContacts::Proxies::Hash do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
it "should allow setting a value" do
|
25
|
-
@proxy[:foo] =
|
25
|
+
@proxy[:foo] = "bar"
|
26
26
|
@proxy.should have(1).entry
|
27
|
-
@proxy[:foo].should ==
|
27
|
+
@proxy[:foo].should == "bar"
|
28
28
|
end
|
29
29
|
|
30
30
|
it "should allow clearing" do
|
31
|
-
@proxy[:foo] =
|
31
|
+
@proxy[:foo] = "bar"
|
32
32
|
@proxy.clear
|
33
33
|
@proxy.should have(:no).entries
|
34
34
|
end
|
@@ -45,13 +45,13 @@ describe GoogleContacts::Proxies::Hash do
|
|
45
45
|
|
46
46
|
it "should work when a new value was set" do
|
47
47
|
lambda {
|
48
|
-
@proxy[:foo] =
|
48
|
+
@proxy[:foo] = "quux"
|
49
49
|
}.should change(@proxy, :changed?).from(false).to(true)
|
50
50
|
end
|
51
51
|
|
52
52
|
it "should not be changed when the new value equals the old one" do
|
53
53
|
lambda {
|
54
|
-
@proxy[:foo] =
|
54
|
+
@proxy[:foo] = "foo"
|
55
55
|
}.should_not change(@proxy, :changed?).from(false)
|
56
56
|
end
|
57
57
|
end
|
@@ -62,17 +62,17 @@ describe GoogleContacts::Proxies::Hash do
|
|
62
62
|
end
|
63
63
|
|
64
64
|
it "should update the group entries" do
|
65
|
-
@proxy[:foo] =
|
66
|
-
@proxy[:baz] =
|
67
|
-
@parent.
|
68
|
-
@parent.
|
69
|
-
@parent.
|
65
|
+
@proxy[:foo] = "quux"
|
66
|
+
@proxy[:baz] = "bar"
|
67
|
+
@parent.should_receive(:remove_xml).with("./prop")
|
68
|
+
@parent.should_receive(:insert_xml).with("prop", { "name" => "foo", "value" => "quux" })
|
69
|
+
@parent.should_receive(:insert_xml).with("prop", { "name" => "baz", "value" => "bar" })
|
70
70
|
@proxy.synchronize
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
74
|
def create_proxy_from_xml(str)
|
75
|
-
@parent = stub(
|
76
|
-
@proxy = GoogleContacts::Proxies::Hash.new(@parent, :tag =>
|
75
|
+
@parent = stub("parent", :xml => Nokogiri::XML.parse(str).root)
|
76
|
+
@proxy = GoogleContacts::Proxies::Hash.new(@parent, :tag => "prop", :key => "name", :value => "value")
|
77
77
|
end
|
78
78
|
end
|
data/spec/proxies/tag_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe GoogleContacts::Proxies::Tag do
|
4
4
|
describe "with existing entries" do
|
@@ -11,7 +11,7 @@ describe GoogleContacts::Proxies::Tag do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should initialize" do
|
14
|
-
@proxy.should ==
|
14
|
+
@proxy.should == "Example"
|
15
15
|
end
|
16
16
|
|
17
17
|
it "should not be changed when initialized" do
|
@@ -32,7 +32,7 @@ describe GoogleContacts::Proxies::Tag do
|
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should not create the tag when initializing" do
|
35
|
-
@parent.xml.xpath(
|
35
|
+
@parent.xml.xpath("./atom:title").should have(:no).entries
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should not be changed when initialized" do
|
@@ -55,8 +55,9 @@ describe GoogleContacts::Proxies::Tag do
|
|
55
55
|
end
|
56
56
|
|
57
57
|
it "should create the tag" do
|
58
|
-
@node =
|
59
|
-
@
|
58
|
+
@node = double("node")
|
59
|
+
@node.should_receive(:content=).with("Example")
|
60
|
+
@parent.should_receive(:insert_xml).with("atom:title").and_return(@node)
|
60
61
|
@proxy.replace("Example")
|
61
62
|
@proxy.synchronize
|
62
63
|
end
|
@@ -74,14 +75,14 @@ describe GoogleContacts::Proxies::Tag do
|
|
74
75
|
it "should update the tag" do
|
75
76
|
@proxy.replace("Replacement")
|
76
77
|
@proxy.synchronize
|
77
|
-
@parent.xml.at(
|
78
|
+
@parent.xml.at("./atom:title").content.should == "Replacement"
|
78
79
|
end
|
79
80
|
end
|
80
81
|
end
|
81
82
|
|
82
83
|
def create_proxy_from_xml(str)
|
83
|
-
@parent = stub(
|
84
|
-
@proxy = GoogleContacts::Proxies::Tag.new(@parent, :tag =>
|
84
|
+
@parent = stub("parent", :xml => Nokogiri::XML.parse(str).root)
|
85
|
+
@proxy = GoogleContacts::Proxies::Tag.new(@parent, :tag => "atom:title")
|
85
86
|
end
|
86
87
|
end
|
87
88
|
|
data/spec/spec_helper.rb
CHANGED
@@ -1,18 +1,10 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require 'spec'
|
5
|
-
require 'spec/autorun'
|
6
|
-
require 'pp'
|
7
|
-
|
8
|
-
require 'fakeweb'
|
9
|
-
FakeWeb.allow_net_connect = false
|
10
|
-
|
11
|
-
require 'google_contacts'
|
1
|
+
require "rspec"
|
2
|
+
require "fakeweb"
|
3
|
+
require "google_contacts"
|
12
4
|
|
13
5
|
module Helpers
|
14
6
|
def consumer
|
15
|
-
::OAuth::AccessToken.new(GoogleContacts::Auth.consumer,
|
7
|
+
::OAuth::AccessToken.new(GoogleContacts::Auth.consumer, "key", "secret")
|
16
8
|
end
|
17
9
|
|
18
10
|
def wrapper
|
@@ -28,7 +20,8 @@ module Helpers
|
|
28
20
|
end
|
29
21
|
end
|
30
22
|
|
31
|
-
|
32
|
-
|
33
|
-
|
23
|
+
FakeWeb.allow_net_connect = false
|
24
|
+
|
25
|
+
RSpec.configure do |config|
|
26
|
+
config.include Helpers
|
34
27
|
end
|
data/spec/wrapper_spec.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe GoogleContacts::Wrapper do
|
4
4
|
describe "fetching" do
|
5
5
|
before(:each) do
|
6
|
-
@options = {
|
6
|
+
@options = { "max-results" => 200, "start-index" => 1 }
|
7
7
|
end
|
8
8
|
|
9
9
|
def params(overrides)
|
10
10
|
@options.merge(overrides).map do |tuple|
|
11
|
-
tuple.join(
|
12
|
-
end.join(
|
11
|
+
tuple.join("=")
|
12
|
+
end.join("&")
|
13
13
|
end
|
14
14
|
|
15
15
|
def register(type, options = {})
|
@@ -18,27 +18,27 @@ describe GoogleContacts::Wrapper do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should be able to get list of contacts" do
|
21
|
-
register(:contacts) { asset(
|
21
|
+
register(:contacts) { asset("contacts_full") }
|
22
22
|
result = wrapper.contacts.find(:all)
|
23
23
|
result.should have(1).contact
|
24
24
|
result.first.should be_a GoogleContacts::Contact
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should be able to get list of contacts when result is paginated" do
|
28
|
-
register(:contacts,
|
29
|
-
register(:contacts,
|
28
|
+
register(:contacts, "start-index" => 1) { asset("contacts_full_page1") }
|
29
|
+
register(:contacts, "start-index" => 26) { asset("contacts_full_page2") }
|
30
30
|
result = wrapper.contacts.find(:all)
|
31
31
|
result.should have(2).contacts
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should be possible to specify the max-results parameter" do
|
35
|
-
register(:contacts,
|
36
|
-
result = wrapper.contacts.find(:all,
|
35
|
+
register(:contacts, "max-results" => 25) { asset("contacts_full") }
|
36
|
+
result = wrapper.contacts.find(:all, "max-results" => 25)
|
37
37
|
result.should have(1).contact
|
38
38
|
end
|
39
39
|
|
40
40
|
it "should be able to get list of groups" do
|
41
|
-
register(:groups) { asset(
|
41
|
+
register(:groups) { asset("groups_full") }
|
42
42
|
result = wrapper.groups.find(:all)
|
43
43
|
result.should have(2).groups
|
44
44
|
result.first.should be_a GoogleContacts::Group
|
@@ -53,48 +53,48 @@ describe GoogleContacts::Wrapper do
|
|
53
53
|
end
|
54
54
|
|
55
55
|
it "should collect operations in a batch" do
|
56
|
-
wrapper.
|
56
|
+
wrapper.should_not_receive(:post)
|
57
57
|
document = wrapper.batch(:return_documents => true) do
|
58
|
-
wrapper.contacts.build(:name =>
|
59
|
-
wrapper.contacts.build(:name =>
|
58
|
+
wrapper.contacts.build(:name => "c1").save
|
59
|
+
wrapper.contacts.build(:name => "c2").save
|
60
60
|
end.first
|
61
61
|
|
62
|
-
document.xpath(
|
63
|
-
document.xpath(
|
64
|
-
operation[
|
62
|
+
document.xpath(".//xmlns:entry").should have(2).entries
|
63
|
+
document.xpath(".//batch:operation").each do |operation|
|
64
|
+
operation["type"].should == "insert"
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
68
68
|
it "should flush batches in chunks of 100" do
|
69
|
-
wrapper.
|
69
|
+
wrapper.should_receive(:post).with(%r@/contacts/@, kind_of(String)).twice
|
70
70
|
wrapper.batch do
|
71
|
-
contact = wrapper.contacts.build(:name =>
|
71
|
+
contact = wrapper.contacts.build(:name => "contact")
|
72
72
|
101.times { contact.save }
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
76
|
it "should not flush when there are no operations to execute" do
|
77
|
-
wrapper.
|
77
|
+
wrapper.should_not_receive(:post)
|
78
78
|
wrapper.batch {}
|
79
79
|
end
|
80
80
|
|
81
81
|
it "should raise when mixing contacts and groups in one batch" do
|
82
82
|
lambda {
|
83
83
|
wrapper.batch {
|
84
|
-
wrapper.contacts.build(:name =>
|
85
|
-
wrapper.groups.build(:name =>
|
84
|
+
wrapper.contacts.build(:name => "contact").save
|
85
|
+
wrapper.groups.build(:name => "group").save
|
86
86
|
}
|
87
87
|
}.should raise_error(/cannot mix/i)
|
88
88
|
end
|
89
89
|
|
90
90
|
it "should POST a single-operation batch to contacts when not batching" do
|
91
|
-
wrapper.
|
92
|
-
wrapper.contacts.build(:name =>
|
91
|
+
wrapper.should_receive(:post).with(%r@/contacts/@, kind_of(String))
|
92
|
+
wrapper.contacts.build(:name => "contact").save
|
93
93
|
end
|
94
94
|
|
95
95
|
it "should POST a single-operation batch to groups when not batching" do
|
96
|
-
wrapper.
|
97
|
-
wrapper.groups.build(:name =>
|
96
|
+
wrapper.should_receive(:post).with(%r@/groups/@, kind_of(String))
|
97
|
+
wrapper.groups.build(:name => "group").save
|
98
98
|
end
|
99
99
|
end
|
100
100
|
end
|