google_contacts_api 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.2
1
+ 0.5.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "google_contacts_api"
8
- s.version = "0.4.2"
8
+ s.version = "0.5.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 = "2014-09-26"
12
+ s.date = "2014-09-27"
13
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 = [
@@ -32,6 +32,7 @@ Gem::Specification.new do |s|
32
32
  "lib/google_contacts_api/contacts.rb",
33
33
  "lib/google_contacts_api/group.rb",
34
34
  "lib/google_contacts_api/group_set.rb",
35
+ "lib/google_contacts_api/groups.rb",
35
36
  "lib/google_contacts_api/result.rb",
36
37
  "lib/google_contacts_api/result_set.rb",
37
38
  "lib/google_contacts_api/user.rb",
@@ -2,7 +2,7 @@
2
2
  module GoogleContactsApi
3
3
  module Contacts
4
4
  # Retrieve the contacts for this user or group
5
- def contacts(params = {})
5
+ def get_contacts(params = {})
6
6
  # TODO: Should return empty ContactSet (haven't implemented one yet)
7
7
  return [] unless @api
8
8
  params = params.with_indifferent_access
@@ -11,13 +11,14 @@ module GoogleContactsApi
11
11
  # Return the contacts in this group and cache them.
12
12
  def contacts(params = {})
13
13
  # contacts in this group
14
- @contacts ||= super({"group" => self.id}.merge(params))
14
+ @contacts ||= get_contacts({"group" => self.id}.merge(params))
15
15
  end
16
16
 
17
17
  # Return the contacts in this group, retrieving them again from the server.
18
18
  def contacts!(params = {})
19
19
  # contacts in this group
20
- @contacts = super({"group" => self.id}.merge(params))
20
+ @contacts = nil
21
+ contacts
21
22
  end
22
23
 
23
24
  # Returns the array of links, as link is an array for Hashie.
@@ -0,0 +1,33 @@
1
+ # Module that implements a method to get groups for a user
2
+ module GoogleContactsApi
3
+ module Groups
4
+ # Retrieve the contacts for this user or group
5
+ def get_groups(params = {})
6
+ params = params.with_indifferent_access
7
+ # compose params into a string
8
+ # See http://code.google.com/apis/contacts/docs/3.0/reference.html#Parameters
9
+ # alt, q, max-results, start-index, updated-min,
10
+ # orderby, showdeleted, requirealldeleted, sortorder
11
+ params["max-results"] = 100000 unless params.key?("max-results")
12
+
13
+ # Set the version, for some reason the header is not effective on its own?
14
+ params["v"] = 2
15
+
16
+ url = "groups/default/full"
17
+ # TODO: So weird thing, version 3 doesn't return system groups
18
+ # When it does, uncomment this line and use it to request instead
19
+ # response = @api.get(url, params)
20
+ response = @api.get(url, params)
21
+
22
+ case GoogleContactsApi::Api.parse_response_code(response)
23
+ # TODO: Better handle 401, 403, 404
24
+ when 401; raise
25
+ when 403; raise
26
+ when 404; raise
27
+ when 400...500; raise
28
+ when 500...600; raise
29
+ end
30
+ GoogleContactsApi::GroupSet.new(response.body, @api)
31
+ end
32
+ end
33
+ end
@@ -1,39 +1,35 @@
1
1
  module GoogleContactsApi
2
2
  class User
3
3
  include GoogleContactsApi::Contacts
4
+ include GoogleContactsApi::Groups
4
5
 
5
6
  attr_reader :api
6
7
  def initialize(oauth)
7
8
  @api = GoogleContactsApi::Api.new(oauth)
8
9
  end
9
10
 
10
- # Retrieve the groups for this user.
11
- def groups(params = {})
12
- params = params.with_indifferent_access
13
- # compose params into a string
14
- # See http://code.google.com/apis/contacts/docs/3.0/reference.html#Parameters
15
- # alt, q, max-results, start-index, updated-min,
16
- # orderby, showdeleted, requirealldeleted, sortorder
17
- params["max-results"] = 100000 unless params.key?("max-results")
11
+ # Return the contacts for this user and cache them.
12
+ def contacts(params = {})
13
+ # contacts in this group
14
+ @contacts ||= get_contacts(params)
15
+ end
18
16
 
19
- # Set the version, for some reason the header is not effective on its own?
20
- params["v"] = 2
17
+ # Return the contacts for this user, retrieving them again from the server.
18
+ def contacts!(params = {})
19
+ # contacts in this group
20
+ @contacts = nil
21
+ contacts
22
+ end
21
23
 
22
- url = "groups/default/full"
23
- # TODO: So weird thing, version 3 doesn't return system groups
24
- # When it does, uncomment this line and use it to request instead
25
- # response = @api.get(url, params)
26
- response = @api.get(url, params)
24
+ # Return the groups for this user and cache them.
25
+ def groups(params = {})
26
+ @groups ||= get_groups(params)
27
+ end
27
28
 
28
- case GoogleContactsApi::Api.parse_response_code(response)
29
- # TODO: Better handle 401, 403, 404
30
- when 401; raise
31
- when 403; raise
32
- when 404; raise
33
- when 400...500; raise
34
- when 500...600; raise
35
- end
36
- GoogleContactsApi::GroupSet.new(response.body, @api)
29
+ # Return the groups for this user, retrieving them again from the server.
30
+ def groups!(params = {})
31
+ @groups = nil
32
+ groups
37
33
  end
38
34
  end
39
35
  end
@@ -1,8 +1,8 @@
1
1
  module GoogleContactsApi
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 4
5
- PATCH = 2
4
+ MINOR = 5
5
+ PATCH = 0
6
6
  BUILD = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
@@ -1,5 +1,6 @@
1
1
  require 'google_contacts_api/api'
2
2
  require 'google_contacts_api/contacts'
3
+ require 'google_contacts_api/groups'
3
4
  require 'google_contacts_api/user'
4
5
  require 'google_contacts_api/result_set'
5
6
  require 'google_contacts_api/group_set'
@@ -75,30 +75,90 @@ describe "GoogleContactsApi" do
75
75
  end
76
76
  end
77
77
 
78
- describe "User" do
79
- before(:each) do
80
- @oauth = double("oauth")
81
- @user = GoogleContactsApi::User.new(@oauth)
82
- allow(@user.api).to receive(:get).and_return(Hashie::Mash.new({
83
- "body" => "some response", # could use example response here
84
- "code" => 200
85
- }))
86
- allow(GoogleContactsApi::GroupSet).to receive(:new).and_return("group set")
87
- allow(GoogleContactsApi::ContactSet).to receive(:new).and_return("contact set")
78
+ describe GoogleContactsApi::Contacts do
79
+ let(:api) { double("api") }
80
+ let(:test_class) {
81
+ Class.new do
82
+ include GoogleContactsApi::Contacts
83
+ def initialize(api)
84
+ @api = api
85
+ end
86
+ end
87
+ }
88
+ describe ".get_contacts" do
89
+ it "should get the contacts using the internal @api object" do
90
+ expect(api).to receive(:get).with("contacts/default/full", kind_of(Hash)).and_return(Hashie::Mash.new({
91
+ "body" => "some response", # could use example response here
92
+ "code" => 200
93
+ }))
94
+ allow(GoogleContactsApi::ContactSet).to receive(:new).and_return("contact set")
95
+ expect(test_class.new(api).get_contacts).to eq("contact set")
96
+ end
97
+ end
98
+ end
99
+
100
+ describe GoogleContactsApi::Groups do
101
+ let(:api) { double("api") }
102
+ let(:test_class) {
103
+ Class.new do
104
+ include GoogleContactsApi::Groups
105
+ def initialize(api)
106
+ @api = api
107
+ end
108
+ end
109
+ }
110
+ describe ".get_groups" do
111
+ it "should get the groups using the internal @api object" do
112
+ expect(api).to receive(:get).with("groups/default/full", hash_including(:v => 2)).and_return(Hashie::Mash.new({
113
+ "body" => "some response", # could use example response here
114
+ "code" => 200
115
+ }))
116
+ allow(GoogleContactsApi::GroupSet).to receive(:new).and_return("group set")
117
+ expect(test_class.new(api).get_groups).to eq("group set")
118
+ end
88
119
  end
120
+ end
121
+
122
+ describe GoogleContactsApi::User do
123
+ let(:oauth) { double ("oauth") }
124
+ let(:user) { GoogleContactsApi::User.new(@oauth) }
125
+
89
126
  # Should hit the right URLs and return the right stuff
90
- it "should be able to get groups including system groups" do
91
- expect(@user.api).to receive(:get).with("groups/default/full", hash_including(:v => 2))
92
- expect(@user.groups).to eq("group set")
127
+ describe ".groups" do
128
+ it "should be able to get groups including system groups" do
129
+ expect(user).to receive(:get_groups).and_return("group set")
130
+ expect(user.groups).to eq("group set")
131
+ end
93
132
  end
94
- it "should be able to get contacts" do
95
- expect(@user.api).to receive(:get).with("contacts/default/full", anything)
96
- expect(@user.contacts).to eq("contact set")
133
+ describe ".contacts" do
134
+ it "should be able to get contacts" do
135
+ expect(user).to receive(:get_contacts).and_return("contact set")
136
+ expect(user.contacts).to eq("contact set")
137
+ end
138
+ it "should use the contact cache for subsequent access" do
139
+ expect(user).to receive(:get_contacts).and_return("contact set").once
140
+ user.contacts
141
+ contacts = user.contacts
142
+ expect(contacts).to eq("contact set")
143
+ end
144
+ end
145
+ describe ".contacts!" do
146
+ it "should be able to get contacts" do
147
+ expect(user).to receive(:get_contacts).and_return("contact set")
148
+ expect(user.contacts!).to eq("contact set")
149
+ end
150
+ it "should reload the contacts" do
151
+ expect(user).to receive(:get_contacts).and_return("contact set").twice
152
+ user.contacts
153
+ contacts = user.contacts!
154
+ expect(contacts).to eq("contact set")
155
+ end
97
156
  end
98
157
  end
99
158
 
100
159
  describe "ResultSet" do
101
- # no testing, it's just an implementation detail to use inheritance
160
+ pending ".each"
161
+ pending ".has_more?"
102
162
  end
103
163
 
104
164
  describe "ContactSet" do
@@ -357,7 +417,7 @@ describe "GoogleContactsApi" do
357
417
  end
358
418
  end
359
419
 
360
- describe "Group" do
420
+ describe GoogleContactsApi::Group do
361
421
  before(:all) do
362
422
  @group_json_hash = group_json_hash
363
423
  @group = GoogleContactsApi::Group.new(group_json_hash)
@@ -380,18 +440,39 @@ describe "GoogleContactsApi" do
380
440
  it "should tell me if it's a system group" do
381
441
  expect(@group).to be_system_group
382
442
  end
383
- it "should get contacts from the group and cache them" do
384
- @api = double("api")
385
- allow(@api).to receive(:get).and_return(Hashie::Mash.new({
386
- "body" => "some response", # could use example response here
387
- "code" => 200
388
- }))
389
- allow(GoogleContactsApi::ContactSet).to receive(:new).and_return("contact set")
390
- @group = GoogleContactsApi::Group.new(@contact_json_hash, nil, @api)
391
- expect(@api).to receive("get").with(an_instance_of(String),
392
- hash_including({"group" => @group.id})).once
393
- @group.contacts
394
- @group.contacts
443
+ describe ".contacts" do
444
+ before(:each) do
445
+ @api = double("api")
446
+ @group = GoogleContactsApi::Group.new(@contact_json_hash, nil, @api)
447
+ allow(@group).to receive(:id).and_return("group id")
448
+ end
449
+ it "should be able to get contacts" do
450
+ expect(@group).to receive(:get_contacts).with(hash_including({"group" => "group id"})).and_return("contact set")
451
+ expect(@group.contacts).to eq("contact set")
452
+ end
453
+ it "should use the contact cache for subsequent access" do
454
+ expect(@group).to receive(:get_contacts).with(hash_including({"group" => "group id"})).and_return("contact set").once
455
+ @group.contacts
456
+ contacts = @group.contacts
457
+ expect(contacts).to eq("contact set")
458
+ end
459
+ end
460
+ describe ".contacts!" do
461
+ before(:each) do
462
+ @api = double("api")
463
+ @group = GoogleContactsApi::Group.new(@contact_json_hash, nil, @api)
464
+ allow(@group).to receive(:id).and_return("group id")
465
+ end
466
+ it "should be able to get contacts" do
467
+ expect(@group).to receive(:get_contacts).with(hash_including({"group" => "group id"})).and_return("contact set")
468
+ expect(@group.contacts!).to eq("contact set")
469
+ end
470
+ it "should use the contact cache for subsequent access" do
471
+ expect(@group).to receive(:get_contacts).with(hash_including({"group" => "group id"})).and_return("contact set").twice
472
+ @group.contacts
473
+ contacts = @group.contacts!
474
+ expect(contacts).to eq("contact set")
475
+ end
395
476
  end
396
477
  end
397
478
  end
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.2
4
+ version: 0.5.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: 2014-09-26 00:00:00.000000000 Z
12
+ date: 2014-09-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -210,6 +210,7 @@ files:
210
210
  - lib/google_contacts_api/contacts.rb
211
211
  - lib/google_contacts_api/group.rb
212
212
  - lib/google_contacts_api/group_set.rb
213
+ - lib/google_contacts_api/groups.rb
213
214
  - lib/google_contacts_api/result.rb
214
215
  - lib/google_contacts_api/result_set.rb
215
216
  - lib/google_contacts_api/user.rb
@@ -235,7 +236,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
235
236
  version: '0'
236
237
  segments:
237
238
  - 0
238
- hash: -1381068612794242354
239
+ hash: 1433605714673791610
239
240
  required_rubygems_version: !ruby/object:Gem::Requirement
240
241
  none: false
241
242
  requirements: