diff --git a/examples/group_create.rb b/examples/group_create.rb new file mode 100644 index 0000000..1467410 --- /dev/null +++ b/examples/group_create.rb @@ -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 diff --git a/examples/group_list.rb b/examples/group_list.rb new file mode 100644 index 0000000..3c09253 --- /dev/null +++ b/examples/group_list.rb @@ -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 diff --git a/lib/messagebird/client.rb b/lib/messagebird/client.rb index d69b324..e634899 100644 --- a/lib/messagebird/client.rb +++ b/lib/messagebird/client.rb @@ -5,6 +5,7 @@ require 'messagebird/balance' require 'messagebird/contact' require 'messagebird/error' +require 'messagebird/group' require 'messagebird/hlr' require 'messagebird/http_client' require 'messagebird/list' @@ -161,5 +162,51 @@ def contact_list(limit = 0, offset = 0) List.new(Contact, request(:get, "contacts?limit=#{limit}&offset=#{offset}")) end + def group(id) + Group.new(request(:get, "groups/#{id}")) + end + + def group_create(name) + Group.new(request(:post, 'groups', { :name => name })) + end + + def group_delete(id) + request(:delete, "groups/#{id}") + end + + def group_list(limit = 0, offset = 0) + List.new(Group, request(:get, "groups?limit=#{limit}&offset=#{offset}")) + end + + def group_update(id, name) + request(:patch, "groups/#{id}", { :name => name }) + end + + def group_add_contacts(group_id, contact_ids) + # We expect an array, but we can handle a string ID as well... + contact_ids = [contact_ids] if contact_ids.is_a? String + + query = add_contacts_query(contact_ids) + + request(:get, "groups/#{group_id}?#{query}") + end + + def group_delete_contact(group_id, contact_id) + request(:delete, "groups/#{group_id}/contacts/#{contact_id}") + end + + private # Applies to every method below this line + + def add_contacts_query(contact_ids) + # add_contacts_query gets a query string to add contacts to a group. + # We're using the alternative "/foo?_method=PUT&key=value" format to send + # the contact IDs as GET params. Sending these in the request body would + # require a painful workaround, as the client sends request bodies as + # JSON by default. See also: + # https://developers.messagebird.com/docs/alternatives. + + '_method=PUT&' + contact_ids.map { |id| "ids[]=#{id}" }.join('&') + end + end end diff --git a/lib/messagebird/contact_reference.rb b/lib/messagebird/contact_reference.rb new file mode 100644 index 0000000..6da56fc --- /dev/null +++ b/lib/messagebird/contact_reference.rb @@ -0,0 +1,7 @@ +require 'messagebird/base' + +module MessageBird + class ContactReference < MessageBird::Base + attr_accessor :href, :totalCount + end +end diff --git a/lib/messagebird/group.rb b/lib/messagebird/group.rb new file mode 100644 index 0000000..d7b19c6 --- /dev/null +++ b/lib/messagebird/group.rb @@ -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 \ No newline at end of file diff --git a/spec/group_spec.rb b/spec/group_spec.rb new file mode 100644 index 0000000..6b512b6 --- /dev/null +++ b/spec/group_spec.rb @@ -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 \ No newline at end of file