From ed6e9a76b34f478e79ef529a26b7fbbf9773576a Mon Sep 17 00:00:00 2001 From: superchilled Date: Fri, 10 Mar 2023 17:38:30 +0000 Subject: [PATCH 1/7] Adding WhatsApp sticker tests --- .../messaging/channels/whats_app_test.rb | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/test/vonage/messaging/channels/whats_app_test.rb b/test/vonage/messaging/channels/whats_app_test.rb index a2e19e41..fd789745 100644 --- a/test/vonage/messaging/channels/whats_app_test.rb +++ b/test/vonage/messaging/channels/whats_app_test.rb @@ -44,6 +44,22 @@ def test_with_valid_type_file_specified assert_includes whatsapp.data, :file end + def test_with_valid_type_sticker_specified_with_url + whatsapp = Vonage::Messaging::Channels::WhatsApp.new(type: 'sticker', message: { url: 'https://example.com/file.webp' }) + + assert_equal 'sticker', whatsapp.data[:message_type] + assert_includes whatsapp.data, :sticker + assert_includes whatsapp.data[:sticker], :url + end + + def test_with_valid_type_sticker_specified_with_id + whatsapp = Vonage::Messaging::Channels::WhatsApp.new(type: 'sticker', message: { id: '16aec2a6-bf69-11ed-afa1-0242ac120002' }) + + assert_equal 'sticker', whatsapp.data[:message_type] + assert_includes whatsapp.data, :sticker + assert_includes whatsapp.data[:sticker], :id + end + def test_with_invalid_type_specified exception = assert_raises { whatsapp = Vonage::Messaging::Channels::WhatsApp.new(type: 'vcard', message: { url: 'https://example.com/contact.vcf' }) @@ -113,6 +129,24 @@ def test_custom_message_with_invalid_message_type assert_match ":message must be a Hash", exception.message end + def test_sticker_without_id_or_url + exception = assert_raises { + whatsapp = Vonage::Messaging::Channels::WhatsApp.new(type: 'sticker', message: {}) + } + + assert_instance_of Vonage::ClientError, exception + assert_match ":message must contain either :id or :url", exception.message + end + + def test_sticker_with_both_id_and_url + exception = assert_raises { + whatsapp = Vonage::Messaging::Channels::WhatsApp.new(type: 'sticker', message: { url: 'https://example.com/file.webp', id: '16aec2a6-bf69-11ed-afa1-0242ac120002' }) + } + + assert_instance_of Vonage::ClientError, exception + assert_match ":message must contain either :id or :url", exception.message + end + def test_with_opts_client_ref whatsapp = Vonage::Messaging::Channels::WhatsApp.new(type: 'text', message: 'Hello world!', opts: { client_ref: 'abc123' }) From 577ef1741f73506c546f227bf5e12e9beb791cf6 Mon Sep 17 00:00:00 2001 From: superchilled Date: Fri, 10 Mar 2023 17:46:45 +0000 Subject: [PATCH 2/7] Implementing WhatsApp Sticker messages --- lib/vonage/messaging/channels/whats_app.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/vonage/messaging/channels/whats_app.rb b/lib/vonage/messaging/channels/whats_app.rb index a8c0fa12..45035dcd 100644 --- a/lib/vonage/messaging/channels/whats_app.rb +++ b/lib/vonage/messaging/channels/whats_app.rb @@ -2,7 +2,7 @@ module Vonage class Messaging::Channels::WhatsApp < Messaging::Message - MESSAGE_TYPES = ['text', 'image', 'audio', 'video', 'file', 'template', 'custom'] + MESSAGE_TYPES = ['text', 'image', 'audio', 'video', 'file', 'template', 'sticker', 'custom'] attr_reader :data @@ -35,6 +35,8 @@ def verify_message raise Vonage::ClientError.new(":name is required in :template") unless message[:name] raise Vonage::ClientError.new(":whatsapp is required in :opts") unless opts[:whatsapp] raise Vonage::ClientError.new(":locale is required in :whatsapp") unless opts[:whatsapp][:locale] + when 'sticker' + raise Vonage::ClientError.new(":message must contain either :id or :url") unless message.has_key?(:id) ^ message.has_key?(:url) when 'custom' raise Vonage::ClientError.new(":message must be a Hash") unless message.is_a? Hash else From f92e1f865cd7cb788b0795accc167c952aa9d935 Mon Sep 17 00:00:00 2001 From: superchilled Date: Sun, 12 Mar 2023 17:11:14 +0000 Subject: [PATCH 3/7] Adding new Viber message types --- lib/vonage/messaging/channels/viber.rb | 9 ++++- test/vonage/messaging/channels/viber_test.rb | 41 ++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/lib/vonage/messaging/channels/viber.rb b/lib/vonage/messaging/channels/viber.rb index 06275e01..a6e24642 100644 --- a/lib/vonage/messaging/channels/viber.rb +++ b/lib/vonage/messaging/channels/viber.rb @@ -2,7 +2,7 @@ module Vonage class Messaging::Channels::Viber < Messaging::Message - MESSAGE_TYPES = ['text', 'image'] + MESSAGE_TYPES = ['text', 'image', 'video', 'file'] attr_reader :data @@ -33,6 +33,13 @@ def verify_message when 'image' raise Vonage::ClientError.new(":message must be a Hash") unless message.is_a? Hash raise Vonage::ClientError.new(":url is required in :message") unless message[:url] + when 'video' + raise Vonage::ClientError.new(":message must be a Hash") unless message.is_a? Hash + raise Vonage::ClientError.new(":url is required in :message") unless message[:url] + raise Vonage::ClientError.new(":thumb_url is required in :message") unless message[:thumb_url] + when 'file' + raise Vonage::ClientError.new(":message must be a Hash") unless message.is_a? Hash + raise Vonage::ClientError.new(":url is required in :message") unless message[:url] end end end diff --git a/test/vonage/messaging/channels/viber_test.rb b/test/vonage/messaging/channels/viber_test.rb index 8ec899dd..9681f417 100644 --- a/test/vonage/messaging/channels/viber_test.rb +++ b/test/vonage/messaging/channels/viber_test.rb @@ -23,6 +23,20 @@ def test_with_valid_type_image_specified assert_includes viber.data, :image end + def test_with_valid_type_video_specified + viber = Vonage::Messaging::Channels::Viber.new(type: 'video', message: { url: 'https://example.com/video.mp4', thumb_url: 'https://example.com/file1.jpg' }) + + assert_equal 'video', viber.data[:message_type] + assert_includes viber.data, :video + end + + def test_with_valid_type_file_specified + viber = Vonage::Messaging::Channels::Viber.new(type: 'file', message: { url: 'https://example.com/file.pdf' }) + + assert_equal 'file', viber.data[:message_type] + assert_includes viber.data, :file + end + def test_with_invalid_type_specified exception = assert_raises { viber = Vonage::Messaging::Channels::Viber.new(type: 'audio', message: { url: 'https://example.com/audio.mp3' }) @@ -56,6 +70,33 @@ def test_image_without_url assert_match ":url is required in :message", exception.message end + def test_video_without_url + exception = assert_raises { + viber = Vonage::Messaging::Channels::Viber.new(type: 'video', message: { thumb_url: 'https://example.com/file1.jpg' }) + } + + assert_instance_of Vonage::ClientError, exception + assert_match ":url is required in :message", exception.message + end + + def test_video_without_thumb_url + exception = assert_raises { + viber = Vonage::Messaging::Channels::Viber.new(type: 'video', message: { url: 'https://example.com/video.mp4' }) + } + + assert_instance_of Vonage::ClientError, exception + assert_match ":thumb_url is required in :message", exception.message + end + + def test_file_without_url + exception = assert_raises { + viber = Vonage::Messaging::Channels::Viber.new(type: 'file', message: {}) + } + + assert_instance_of Vonage::ClientError, exception + assert_match ":url is required in :message", exception.message + end + def test_with_opts_client_ref viber = Vonage::Messaging::Channels::Viber.new(type: 'text', message: 'Hello world!', opts: { client_ref: 'abc123' }) From c39b28a183191bb24272e3f05643f938edf1a0ca Mon Sep 17 00:00:00 2001 From: superchilled Date: Thu, 30 Mar 2023 11:46:38 +0100 Subject: [PATCH 4/7] Updating Numbers to use Basic auth --- lib/vonage/numbers.rb | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/vonage/numbers.rb b/lib/vonage/numbers.rb index 08c86293..d3cafdc0 100644 --- a/lib/vonage/numbers.rb +++ b/lib/vonage/numbers.rb @@ -5,6 +5,8 @@ module Vonage class Numbers < Namespace include Keys + self.authentication = Basic + self.host = :rest_host # Retrieve all the inbound numbers associated with your Vonage account. @@ -52,7 +54,7 @@ class Numbers < Namespace # @see https://developer.nexmo.com/api/developer/numbers#getOwnedNumbers # def list(params = nil) - request('/account/numbers', params: params, response_class: ListResponse) + request("/account/numbers", params: params, response_class: ListResponse) end # Retrieve inbound numbers that are available for the specified country. @@ -100,7 +102,7 @@ def list(params = nil) # @see https://developer.nexmo.com/api/developer/numbers#getAvailableNumbers # def search(params) - request('/number/search', params: params, response_class: ListResponse) + request("/number/search", params: params, response_class: ListResponse) end # Request to purchase a specific inbound number. @@ -125,7 +127,12 @@ def search(params) # @see https://developer.nexmo.com/api/developer/numbers#buyANumber # def buy(params) - request('/number/buy', params: params, type: Post, response_class: Response) + request( + "/number/buy", + params: params, + type: Post, + response_class: Response + ) end # Cancel your subscription for a specific inbound number. @@ -150,7 +157,12 @@ def buy(params) # @see https://developer.nexmo.com/api/developer/numbers#cancelANumber # def cancel(params) - request('/number/cancel', params: params, type: Post, response_class: Response) + request( + "/number/cancel", + params: params, + type: Post, + response_class: Response + ) end # Change the behaviour of a number that you own. @@ -198,14 +210,25 @@ def cancel(params) # @see https://developer.nexmo.com/api/developer/numbers#updateANumber # def update(params) - request('/number/update', params: camelcase(params), type: Post, response_class: Response) + request( + "/number/update", + params: camelcase(params), + type: Post, + response_class: Response + ) end private # A specific implementation of iterable_request for Numbers, because the Numbers API # handles pagination differently to other Vonage APIs - def iterable_request(path, response: nil, response_class: nil, params: {}, &block) + def iterable_request( + path, + response: nil, + response_class: nil, + params: {}, + &block + ) response = parse(response, response_class) params[:index] = 1 unless params.has_key?(:index) size = params.fetch(:size, 10) From 62f1acd874239dbcc9bc4f91fe1fc2c11d2ab1e8 Mon Sep 17 00:00:00 2001 From: superchilled Date: Thu, 30 Mar 2023 11:54:53 +0100 Subject: [PATCH 5/7] Updating NumbersTest tests in line with Basic auth usage --- test/vonage/numbers_test.rb | 87 +++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 33 deletions(-) diff --git a/test/vonage/numbers_test.rb b/test/vonage/numbers_test.rb index fbd21323..3d66fc54 100644 --- a/test/vonage/numbers_test.rb +++ b/test/vonage/numbers_test.rb @@ -1,5 +1,5 @@ # typed: false -require_relative './test' +require_relative "./test" class Vonage::NumbersTest < Vonage::Test def numbers @@ -7,45 +7,49 @@ def numbers end def test_list_method - uri = 'https://rest.nexmo.com/account/numbers' + uri = "https://rest.nexmo.com/account/numbers" - params = {size: 25, pattern: '33'} + params = { size: 25, pattern: "33" } - stub_request(:get, uri).with(query: params.merge(api_key_and_secret)).to_return(numbers_response) + stub_request(:get, uri).with(query: params).to_return(numbers_response) response = numbers.list(params) - response.each{|resp| assert_kind_of Vonage::Numbers::ListResponse, resp } + response.each { |resp| assert_kind_of Vonage::Numbers::ListResponse, resp } end def test_list_method_without_args - uri = 'https://rest.nexmo.com/account/numbers' + uri = "https://rest.nexmo.com/account/numbers" - stub_request(:get, uri).with(query: api_key_and_secret).to_return(numbers_response) + stub_request(:get, uri).to_return(numbers_response) response = numbers.list - response.each{|resp| assert_kind_of Vonage::Numbers::ListResponse, resp } + response.each { |resp| assert_kind_of Vonage::Numbers::ListResponse, resp } end def test_search_method - uri = 'https://rest.nexmo.com/number/search' + uri = "https://rest.nexmo.com/number/search" - params = {size: 25, country: country} + params = { size: 25, country: country } - stub_request(:get, uri).with(query: params.merge(api_key_and_secret)).to_return(response) + stub_request(:get, uri).with(query: params).to_return(response) assert_kind_of Vonage::Numbers::ListResponse, numbers.search(params) end def test_search_method_with_auto_advance - uri = 'https://rest.nexmo.com/number/search' + uri = "https://rest.nexmo.com/number/search" - params = {country: country} + params = { country: country } - stub_request(:get, uri).with(query: params.merge(api_key_and_secret)).to_return(numbers_response_paginated_page_1) + stub_request(:get, uri).with(query: params).to_return( + numbers_response_paginated_page_1 + ) - stub_request(:get, uri).with(query: params.merge(api_key_and_secret.merge(index: 2))).to_return(numbers_response_paginated_page_2) + stub_request(:get, uri).with(query: params.merge(index: 2)).to_return( + numbers_response_paginated_page_2 + ) response = numbers.search(params.merge(auto_advance: true)) @@ -54,11 +58,13 @@ def test_search_method_with_auto_advance end def test_search_method_with_auto_advance_with_index_offset_by_one_page - uri = 'https://rest.nexmo.com/number/search' + uri = "https://rest.nexmo.com/number/search" - params = {country: country} + params = { country: country } - stub_request(:get, uri).with(query: params.merge(api_key_and_secret.merge(index: 2))).to_return(numbers_response_paginated_page_2) + stub_request(:get, uri).with(query: params.merge(index: 2)).to_return( + numbers_response_paginated_page_2 + ) response = numbers.search(params.merge(auto_advance: true, index: 2)) @@ -67,34 +73,49 @@ def test_search_method_with_auto_advance_with_index_offset_by_one_page end def test_buy_method - uri = 'https://rest.nexmo.com/number/buy' + uri = "https://rest.nexmo.com/number/buy" - params = {country: country, msisdn: msisdn} + params = { country: country, msisdn: msisdn } - stub_request(:post, uri).with(headers: headers, body: params.merge(api_key_and_secret)).to_return(response) + stub_request(:post, uri).with(headers: headers, body: params).to_return( + response + ) assert_kind_of Vonage::Numbers::Response, numbers.buy(params) end def test_cancel_method - uri = 'https://rest.nexmo.com/number/cancel' + uri = "https://rest.nexmo.com/number/cancel" - params = {country: country, msisdn: msisdn} + params = { country: country, msisdn: msisdn } - stub_request(:post, uri).with(headers: headers, body: params.merge(api_key_and_secret)).to_return(response) + stub_request(:post, uri).with(headers: headers, body: params).to_return( + response + ) assert_kind_of Vonage::Numbers::Response, numbers.cancel(params) end def test_update_method - uri = 'https://rest.nexmo.com/number/update' - - mo_http_url = 'https://example.com/callback' - - params = {'country' => country, 'msisdn' => msisdn, 'moHttpUrl' => mo_http_url} - - stub_request(:post, uri).with(headers: headers, body: params.merge(api_key_and_secret)).to_return(response) - - assert_kind_of Vonage::Numbers::Response, numbers.update(country: country, msisdn: msisdn, mo_http_url: mo_http_url) + uri = "https://rest.nexmo.com/number/update" + + mo_http_url = "https://example.com/callback" + + params = { + "country" => country, + "msisdn" => msisdn, + "moHttpUrl" => mo_http_url + } + + stub_request(:post, uri).with(headers: headers, body: params).to_return( + response + ) + + assert_kind_of Vonage::Numbers::Response, + numbers.update( + country: country, + msisdn: msisdn, + mo_http_url: mo_http_url + ) end end From 2d6d434c268b970e0d9cdec789978e9a77aaf2ae Mon Sep 17 00:00:00 2001 From: superchilled Date: Thu, 30 Mar 2023 15:01:40 +0100 Subject: [PATCH 6/7] Update changelog --- CHANGES.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 8ff78ab1..58a6ac94 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,8 +1,13 @@ +# 7.9.0 + +* Updates the Messages API implementation to ass support for `video` and `file` messages types to the Viber channel, and `sticker` messages in the WhatsApp channel. [#260](https://github.com/Vonage/vonage-ruby-sdk/pull/260) +* Updates the Numbers API implementation to use Basic authentication. [#262](https://github.com/Vonage/vonage-ruby-sdk/pull/262) + # 7.8.2 * Updates the GSM::CHARACTERS constant to remove `ç` and instead add `Ç`. Fixes [#256](https://github.com/Vonage/vonage-ruby-sdk/issues/255) * Updates code comments for `SMS#send` method to remove properties for unsupported message types `vCal`, `vCard`, and `wappush` -* Updates namespacing for referencing `SecurityUtils#secure_compare` method due to change in `ruby-jwt ` gem dependency. +* Updates namespacing for referencing `SecurityUtils#secure_compare` method due to change in `ruby-jwt ` gem dependency. # 7.8.1 From 1b4ddc606d2a0d661bafa7abe5857642078c1e85 Mon Sep 17 00:00:00 2001 From: superchilled Date: Thu, 30 Mar 2023 15:01:46 +0100 Subject: [PATCH 7/7] Bump version --- lib/vonage/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vonage/version.rb b/lib/vonage/version.rb index ef4e2370..c34e68f7 100644 --- a/lib/vonage/version.rb +++ b/lib/vonage/version.rb @@ -1,5 +1,5 @@ # typed: strong module Vonage - VERSION = '7.8.2' + VERSION = "7.9.0" end