-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from messagebird/add-groups
Add groups
- Loading branch information
Showing
6 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/usr/bin/env ruby | ||
|
||
$:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib/') | ||
require 'messagebird' | ||
|
||
# ACCESS_KEY = 'YOUR KEY HERE' | ||
# GROUP_NAME = 'YOUR GROUP NAME HERE' | ||
|
||
unless defined?(ACCESS_KEY) | ||
puts 'You need to set an ACCESS_KEY constant in this file' | ||
exit 1 | ||
end | ||
|
||
unless defined?(GROUP_NAME) | ||
puts 'You need to set an GROUP_NAME constant in this file' | ||
exit 1 | ||
end | ||
|
||
begin | ||
# Create a MessageBird client with the specified ACCESS_KEY. | ||
client = MessageBird::Client.new(ACCESS_KEY) | ||
|
||
# Create a new Group object. | ||
group = client.group_create(GROUP_NAME) | ||
|
||
# Print the object information. | ||
puts | ||
puts " Group :" | ||
puts " id : #{group.id}" | ||
puts " href : #{group.href}" | ||
puts " name : #{group.name}" | ||
puts " contacts : #{group.contacts.href}" | ||
puts | ||
|
||
rescue MessageBird::ErrorException => ex | ||
puts | ||
puts 'An error occurred while creating a group:' | ||
puts | ||
|
||
ex.errors.each do |error| | ||
puts " code : #{error.code}" | ||
puts " description : #{error.description}" | ||
puts " parameter : #{error.parameter}" | ||
puts | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/usr/bin/env ruby | ||
|
||
$:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib/') | ||
require 'messagebird' | ||
|
||
ACCESS_KEY = 'YOUR KEY HERE' | ||
|
||
unless defined?(ACCESS_KEY) | ||
puts 'You need to set an ACCESS_KEY constant in this file' | ||
exit 1 | ||
end | ||
|
||
begin | ||
# Create a MessageBird client with the specified ACCESS_KEY. | ||
client = MessageBird::Client.new(ACCESS_KEY) | ||
|
||
# Fetch the Group list with pagination options (skip the first 5 objects and take 10). | ||
limit = 10 | ||
offset = 5 | ||
groups = client.group_list(limit, offset) | ||
|
||
# Print the object information. | ||
puts | ||
puts "The following information was returned as a Group list:" | ||
puts | ||
puts " count : #{groups.count}" | ||
puts " limit : #{groups.limit}" | ||
puts " offset : #{groups.offset}" | ||
puts " totalCount : #{groups.totalCount}" | ||
puts " links : #{groups.links}" | ||
|
||
unless groups.items.empty? | ||
group = groups[0] # Equivalent to groups.items[0] | ||
|
||
puts " Group :" | ||
puts " id : #{group.id}" | ||
puts " href : #{group.href}" | ||
puts " name : #{group.name}" | ||
puts " contacts : #{group.contacts.href}" | ||
end | ||
|
||
rescue MessageBird::ErrorException => ex | ||
puts | ||
puts 'An error occurred while listing your groups:' | ||
puts | ||
|
||
ex.errors.each do |error| | ||
puts " code : #{error.code}" | ||
puts " description : #{error.description}" | ||
puts " parameter : #{error.parameter}" | ||
puts | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'messagebird/base' | ||
|
||
module MessageBird | ||
class ContactReference < MessageBird::Base | ||
attr_accessor :href, :totalCount | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'messagebird/base' | ||
require 'messagebird/contact_reference' | ||
|
||
module MessageBird | ||
class Group < MessageBird::Base | ||
attr_accessor :id, :href, :name, :contacts, :createdDatetime, | ||
:updatedDatetime | ||
|
||
def contacts=(value) | ||
@contacts = MessageBird::ContactReference.new(value) | ||
end | ||
|
||
def createdDatetime=(value) | ||
@createdDatetime = value_to_time(value) | ||
end | ||
|
||
def updatedDatetime=(value) | ||
@updatedDatetime = value_to_time(value) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
describe 'Group' do | ||
|
||
it 'creates' do | ||
|
||
http_client = double(MessageBird::HttpClient) | ||
client = MessageBird::Client.new('', http_client) | ||
|
||
expect(http_client) | ||
.to receive(:request) | ||
.with(:post, 'groups', { :name => 'friends'}) | ||
.and_return('{}') | ||
|
||
client.group_create('friends') | ||
|
||
end | ||
|
||
it 'deletes' do | ||
|
||
http_client = double(MessageBird::HttpClient) | ||
client = MessageBird::Client.new('', http_client) | ||
|
||
expect(http_client) | ||
.to receive(:request) | ||
.with(:delete, 'groups/group-id', {}) | ||
.and_return('') | ||
|
||
client.group_delete('group-id') | ||
|
||
end | ||
|
||
it 'lists' do | ||
|
||
http_client = double(MessageBird::HttpClient) | ||
client = MessageBird::Client.new('', http_client) | ||
|
||
expect(http_client) | ||
.to receive(:request) | ||
.with(:get, 'groups?limit=0&offset=0', {}) | ||
.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"}]}') | ||
|
||
list = client.group_list | ||
|
||
expect(list.count).to eq 2 | ||
expect(list[0].id).to eq 'first-id' | ||
|
||
end | ||
|
||
it 'reads an existing' do | ||
|
||
http_client = double(MessageBird::HttpClient) | ||
client = MessageBird::Client.new('', http_client) | ||
|
||
expect(http_client) | ||
.to receive(:request) | ||
.with(:get, 'groups/group-id', {}) | ||
.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"}') | ||
|
||
group = client.group('group-id') | ||
|
||
expect(group.id).to eq 'group-id' | ||
expect(group.name).to eq 'Friends' | ||
|
||
end | ||
|
||
it 'reads the contact reference' do | ||
|
||
http_client = double(MessageBird::HttpClient) | ||
client = MessageBird::Client.new('', http_client) | ||
|
||
expect(http_client) | ||
.to receive(:request) | ||
.with(:get, 'groups/group-id', {}) | ||
.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"}') | ||
|
||
group = client.group('group-id') | ||
|
||
expect(group.contacts.href).to eq 'https://rest.messagebird.com/groups/group-id/contacts' | ||
expect(group.contacts.totalCount).to eq 3 | ||
|
||
end | ||
|
||
it 'updates' do | ||
|
||
http_client = double(MessageBird::HttpClient) | ||
client = MessageBird::Client.new('', http_client) | ||
|
||
expect(http_client) | ||
.to receive(:request) | ||
.with(:patch, 'groups/group-id', { :name => 'family' }) | ||
.and_return('{}') | ||
|
||
client.group_update('group-id', 'family') | ||
|
||
end | ||
|
||
it 'adds contacts' do | ||
|
||
http_client = double(MessageBird::HttpClient) | ||
client = MessageBird::Client.new('', http_client) | ||
|
||
expect(http_client) | ||
.to receive(:request) | ||
.with(:get, 'groups/group-id?_method=PUT&ids[]=first-contact-id&ids[]=second-contact-id', { }) | ||
.and_return('{}') | ||
|
||
client.group_add_contacts('group-id', ['first-contact-id', 'second-contact-id']) | ||
|
||
end | ||
|
||
it 'adds single contact' do | ||
|
||
http_client = double(MessageBird::HttpClient) | ||
client = MessageBird::Client.new('', http_client) | ||
|
||
expect(http_client) | ||
.to receive(:request) | ||
.with(:get, 'groups/group-id?_method=PUT&ids[]=contact-id', { }) | ||
.and_return('{}') | ||
|
||
client.group_add_contacts('group-id', 'contact-id') | ||
|
||
end | ||
|
||
it 'removes contact' do | ||
|
||
http_client = double(MessageBird::HttpClient) | ||
client = MessageBird::Client.new('', http_client) | ||
|
||
expect(http_client) | ||
.to receive(:request) | ||
.with(:delete, 'groups/group-id/contacts/contact-id', {}) | ||
.and_return('{}') | ||
|
||
client.group_delete_contact('group-id', 'contact-id') | ||
|
||
end | ||
|
||
end |