Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Instagram Support #272

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions lib/vonage/messaging/channels/instagram.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true
# typed: true

module Vonage
class Messaging::Channels::Instagram < Messaging::Message
MESSAGE_TYPES = %w[text image audio video file].freeze

attr_reader :data

# initializes a Vonage::Message for Instagram
# @param [Hash] attributes
def initialize(attributes = {})
@type = attributes.fetch(:type, nil)
@message = attributes.fetch(:message, nil)
@opts = attributes.fetch(:opts, {})
@data = {}

after_initialize!
end

private

# sets channel to 'instagram' and calls super creating a Vonage::Message
# to be sent via SDK
# @return Message
def build
data[:channel] = 'instagram'
super
end

# raises ClientError if message has an invalid type from defined
# MESSAGE_TYPES
# @raise [Vonage::ClientError]
def verify_type
raise Vonage::ClientError, 'Invalid message type' unless
MESSAGE_TYPES.include?(type)
end

# raises ClientError if message has invalid arguments
# @raise [Vonage::ClientError]
# @return nil
def verify_message
case type
when 'text'
raise Vonage::ClientError, ':message must be a String' unless
message.is_a? String
else
raise Vonage::ClientError, ':message must be a Hash' unless
message.is_a? Hash
raise Vonage::ClientError, ':url is required in :message' unless
message[:url]
end
end
end
end
3 changes: 2 additions & 1 deletion lib/vonage/messaging/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class Messaging::Message
mms: Vonage::Messaging::Channels::MMS,
whatsapp: Vonage::Messaging::Channels::WhatsApp,
messenger: Vonage::Messaging::Channels::Messenger,
viber: Vonage::Messaging::Channels::Viber
viber: Vonage::Messaging::Channels::Viber,
instagram: Vonage::Messaging::Channels::Instagram
}

class << self
Expand Down
134 changes: 134 additions & 0 deletions test/vonage/messaging/channels/instagram_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# frozen_string_literal: true
# typed: false

class Vonage::Messaging::Channels::MessengerTest < Vonage::Test
def test_instagram_initialize
message = Vonage::Messaging::Channels::Instagram.new(
type: 'text', message: 'Hello world!'
)

assert_kind_of Vonage::Messaging::Channels::Instagram, message
assert_equal message.data, {
channel: 'instagram', message_type: 'text', text: 'Hello world!'
}
end

def test_with_valid_type_text_specified
message = Vonage::Messaging::Channels::Instagram.new(
type: 'text', message: 'Hello world!'
)

assert_equal 'text', message.data[:message_type]
assert_includes message.data, :text
end

def test_with_valid_type_image_specified
message = Vonage::Messaging::Channels::Instagram.new(
type: 'image',
message: { url: 'https://example.com/image.jpg' }
)

assert_equal 'image', message.data[:message_type]
assert_includes message.data, :image
end

def test_with_valid_type_audio_specified
message = Vonage::Messaging::Channels::Instagram.new(
type: 'audio',
message: { url: 'https://example.com/audio.mp3' }
)

assert_equal 'audio', message.data[:message_type]
assert_includes message.data, :audio
end

def test_with_valid_type_video_specified
message = Vonage::Messaging::Channels::Instagram.new(
type: 'video',
message: { url: 'https://example.com/video.mp4' }
)

assert_equal 'video', message.data[:message_type]
assert_includes message.data, :video
end

def test_with_valid_type_file_specified
message = Vonage::Messaging::Channels::Instagram.new(
type: 'file',
message: { url: 'https://example.com/file.pdf' }
)

assert_equal 'file', message.data[:message_type]
assert_includes message.data, :file
end

def test_with_invalid_type_specified
exception = assert_raises do
Vonage::Messaging::Channels::Instagram.new(
type: 'vcard',
message: { url: 'https://example.com/contact.vcf' }
)
end

assert_instance_of Vonage::ClientError, exception
assert_match 'Invalid message type', exception.message
end

def test_with_valid_message_class
message = Vonage::Messaging::Channels::Instagram.new(
type: 'image',
message: { url: 'https://example.com/image.jpg' }
)

assert_kind_of Hash, message.data[:image]
end

def test_with_invalid_message_class
exception = assert_raises do
Vonage::Messaging::Channels::Instagram.new(
type: 'image', message: 'https://example.com/image.jpg'
)
end

assert_instance_of Vonage::ClientError, exception
assert_match ':message must be a Hash', exception.message
end

def test_object_message_without_url
exception = assert_raises do
Vonage::Messaging::Channels::Instagram.new(
type: 'image',
message: {}
)
end

assert_instance_of Vonage::ClientError, exception
assert_match ':url is required in :message', exception.message
end

def test_with_opts_client_ref
message = Vonage::Messaging::Channels::Instagram.new(
type: 'text',
message: 'Hello world!',
opts: { client_ref: 'abc123' }
)

assert_equal 'abc123', message.data[:client_ref]
end

def test_with_opts_instagram_object
message = Vonage::Messaging::Channels::Instagram.new(
type: 'text',
message: 'Hello world!',
opts: {
instagram: {
category: 'response',
tag: 'CONFIRMED_EVENT_UPDATE'
}
}
)

assert_equal 'response', message.data[:instagram][:category]
assert_equal 'CONFIRMED_EVENT_UPDATE', message.data[:instagram][:tag]
end
end