Skip to content

Commit 855ab62

Browse files
author
Artur Khabibullin
authored
Merge pull request #19 from messagebird/add-groups
Add groups
2 parents d500b21 + 38e5a80 commit 855ab62

File tree

6 files changed

+312
-0
lines changed

6 files changed

+312
-0
lines changed

examples/group_create.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env ruby
2+
3+
$:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib/')
4+
require 'messagebird'
5+
6+
# ACCESS_KEY = 'YOUR KEY HERE'
7+
# GROUP_NAME = 'YOUR GROUP NAME HERE'
8+
9+
unless defined?(ACCESS_KEY)
10+
puts 'You need to set an ACCESS_KEY constant in this file'
11+
exit 1
12+
end
13+
14+
unless defined?(GROUP_NAME)
15+
puts 'You need to set an GROUP_NAME constant in this file'
16+
exit 1
17+
end
18+
19+
begin
20+
# Create a MessageBird client with the specified ACCESS_KEY.
21+
client = MessageBird::Client.new(ACCESS_KEY)
22+
23+
# Create a new Group object.
24+
group = client.group_create(GROUP_NAME)
25+
26+
# Print the object information.
27+
puts
28+
puts " Group :"
29+
puts " id : #{group.id}"
30+
puts " href : #{group.href}"
31+
puts " name : #{group.name}"
32+
puts " contacts : #{group.contacts.href}"
33+
puts
34+
35+
rescue MessageBird::ErrorException => ex
36+
puts
37+
puts 'An error occurred while creating a group:'
38+
puts
39+
40+
ex.errors.each do |error|
41+
puts " code : #{error.code}"
42+
puts " description : #{error.description}"
43+
puts " parameter : #{error.parameter}"
44+
puts
45+
end
46+
end

examples/group_list.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env ruby
2+
3+
$:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib/')
4+
require 'messagebird'
5+
6+
ACCESS_KEY = 'YOUR KEY HERE'
7+
8+
unless defined?(ACCESS_KEY)
9+
puts 'You need to set an ACCESS_KEY constant in this file'
10+
exit 1
11+
end
12+
13+
begin
14+
# Create a MessageBird client with the specified ACCESS_KEY.
15+
client = MessageBird::Client.new(ACCESS_KEY)
16+
17+
# Fetch the Group list with pagination options (skip the first 5 objects and take 10).
18+
limit = 10
19+
offset = 5
20+
groups = client.group_list(limit, offset)
21+
22+
# Print the object information.
23+
puts
24+
puts "The following information was returned as a Group list:"
25+
puts
26+
puts " count : #{groups.count}"
27+
puts " limit : #{groups.limit}"
28+
puts " offset : #{groups.offset}"
29+
puts " totalCount : #{groups.totalCount}"
30+
puts " links : #{groups.links}"
31+
32+
unless groups.items.empty?
33+
group = groups[0] # Equivalent to groups.items[0]
34+
35+
puts " Group :"
36+
puts " id : #{group.id}"
37+
puts " href : #{group.href}"
38+
puts " name : #{group.name}"
39+
puts " contacts : #{group.contacts.href}"
40+
end
41+
42+
rescue MessageBird::ErrorException => ex
43+
puts
44+
puts 'An error occurred while listing your groups:'
45+
puts
46+
47+
ex.errors.each do |error|
48+
puts " code : #{error.code}"
49+
puts " description : #{error.description}"
50+
puts " parameter : #{error.parameter}"
51+
puts
52+
end
53+
end

lib/messagebird/client.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require 'messagebird/balance'
66
require 'messagebird/contact'
77
require 'messagebird/error'
8+
require 'messagebird/group'
89
require 'messagebird/hlr'
910
require 'messagebird/http_client'
1011
require 'messagebird/list'
@@ -161,5 +162,51 @@ def contact_list(limit = 0, offset = 0)
161162
List.new(Contact, request(:get, "contacts?limit=#{limit}&offset=#{offset}"))
162163
end
163164

165+
def group(id)
166+
Group.new(request(:get, "groups/#{id}"))
167+
end
168+
169+
def group_create(name)
170+
Group.new(request(:post, 'groups', { :name => name }))
171+
end
172+
173+
def group_delete(id)
174+
request(:delete, "groups/#{id}")
175+
end
176+
177+
def group_list(limit = 0, offset = 0)
178+
List.new(Group, request(:get, "groups?limit=#{limit}&offset=#{offset}"))
179+
end
180+
181+
def group_update(id, name)
182+
request(:patch, "groups/#{id}", { :name => name })
183+
end
184+
185+
def group_add_contacts(group_id, contact_ids)
186+
# We expect an array, but we can handle a string ID as well...
187+
contact_ids = [contact_ids] if contact_ids.is_a? String
188+
189+
query = add_contacts_query(contact_ids)
190+
191+
request(:get, "groups/#{group_id}?#{query}")
192+
end
193+
194+
def group_delete_contact(group_id, contact_id)
195+
request(:delete, "groups/#{group_id}/contacts/#{contact_id}")
196+
end
197+
198+
private # Applies to every method below this line
199+
200+
def add_contacts_query(contact_ids)
201+
# add_contacts_query gets a query string to add contacts to a group.
202+
# We're using the alternative "/foo?_method=PUT&key=value" format to send
203+
# the contact IDs as GET params. Sending these in the request body would
204+
# require a painful workaround, as the client sends request bodies as
205+
# JSON by default. See also:
206+
# https://developers.messagebird.com/docs/alternatives.
207+
208+
'_method=PUT&' + contact_ids.map { |id| "ids[]=#{id}" }.join('&')
209+
end
210+
164211
end
165212
end

lib/messagebird/contact_reference.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'messagebird/base'
2+
3+
module MessageBird
4+
class ContactReference < MessageBird::Base
5+
attr_accessor :href, :totalCount
6+
end
7+
end

lib/messagebird/group.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'messagebird/base'
2+
require 'messagebird/contact_reference'
3+
4+
module MessageBird
5+
class Group < MessageBird::Base
6+
attr_accessor :id, :href, :name, :contacts, :createdDatetime,
7+
:updatedDatetime
8+
9+
def contacts=(value)
10+
@contacts = MessageBird::ContactReference.new(value)
11+
end
12+
13+
def createdDatetime=(value)
14+
@createdDatetime = value_to_time(value)
15+
end
16+
17+
def updatedDatetime=(value)
18+
@updatedDatetime = value_to_time(value)
19+
end
20+
end
21+
end

spec/group_spec.rb

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
describe 'Group' do
2+
3+
it 'creates' do
4+
5+
http_client = double(MessageBird::HttpClient)
6+
client = MessageBird::Client.new('', http_client)
7+
8+
expect(http_client)
9+
.to receive(:request)
10+
.with(:post, 'groups', { :name => 'friends'})
11+
.and_return('{}')
12+
13+
client.group_create('friends')
14+
15+
end
16+
17+
it 'deletes' do
18+
19+
http_client = double(MessageBird::HttpClient)
20+
client = MessageBird::Client.new('', http_client)
21+
22+
expect(http_client)
23+
.to receive(:request)
24+
.with(:delete, 'groups/group-id', {})
25+
.and_return('')
26+
27+
client.group_delete('group-id')
28+
29+
end
30+
31+
it 'lists' do
32+
33+
http_client = double(MessageBird::HttpClient)
34+
client = MessageBird::Client.new('', http_client)
35+
36+
expect(http_client)
37+
.to receive(:request)
38+
.with(:get, 'groups?limit=0&offset=0', {})
39+
.and_return('{"offset": 0,"limit": 10,"count": 2,"totalCount": 2,"links": {"first": "https://rest.messagebird.com/groups?offset=0&limit=10","previous": null,"next": null,"last": "https://rest.messagebird.com/groups?offset=0&limit=10"},"items": [{"id": "first-id","href": "https://rest.messagebird.com/groups/first-id","name": "First","contacts": {"totalCount": 3,"href": "https://rest.messagebird.com/groups/first-id/contacts"},"createdDatetime": "2018-07-25T11:47:42+00:00","updatedDatetime": "2018-07-25T14:03:09+00:00"},{"id": "second-id","href": "https://rest.messagebird.com/groups/second-id","name": "Second","contacts": {"totalCount": 4,"href": "https://rest.messagebird.com/groups/second-id/contacts"},"createdDatetime": "2018-07-25T11:47:39+00:00","updatedDatetime": "2018-07-25T14:03:09+00:00"}]}')
40+
41+
list = client.group_list
42+
43+
expect(list.count).to eq 2
44+
expect(list[0].id).to eq 'first-id'
45+
46+
end
47+
48+
it 'reads an existing' do
49+
50+
http_client = double(MessageBird::HttpClient)
51+
client = MessageBird::Client.new('', http_client)
52+
53+
expect(http_client)
54+
.to receive(:request)
55+
.with(:get, 'groups/group-id', {})
56+
.and_return('{"id": "group-id","href": "https://rest.messagebird.com/groups/group-id","name": "Friends","contacts": {"totalCount": 3,"href": "https://rest.messagebird.com/groups/group-id"},"createdDatetime": "2018-07-25T12:16:10+00:00","updatedDatetime": "2018-07-25T12:16:23+00:00"}')
57+
58+
group = client.group('group-id')
59+
60+
expect(group.id).to eq 'group-id'
61+
expect(group.name).to eq 'Friends'
62+
63+
end
64+
65+
it 'reads the contact reference' do
66+
67+
http_client = double(MessageBird::HttpClient)
68+
client = MessageBird::Client.new('', http_client)
69+
70+
expect(http_client)
71+
.to receive(:request)
72+
.with(:get, 'groups/group-id', {})
73+
.and_return('{"id": "group-id","href": "https://rest.messagebird.com/groups/group-id","name": "Friends","contacts": {"totalCount": 3,"href": "https://rest.messagebird.com/groups/group-id/contacts"},"createdDatetime": "2018-07-25T12:16:10+00:00","updatedDatetime": "2018-07-25T12:16:23+00:00"}')
74+
75+
group = client.group('group-id')
76+
77+
expect(group.contacts.href).to eq 'https://rest.messagebird.com/groups/group-id/contacts'
78+
expect(group.contacts.totalCount).to eq 3
79+
80+
end
81+
82+
it 'updates' do
83+
84+
http_client = double(MessageBird::HttpClient)
85+
client = MessageBird::Client.new('', http_client)
86+
87+
expect(http_client)
88+
.to receive(:request)
89+
.with(:patch, 'groups/group-id', { :name => 'family' })
90+
.and_return('{}')
91+
92+
client.group_update('group-id', 'family')
93+
94+
end
95+
96+
it 'adds contacts' do
97+
98+
http_client = double(MessageBird::HttpClient)
99+
client = MessageBird::Client.new('', http_client)
100+
101+
expect(http_client)
102+
.to receive(:request)
103+
.with(:get, 'groups/group-id?_method=PUT&ids[]=first-contact-id&ids[]=second-contact-id', { })
104+
.and_return('{}')
105+
106+
client.group_add_contacts('group-id', ['first-contact-id', 'second-contact-id'])
107+
108+
end
109+
110+
it 'adds single contact' do
111+
112+
http_client = double(MessageBird::HttpClient)
113+
client = MessageBird::Client.new('', http_client)
114+
115+
expect(http_client)
116+
.to receive(:request)
117+
.with(:get, 'groups/group-id?_method=PUT&ids[]=contact-id', { })
118+
.and_return('{}')
119+
120+
client.group_add_contacts('group-id', 'contact-id')
121+
122+
end
123+
124+
it 'removes contact' do
125+
126+
http_client = double(MessageBird::HttpClient)
127+
client = MessageBird::Client.new('', http_client)
128+
129+
expect(http_client)
130+
.to receive(:request)
131+
.with(:delete, 'groups/group-id/contacts/contact-id', {})
132+
.and_return('{}')
133+
134+
client.group_delete_contact('group-id', 'contact-id')
135+
136+
end
137+
138+
end

0 commit comments

Comments
 (0)