google_contacts_api 0.3.3 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.travis.yml +4 -5
- data/Gemfile +1 -1
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/google_contacts_api.gemspec +6 -6
- data/lib/google_contacts_api/api.rb +13 -2
- data/lib/google_contacts_api/contact.rb +71 -0
- data/spec/google_contacts_api_spec.rb +206 -74
- data/spec/spec_helper.rb +0 -1
- metadata +8 -9
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/Rakefile
CHANGED
@@ -16,7 +16,7 @@ Jeweler::Tasks.new do |gem|
|
|
16
16
|
gem.homepage = "http://github.com/aliang/google_contacts_api"
|
17
17
|
gem.license = "MIT"
|
18
18
|
gem.summary = %Q{Lets you read from the Google Contacts API}
|
19
|
-
gem.description = %Q{Lets you read from the Google Contacts API. Posting to come later.
|
19
|
+
gem.description = %Q{Lets you read from the Google Contacts API. Posting to come later.}
|
20
20
|
gem.email = "ayliang@gmail.com"
|
21
21
|
gem.authors = ["Alvin Liang"]
|
22
22
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/google_contacts_api.gemspec
CHANGED
@@ -5,12 +5,12 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "google_contacts_api"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Alvin Liang"]
|
12
|
-
s.date = "
|
13
|
-
s.description = "Lets you read from the Google Contacts API. Posting to come later.
|
12
|
+
s.date = "2014-08-21"
|
13
|
+
s.description = "Lets you read from the Google Contacts API. Posting to come later."
|
14
14
|
s.email = "ayliang@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE.txt",
|
@@ -58,7 +58,7 @@ Gem::Specification.new do |s|
|
|
58
58
|
s.add_runtime_dependency(%q<hashie>, [">= 0"])
|
59
59
|
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
60
60
|
s.add_development_dependency(%q<simplecov>, [">= 0"])
|
61
|
-
s.add_development_dependency(%q<rspec>, ["
|
61
|
+
s.add_development_dependency(%q<rspec>, ["~> 3.0"])
|
62
62
|
s.add_development_dependency(%q<rdoc>, [">= 0"])
|
63
63
|
s.add_development_dependency(%q<bundler>, [">= 0"])
|
64
64
|
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
@@ -70,7 +70,7 @@ Gem::Specification.new do |s|
|
|
70
70
|
s.add_dependency(%q<hashie>, [">= 0"])
|
71
71
|
s.add_dependency(%q<shoulda>, [">= 0"])
|
72
72
|
s.add_dependency(%q<simplecov>, [">= 0"])
|
73
|
-
s.add_dependency(%q<rspec>, ["
|
73
|
+
s.add_dependency(%q<rspec>, ["~> 3.0"])
|
74
74
|
s.add_dependency(%q<rdoc>, [">= 0"])
|
75
75
|
s.add_dependency(%q<bundler>, [">= 0"])
|
76
76
|
s.add_dependency(%q<jeweler>, [">= 0"])
|
@@ -83,7 +83,7 @@ Gem::Specification.new do |s|
|
|
83
83
|
s.add_dependency(%q<hashie>, [">= 0"])
|
84
84
|
s.add_dependency(%q<shoulda>, [">= 0"])
|
85
85
|
s.add_dependency(%q<simplecov>, [">= 0"])
|
86
|
-
s.add_dependency(%q<rspec>, ["
|
86
|
+
s.add_dependency(%q<rspec>, ["~> 3.0"])
|
87
87
|
s.add_dependency(%q<rdoc>, [">= 0"])
|
88
88
|
s.add_dependency(%q<bundler>, [">= 0"])
|
89
89
|
s.add_dependency(%q<jeweler>, [">= 0"])
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'active_support'
|
1
2
|
require 'active_support/core_ext'
|
2
3
|
|
3
4
|
module GoogleContactsApi
|
@@ -20,9 +21,9 @@ module GoogleContactsApi
|
|
20
21
|
# ":" is replaced with $, element content is keyed with $t
|
21
22
|
# Raise UnauthorizedError if not authorized.
|
22
23
|
def get(link, params = {}, headers = {})
|
23
|
-
|
24
|
+
merged_params = params_with_defaults(params)
|
24
25
|
begin
|
25
|
-
result = @oauth.get("#{BASE_URL}#{link}?#{
|
26
|
+
result = @oauth.get("#{BASE_URL}#{link}?#{merged_params.to_query}", headers)
|
26
27
|
rescue => e
|
27
28
|
# TODO: OAuth 2.0 will raise a real error
|
28
29
|
raise UnauthorizedError if defined?(e.response) && self.class.parse_response_code(e.response) == 401
|
@@ -63,5 +64,15 @@ module GoogleContactsApi
|
|
63
64
|
def self.parse_response_code(response)
|
64
65
|
(defined?(response.code) ? response.code : response.status).to_i
|
65
66
|
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def params_with_defaults(params)
|
71
|
+
p = params.merge({
|
72
|
+
"alt" => "json"
|
73
|
+
})
|
74
|
+
p['v'] = '3' unless p['v']
|
75
|
+
p
|
76
|
+
end
|
66
77
|
end
|
67
78
|
end
|
@@ -83,5 +83,76 @@ module GoogleContactsApi
|
|
83
83
|
def ims
|
84
84
|
self["gd$im"] ? self["gd$im"].map { |i| i.address } : []
|
85
85
|
end
|
86
|
+
|
87
|
+
def nested_t_field_or_nil(level1, level2)
|
88
|
+
if self[level1]
|
89
|
+
self[level1][level2] ? self[level1][level2]['$t']: nil
|
90
|
+
end
|
91
|
+
end
|
92
|
+
def given_name
|
93
|
+
nested_t_field_or_nil 'gd$name', 'gd$givenName'
|
94
|
+
end
|
95
|
+
def family_name
|
96
|
+
nested_t_field_or_nil 'gd$name', 'gd$familyName'
|
97
|
+
end
|
98
|
+
def full_name
|
99
|
+
nested_t_field_or_nil 'gd$name', 'gd$fullName'
|
100
|
+
end
|
101
|
+
def additional_name
|
102
|
+
nested_t_field_or_nil 'gd$name', 'gd$additionalName'
|
103
|
+
end
|
104
|
+
def name_prefix
|
105
|
+
nested_t_field_or_nil 'gd$name', 'gd$namePrefix'
|
106
|
+
end
|
107
|
+
def name_suffix
|
108
|
+
nested_t_field_or_nil 'gd$name', 'gd$nameSuffix'
|
109
|
+
end
|
110
|
+
|
111
|
+
def relations
|
112
|
+
self['gContact$relation'] ? self['gContact$relation'] : []
|
113
|
+
end
|
114
|
+
def spouse
|
115
|
+
spouse_rel = relations.find {|r| r.rel = 'spouse'}
|
116
|
+
spouse_rel['$t'] if spouse_rel
|
117
|
+
end
|
118
|
+
|
119
|
+
def addresses
|
120
|
+
self['gd$structuredPostalAddress'] ? self['gd$structuredPostalAddress'].map(&method(:format_address)) : []
|
121
|
+
end
|
122
|
+
def phone_numbers_full
|
123
|
+
self["gd$phoneNumber"] ? self["gd$phoneNumber"].map(&method(:format_phone_number)) : []
|
124
|
+
end
|
125
|
+
def emails_full
|
126
|
+
self["gd$email"] ? self["gd$email"].map(&method(:format_email)) : []
|
127
|
+
end
|
128
|
+
|
129
|
+
private
|
130
|
+
def format_address(unformatted)
|
131
|
+
formatted = {}
|
132
|
+
formatted[:rel] = unformatted['rel'] ? unformatted['rel'].gsub('http://schemas.google.com/g/2005#', '') : 'work'
|
133
|
+
unformatted.delete 'rel'
|
134
|
+
unformatted.each do |key, value|
|
135
|
+
formatted[key.sub('gd$', '').underscore.to_sym] = value['$t']
|
136
|
+
end
|
137
|
+
formatted
|
138
|
+
end
|
139
|
+
|
140
|
+
def format_email_or_phone(unformatted)
|
141
|
+
formatted = {}
|
142
|
+
unformatted.each do |key, value|
|
143
|
+
formatted[key.underscore.to_sym] = value ? value.gsub('http://schemas.google.com/g/2005#', '') : value
|
144
|
+
end
|
145
|
+
formatted[:primary] = unformatted['primary'] ? unformatted['primary'] == 'true' : false
|
146
|
+
formatted
|
147
|
+
end
|
148
|
+
|
149
|
+
def format_phone_number(unformatted)
|
150
|
+
unformatted[:number] = unformatted['$t']
|
151
|
+
unformatted.delete '$t'
|
152
|
+
format_email_or_phone unformatted
|
153
|
+
end
|
154
|
+
def format_email(unformatted)
|
155
|
+
format_email_or_phone unformatted
|
156
|
+
end
|
86
157
|
end
|
87
158
|
end
|
@@ -12,18 +12,29 @@ describe "GoogleContactsApi" do
|
|
12
12
|
describe "Api" do
|
13
13
|
before(:each) do
|
14
14
|
@oauth = double("oauth")
|
15
|
-
@oauth.
|
15
|
+
allow(@oauth).to receive(:get).and_return("get response")
|
16
16
|
@api = GoogleContactsApi::Api.new(@oauth)
|
17
17
|
end
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
19
|
+
describe ".get" do
|
20
|
+
it "should perform a get request using oauth returning json with version 3" do
|
21
|
+
# expectation should come before execution
|
22
|
+
expect(@oauth).to receive(:get).with(
|
23
|
+
GoogleContactsApi::Api::BASE_URL + "any_url?alt=json¶m=param&v=3", {"header" => "header"})
|
24
|
+
expect(@api.get("any_url",
|
25
|
+
{"param" => "param"},
|
26
|
+
{"header" => "header"})).to eq("get response")
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should perform a get request using oauth with the version specified" do
|
30
|
+
expect(@oauth).to receive(:get).with(
|
31
|
+
GoogleContactsApi::Api::BASE_URL + "any_url?alt=json¶m=param&v=2", {"header" => "header"})
|
32
|
+
expect(@api.get("any_url",
|
33
|
+
{"param" => "param", "v" => "2"},
|
34
|
+
{"header" => "header"})).to eq("get response")
|
35
|
+
end
|
26
36
|
end
|
37
|
+
|
27
38
|
# Not implemented yet
|
28
39
|
pending "should perform a post request using oauth"
|
29
40
|
pending "should perform a put request using oauth"
|
@@ -32,21 +43,21 @@ describe "GoogleContactsApi" do
|
|
32
43
|
it "should raise UnauthorizedError if OAuth 1.0 returns unauthorized" do
|
33
44
|
oauth = double("oauth")
|
34
45
|
error_html = load_file(File.join('errors', 'auth_sub_401.html'))
|
35
|
-
oauth.
|
46
|
+
allow(oauth).to receive(:get).and_return(Net::HTTPUnauthorized.new("1.1", 401, error_html))
|
36
47
|
api = GoogleContactsApi::Api.new(oauth)
|
37
|
-
|
48
|
+
expect { api.get("any url",
|
38
49
|
{"param" => "param"},
|
39
|
-
{"header" => "header"}) }.
|
50
|
+
{"header" => "header"}) }.to raise_error(GoogleContactsApi::UnauthorizedError)
|
40
51
|
end
|
41
52
|
|
42
53
|
it "should raise UnauthorizedError if OAuth 2.0 returns unauthorized" do
|
43
54
|
oauth = double("oauth2")
|
44
55
|
oauth2_response = Struct.new(:status)
|
45
|
-
oauth.
|
56
|
+
allow(oauth).to receive(:get).and_raise(MockOAuth2Error.new(oauth2_response.new(401)))
|
46
57
|
api = GoogleContactsApi::Api.new(oauth)
|
47
|
-
|
58
|
+
expect { api.get("any url",
|
48
59
|
{"param" => "param"},
|
49
|
-
{"header" => "header"}) }.
|
60
|
+
{"header" => "header"}) }.to raise_error(GoogleContactsApi::UnauthorizedError)
|
50
61
|
end
|
51
62
|
|
52
63
|
describe "parsing response code" do
|
@@ -55,41 +66,41 @@ describe "GoogleContactsApi" do
|
|
55
66
|
@Oauth2 = Struct.new(:status)
|
56
67
|
end
|
57
68
|
it "should parse something that looks like an oauth gem response" do
|
58
|
-
GoogleContactsApi::Api.parse_response_code(@Oauth.new("401")).
|
69
|
+
expect(GoogleContactsApi::Api.parse_response_code(@Oauth.new("401"))).to eq(401)
|
59
70
|
end
|
60
|
-
|
71
|
+
|
61
72
|
it "should parse something that looks like an oauth2 gem response" do
|
62
|
-
GoogleContactsApi::Api.parse_response_code(@Oauth2.new(401)).
|
73
|
+
expect(GoogleContactsApi::Api.parse_response_code(@Oauth2.new(401))).to eq(401)
|
63
74
|
end
|
64
75
|
end
|
65
76
|
end
|
66
|
-
|
77
|
+
|
67
78
|
describe "User" do
|
68
79
|
before(:each) do
|
69
80
|
@oauth = double("oauth")
|
70
81
|
@user = GoogleContactsApi::User.new(@oauth)
|
71
|
-
@user.api.
|
82
|
+
allow(@user.api).to receive(:get).and_return(Hashie::Mash.new({
|
72
83
|
"body" => "some response", # could use example response here
|
73
84
|
"code" => 200
|
74
85
|
}))
|
75
|
-
GoogleContactsApi::GroupSet.
|
76
|
-
GoogleContactsApi::ContactSet.
|
86
|
+
allow(GoogleContactsApi::GroupSet).to receive(:new).and_return("group set")
|
87
|
+
allow(GoogleContactsApi::ContactSet).to receive(:new).and_return("contact set")
|
77
88
|
end
|
78
89
|
# Should hit the right URLs and return the right stuff
|
79
90
|
it "should be able to get groups including system groups" do
|
80
|
-
@user.api.
|
81
|
-
@user.groups.
|
91
|
+
expect(@user.api).to receive(:get).with("groups/default/full", hash_including(:v => 2))
|
92
|
+
expect(@user.groups).to eq("group set")
|
82
93
|
end
|
83
94
|
it "should be able to get contacts" do
|
84
|
-
@user.api.
|
85
|
-
@user.contacts.
|
95
|
+
expect(@user.api).to receive(:get).with("contacts/default/full", anything)
|
96
|
+
expect(@user.contacts).to eq("contact set")
|
86
97
|
end
|
87
98
|
end
|
88
|
-
|
99
|
+
|
89
100
|
describe "ResultSet" do
|
90
101
|
# no testing, it's just an implementation detail to use inheritance
|
91
102
|
end
|
92
|
-
|
103
|
+
|
93
104
|
describe "ContactSet" do
|
94
105
|
describe "with entries" do
|
95
106
|
before(:all) do
|
@@ -98,31 +109,31 @@ describe "GoogleContactsApi" do
|
|
98
109
|
end
|
99
110
|
|
100
111
|
it "should return the right starting index" do
|
101
|
-
@contact_set.start_index.
|
112
|
+
expect(@contact_set.start_index).to eq(1)
|
102
113
|
end
|
103
114
|
it "should return the right number of results per page" do
|
104
|
-
@contact_set.items_per_page.
|
115
|
+
expect(@contact_set.items_per_page).to eq(25)
|
105
116
|
end
|
106
117
|
it "should return the right number of total results" do
|
107
|
-
@contact_set.total_results.
|
118
|
+
expect(@contact_set.total_results).to eq(500)
|
108
119
|
end
|
109
120
|
it "should tell me if there are more results" do
|
110
121
|
# yeah this is an awkward assertion and matcher
|
111
|
-
@contact_set.
|
112
|
-
@contact_set.has_more
|
122
|
+
expect(@contact_set).to be_has_more
|
123
|
+
expect(@contact_set.has_more?).to eq(true)
|
113
124
|
end
|
114
125
|
it "should parse results into Contacts" do
|
115
|
-
@contact_set.to_a.first.
|
126
|
+
expect(@contact_set.to_a.first).to be_instance_of(GoogleContactsApi::Contact)
|
116
127
|
end
|
117
128
|
end
|
118
129
|
it "should parse nil results into an empty array" do
|
119
130
|
@empty_contact_set_json = empty_contact_set_json
|
120
131
|
@empty_contact_set = GoogleContactsApi::ContactSet.new(@empty_contact_set_json)
|
121
|
-
@empty_contact_set.total_results.
|
122
|
-
@empty_contact_set.instance_variable_get("@results").
|
132
|
+
expect(@empty_contact_set.total_results).to eq(0)
|
133
|
+
expect(@empty_contact_set.instance_variable_get("@results")).to eq([])
|
123
134
|
end
|
124
135
|
end
|
125
|
-
|
136
|
+
|
126
137
|
describe "GroupSet" do
|
127
138
|
before(:all) do
|
128
139
|
@group_set_json = group_set_json
|
@@ -130,28 +141,28 @@ describe "GoogleContactsApi" do
|
|
130
141
|
end
|
131
142
|
|
132
143
|
it "should return the right starting index" do
|
133
|
-
@group_set.start_index.
|
144
|
+
expect(@group_set.start_index).to eq(1)
|
134
145
|
end
|
135
146
|
it "should return the right number of results per page" do
|
136
|
-
@group_set.items_per_page.
|
147
|
+
expect(@group_set.items_per_page).to eq(25)
|
137
148
|
end
|
138
149
|
it "should return the right number of total results" do
|
139
|
-
@group_set.total_results.
|
150
|
+
expect(@group_set.total_results).to eq(5)
|
140
151
|
end
|
141
152
|
it "should tell me if there are more results" do
|
142
153
|
# yeah this is an awkward assertion and matcher
|
143
|
-
@group_set.
|
144
|
-
@group_set.has_more
|
154
|
+
expect(@group_set).not_to be_has_more
|
155
|
+
expect(@group_set.has_more?).to eq(false)
|
145
156
|
end
|
146
157
|
it "should parse results into Groups" do
|
147
|
-
@group_set.to_a.first.
|
158
|
+
expect(@group_set.to_a.first).to be_instance_of(GoogleContactsApi::Group)
|
148
159
|
end
|
149
160
|
end
|
150
|
-
|
161
|
+
|
151
162
|
describe "Result" do
|
152
163
|
# no testing, it's just an implementation detail to inherit
|
153
164
|
end
|
154
|
-
|
165
|
+
|
155
166
|
describe "Contact" do
|
156
167
|
before(:all) do
|
157
168
|
@contact_json_hash = contact_json_hash
|
@@ -159,72 +170,193 @@ describe "GoogleContactsApi" do
|
|
159
170
|
end
|
160
171
|
# ok, these tests are kind of silly
|
161
172
|
it "should return the right title" do
|
162
|
-
@contact.title.
|
173
|
+
expect(@contact.title).to eq("Contact 1")
|
163
174
|
end
|
164
175
|
it "should return the right id" do
|
165
|
-
@contact.id.
|
176
|
+
expect(@contact.id).to eq("http://www.google.com/m8/feeds/contacts/example%40gmail.com/base/0")
|
166
177
|
end
|
167
178
|
it "should return the right content" do
|
168
179
|
# TODO: Nothing in source, oops
|
169
|
-
@contact.content.
|
180
|
+
expect(@contact.content).to eq(nil)
|
170
181
|
end
|
171
182
|
it "should return the right updated time" do
|
172
183
|
# different representation slightly
|
173
|
-
@contact.updated.to_s.
|
184
|
+
expect(@contact.updated.to_s).to eq("2011-07-07T21:02:42+00:00")
|
174
185
|
end
|
175
186
|
it "should return the right self link" do
|
176
|
-
@contact.self_link.
|
187
|
+
expect(@contact.self_link).to eq("https://www.google.com/m8/feeds/contacts/example%40gmail.com/full/0")
|
177
188
|
end
|
178
189
|
it "should return the right photo link" do
|
179
|
-
@contact.photo_link.
|
190
|
+
expect(@contact.photo_link).to eq("https://www.google.com/m8/feeds/photos/media/example%40gmail.com/0")
|
180
191
|
end
|
181
192
|
it "should return the right edit link" do
|
182
|
-
@contact.edit_link.
|
193
|
+
expect(@contact.edit_link).to eq("https://www.google.com/m8/feeds/contacts/example%40gmail.com/full/0")
|
183
194
|
end
|
184
195
|
it "should return the right edit photo link" do
|
185
196
|
# TODO: there isn't one in this contact, hahah
|
186
|
-
@contact.edit_photo_link.
|
197
|
+
expect(@contact.edit_photo_link).to eq(nil)
|
187
198
|
end
|
188
199
|
it "should try to fetch a photo" do
|
189
|
-
@oauth =
|
190
|
-
@oauth.
|
200
|
+
@oauth = double("oauth")
|
201
|
+
allow(@oauth).to receive(:get).and_return(Hashie::Mash.new({
|
191
202
|
"body" => "some response", # could use example response here
|
192
203
|
"code" => 200
|
193
204
|
}))
|
194
205
|
# @api = GoogleContactsApi::Api.new(@oauth)
|
195
|
-
@api =
|
196
|
-
@api.
|
206
|
+
@api = double("api")
|
207
|
+
allow(@api).to receive(:oauth).and_return(@oauth)
|
197
208
|
@contact = GoogleContactsApi::Contact.new(@contact_json_hash, nil, @api)
|
198
|
-
@oauth.
|
209
|
+
expect(@oauth).to receive("get").with(@contact.photo_link)
|
199
210
|
@contact.photo
|
200
211
|
end
|
201
212
|
# TODO: there isn't any phone number in here
|
202
213
|
pending "should return all phone numbers"
|
203
214
|
it "should return all e-mail addresses" do
|
204
|
-
@contact.emails.
|
215
|
+
expect(@contact.emails).to eq(["contact1@example.com"])
|
205
216
|
end
|
206
217
|
it "should return the right primary e-mail address" do
|
207
|
-
@contact.primary_email.
|
218
|
+
expect(@contact.primary_email).to eq("contact1@example.com")
|
208
219
|
end
|
209
220
|
it "should return an empty array if there are no e-mail addresses" do
|
210
221
|
@contact = GoogleContactsApi::Contact.new(contact_no_emails_json_hash)
|
211
|
-
@contact.emails.
|
222
|
+
expect(@contact.emails).to eq([])
|
212
223
|
end
|
213
224
|
it "should return nil if there is no primary e-mail address" do
|
214
225
|
@contact2 = GoogleContactsApi::Contact.new(contact_no_emails_json_hash)
|
215
|
-
@contact2.primary_email.
|
226
|
+
expect(@contact2.primary_email).to be_nil
|
216
227
|
@contact3 = GoogleContactsApi::Contact.new(contact_no_primary_email_json_hash)
|
217
|
-
@contact3.primary_email.
|
228
|
+
expect(@contact3.primary_email).to be_nil
|
218
229
|
end
|
219
230
|
it "should return all instant messaging accounts" do
|
220
|
-
@contact.ims.
|
231
|
+
expect(@contact.ims).to eq(["contact1@example.com"])
|
221
232
|
end
|
222
233
|
it "should return an empty array if there are no instant messaging accounts" do
|
223
234
|
@contact = GoogleContactsApi::Contact.new(contact_no_emails_json_hash)
|
224
|
-
@contact.ims.
|
235
|
+
expect(@contact.ims).to eq([])
|
236
|
+
end
|
237
|
+
|
238
|
+
#
|
239
|
+
describe 'Contacts API v3 fields' do
|
240
|
+
before do
|
241
|
+
@empty = GoogleContactsApi::Contact.new
|
242
|
+
|
243
|
+
@partly_empty = GoogleContactsApi::Contact.new(
|
244
|
+
'gd$name' => {},
|
245
|
+
'gContact$relation' => []
|
246
|
+
)
|
247
|
+
|
248
|
+
@contact_v3 = GoogleContactsApi::Contact.new(
|
249
|
+
'gd$name' => {
|
250
|
+
'gd$givenName' => { '$t' => 'John' },
|
251
|
+
'gd$familyName' => { '$t' => 'Doe' },
|
252
|
+
'gd$fullName' => { '$t' => 'John Doe' }
|
253
|
+
},
|
254
|
+
'gContact$relation' => [ { '$t' => 'Jane', 'rel' => 'spouse' } ],
|
255
|
+
'gd$structuredPostalAddress' => [
|
256
|
+
{
|
257
|
+
'gd$country' => { '$t' => 'United States of America' },
|
258
|
+
'gd$formattedAddress' => { '$t' => "2345 Long Dr. #232\nSomwhere\nIL\n12345\nUnited States of America" },
|
259
|
+
'gd$city' => { '$t' => 'Somwhere' },
|
260
|
+
'gd$street' => { '$t' => '2345 Long Dr. #232' },
|
261
|
+
'gd$region' => { '$t' => 'IL' },
|
262
|
+
'gd$postcode' => { '$t' => '12345' }
|
263
|
+
},
|
264
|
+
{
|
265
|
+
'rel' => 'http://schemas.google.com/g/2005#home',
|
266
|
+
'gd$country' => { '$t' => 'United States of America' },
|
267
|
+
'gd$formattedAddress' => { '$t' => "123 Far Ln.\nAnywhere\nMO\n67891\nUnited States of America" },
|
268
|
+
'gd$city' => { '$t' => 'Anywhere' },
|
269
|
+
'gd$street' => { '$t' => '123 Far Ln.' },
|
270
|
+
'gd$region' => { '$t' => 'MO' },
|
271
|
+
'gd$postcode' => { '$t' => '67891' }
|
272
|
+
}
|
273
|
+
],
|
274
|
+
'gd$email' => [
|
275
|
+
{
|
276
|
+
'primary' => 'true',
|
277
|
+
'rel' => 'http://schemas.google.com/g/2005#other',
|
278
|
+
'address' => 'johnsmith@example.com'
|
279
|
+
}
|
280
|
+
],
|
281
|
+
'gd$phoneNumber' => [
|
282
|
+
{
|
283
|
+
'primary' => 'true',
|
284
|
+
'$t' => '(123) 334-5158',
|
285
|
+
'rel' => 'http://schemas.google.com/g/2005#mobile'
|
286
|
+
}
|
287
|
+
]
|
288
|
+
)
|
289
|
+
end
|
290
|
+
|
291
|
+
it 'should catch nil values for nested fields' do
|
292
|
+
expect(@empty.nested_t_field_or_nil('gd$name', 'gd$givenName')).to be_nil
|
293
|
+
expect(@partly_empty.nested_t_field_or_nil('gd$name', 'gd$givenName')).to be_nil
|
294
|
+
expect(@contact_v3.nested_t_field_or_nil('gd$name', 'gd$givenName')).to eq('John')
|
295
|
+
end
|
296
|
+
|
297
|
+
it 'has given_name' do
|
298
|
+
expect(@contact_v3).to receive(:nested_t_field_or_nil).with('gd$name', 'gd$givenName').and_return('val')
|
299
|
+
expect(@contact_v3.given_name).to eq('val')
|
300
|
+
end
|
301
|
+
|
302
|
+
it 'has family_name' do
|
303
|
+
expect(@contact_v3).to receive(:nested_t_field_or_nil).with('gd$name', 'gd$familyName').and_return('val')
|
304
|
+
expect(@contact_v3.family_name).to eq('val')
|
305
|
+
end
|
306
|
+
|
307
|
+
it 'has full_name' do
|
308
|
+
expect(@contact_v3).to receive(:nested_t_field_or_nil).with('gd$name', 'gd$fullName').and_return('val')
|
309
|
+
expect(@contact_v3.full_name).to eq('val')
|
310
|
+
end
|
311
|
+
|
312
|
+
it 'has relations' do
|
313
|
+
expect(@empty.relations).to eq([])
|
314
|
+
expect(@partly_empty.relations).to eq([])
|
315
|
+
expect(@contact_v3.relations).to eq([ { '$t' => 'Jane', 'rel' => 'spouse' } ])
|
316
|
+
end
|
317
|
+
it 'has spouse' do
|
318
|
+
expect(@empty.spouse).to be_nil
|
319
|
+
expect(@partly_empty.spouse).to be_nil
|
320
|
+
expect(@contact_v3.spouse).to eq('Jane')
|
321
|
+
end
|
322
|
+
|
323
|
+
it 'has addresses' do
|
324
|
+
expect(@empty.addresses).to eq([])
|
325
|
+
|
326
|
+
formatted_addresses = [
|
327
|
+
{
|
328
|
+
:rel => 'work',
|
329
|
+
:country => 'United States of America',
|
330
|
+
:formatted_address => "2345 Long Dr. #232\nSomwhere\nIL\n12345\nUnited States of America",
|
331
|
+
:city => 'Somwhere',
|
332
|
+
:street => '2345 Long Dr. #232',
|
333
|
+
:region => 'IL',
|
334
|
+
:postcode => '12345'
|
335
|
+
},
|
336
|
+
{
|
337
|
+
:rel => 'home',
|
338
|
+
:country => 'United States of America',
|
339
|
+
:formatted_address => "123 Far Ln.\nAnywhere\nMO\n67891\nUnited States of America",
|
340
|
+
:city => 'Anywhere',
|
341
|
+
:street => '123 Far Ln.',
|
342
|
+
:region => 'MO',
|
343
|
+
:postcode => '67891'
|
344
|
+
}
|
345
|
+
]
|
346
|
+
expect(@contact_v3.addresses).to eq(formatted_addresses)
|
347
|
+
end
|
348
|
+
|
349
|
+
it 'has full phone numbers' do
|
350
|
+
expect(@empty.phone_numbers_full).to eq([])
|
351
|
+
expect(@contact_v3.phone_numbers_full).to eq([ { :primary => true, :number => '(123) 334-5158', :rel => 'mobile' } ])
|
352
|
+
end
|
353
|
+
it 'has full emails' do
|
354
|
+
expect(@empty.emails_full).to eq([])
|
355
|
+
expect(@contact_v3.emails_full).to eq([ { :primary => true, :address => 'johnsmith@example.com', :rel => 'other' } ])
|
356
|
+
end
|
225
357
|
end
|
226
358
|
end
|
227
|
-
|
359
|
+
|
228
360
|
describe "Group" do
|
229
361
|
before(:all) do
|
230
362
|
@group_json_hash = group_json_hash
|
@@ -232,31 +364,31 @@ describe "GoogleContactsApi" do
|
|
232
364
|
end
|
233
365
|
# ok, these tests are kind of silly
|
234
366
|
it "should return the right title" do
|
235
|
-
@group.title.
|
367
|
+
expect(@group.title).to eq("System Group: My Contacts")
|
236
368
|
end
|
237
369
|
it "should return the right id" do
|
238
|
-
@group.id.
|
370
|
+
expect(@group.id).to eq("http://www.google.com/m8/feeds/groups/example%40gmail.com/base/6")
|
239
371
|
end
|
240
372
|
it "should return the right content" do
|
241
373
|
# TODO: Nothing in source, oops
|
242
|
-
@group.content.
|
374
|
+
expect(@group.content).to eq("System Group: My Contacts")
|
243
375
|
end
|
244
376
|
it "should return the right updated time" do
|
245
377
|
# different representation slightly
|
246
|
-
@group.updated.to_s.
|
378
|
+
expect(@group.updated.to_s).to eq("1970-01-01T00:00:00+00:00")
|
247
379
|
end
|
248
380
|
it "should tell me if it's a system group" do
|
249
|
-
@group.
|
381
|
+
expect(@group).to be_system_group
|
250
382
|
end
|
251
383
|
it "should get contacts from the group and cache them" do
|
252
|
-
@api =
|
253
|
-
@api.
|
384
|
+
@api = double("api")
|
385
|
+
allow(@api).to receive(:get).and_return(Hashie::Mash.new({
|
254
386
|
"body" => "some response", # could use example response here
|
255
387
|
"code" => 200
|
256
388
|
}))
|
257
|
-
GoogleContactsApi::ContactSet.
|
389
|
+
allow(GoogleContactsApi::ContactSet).to receive(:new).and_return("contact set")
|
258
390
|
@group = GoogleContactsApi::Group.new(@contact_json_hash, nil, @api)
|
259
|
-
@api.
|
391
|
+
expect(@api).to receive("get").with(an_instance_of(String),
|
260
392
|
hash_including({"group" => @group.id})).once
|
261
393
|
@group.contacts
|
262
394
|
@group.contacts
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google_contacts_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-08-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -112,17 +112,17 @@ dependencies:
|
|
112
112
|
requirement: !ruby/object:Gem::Requirement
|
113
113
|
none: false
|
114
114
|
requirements:
|
115
|
-
- -
|
115
|
+
- - ~>
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version: '0'
|
117
|
+
version: '3.0'
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
none: false
|
122
122
|
requirements:
|
123
|
-
- -
|
123
|
+
- - ~>
|
124
124
|
- !ruby/object:Gem::Version
|
125
|
-
version: '0'
|
125
|
+
version: '3.0'
|
126
126
|
- !ruby/object:Gem::Dependency
|
127
127
|
name: rdoc
|
128
128
|
requirement: !ruby/object:Gem::Requirement
|
@@ -187,8 +187,7 @@ dependencies:
|
|
187
187
|
- - ! '>='
|
188
188
|
- !ruby/object:Gem::Version
|
189
189
|
version: '0'
|
190
|
-
description: Lets you read from the Google Contacts API. Posting to come later.
|
191
|
-
to come later.
|
190
|
+
description: Lets you read from the Google Contacts API. Posting to come later.
|
192
191
|
email: ayliang@gmail.com
|
193
192
|
executables: []
|
194
193
|
extensions: []
|
@@ -235,7 +234,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
235
234
|
version: '0'
|
236
235
|
segments:
|
237
236
|
- 0
|
238
|
-
hash: -
|
237
|
+
hash: -813692119890348937
|
239
238
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
240
239
|
none: false
|
241
240
|
requirements:
|