Skip to content

Commit

Permalink
Update Messages API implementation (#308)
Browse files Browse the repository at this point in the history
* Minor usage improvements to Messages API implementation
  • Loading branch information
superchilled authored Apr 23, 2024
1 parent 5b6171d commit 35e4214
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ jobs:
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
- name: Install dependencies
run: bundle install
bundler-cache: true
- name: Run tests
run: bundle exec rake test
env:
Expand Down
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 7.24.0

* Updating Video API functionality with methods for Live Captions, Audio Connector, Experience Composer, and a `publisheronly` cleint token role. [#307](https://github.com/Vonage/vonage-ruby-sdk/pull/307)
* Updating Messages API implementation to add a delegator for the `Message` class, and enforce some required args in `Messaging#send`. [#308](https://github.com/Vonage/vonage-ruby-sdk/pull/308)

# 7.23.0

Expand Down
11 changes: 8 additions & 3 deletions lib/vonage/messaging.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
# typed: true
# frozen_string_literal: true
require 'forwardable'

module Vonage
class Messaging < Namespace
extend Forwardable

self.authentication = BearerToken

self.request_body = JSON

def_delegators Message, *Message::CHANNELS.keys

# Send a Message.
#
# @example
# message = Vonage::Messaging::Message.sms(message: "Hello world!")
# message = client.messaging.sms(message: "Hello world!")
# response = client.messaging.send(to: "447700900000", from: "447700900001", **message)
#
# @option params [required, String] :to
Expand All @@ -22,8 +27,8 @@ class Messaging < Namespace
#
# @see https://developer.vonage.com/api/messages-olympus#SendMessage
#
def send(params)
request('/v1/messages', params: params, type: Post)
def send(to:, from:, **message)
request('/v1/messages', params: {to: to, from: from, **message}, type: Post)
end

# Validate a JSON Web Token from a Messages API Webhook.
Expand Down
17 changes: 17 additions & 0 deletions test/vonage/messaging_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ def messaging_uri
'https://api.nexmo.com/v1/messages'
end

def test_valid_message_delegator
message = Vonage::Messaging::Message.sms(message: "Hello world!")
assert_equal message, messaging.sms(message: "Hello world!")
end

def test_invalid_message_delegator
assert_raises { messaging.invalid }
end

def test_send_method
params = {
to: "447700900000",
Expand All @@ -26,6 +35,14 @@ def test_send_method
assert_kind_of Vonage::Response, messaging.send(to: "447700900000", from: "447700900001", **message)
end

def test_send_method_without_to
assert_raises(ArgumentError) { messaging.send(from: "447700900001", message: "Hello world!") }
end

def test_send_method_without_from
assert_raises(ArgumentError) { messaging.send(to: "447700900000", message: "Hello world!") }
end

def test_verify_webhook_token_method_with_valid_secret_passed_in
verification = messaging.verify_webhook_token(token: sample_webhook_token, signature_secret: sample_valid_signature_secret)

Expand Down

0 comments on commit 35e4214

Please sign in to comment.