Skip to content

Commit

Permalink
Added support for message multicast
Browse files Browse the repository at this point in the history
  • Loading branch information
spidergears committed May 17, 2015
1 parent 8b676bf commit 0422793
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/valuefirst.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "valuefirst/xml_payload/request_credit.rb"
require "valuefirst/xml_payload/batchtext.rb"
require "valuefirst/xml_payload/text_message.rb"
require "valuefirst/xml_payload/multicast_message.rb"
require "valuefirst/xml_payload/status_request.rb"

#gems
Expand Down
19 changes: 18 additions & 1 deletion lib/valuefirst/valuefirst.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def status_request guid_seq_hash
call_api payload, "status"
end

def send_message message_content, phone_number, sender_id
def send_message message_content, phone_number, sender_id = nil
payload = XmlPayload::TextMessage.textmessage @config, message_content, phone_number, sender_id
call_api payload, "send"
end
Expand All @@ -37,6 +37,23 @@ def bulksend_message file_path
call_api payload, "send"
end

def multicast_message message_content, phone_number_array, sender_id = nil
payload = XmlPayload::MulticastMessage.multicastmessage @config, message_content, phone_number_array, sender_id
call_api payload, "send"
end

def send_unicode
end

def bulksend_unicode
end

def send_vcard
end

def bulksend_vcard
end

private

def call_api payload, action
Expand Down
30 changes: 30 additions & 0 deletions lib/valuefirst/xml_payload/multicast_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module XmlPayload
class MulticastMessage
def self.multicastmessage(vfirst_config, message_content, phone_number_array, sender_id = nil)
doc = XmlPayload::XmlGenerator.new_doc
message_tag = XmlPayload::XmlGenerator.create_node("MESSAGE", attributes: {"VER" => XmlPayload::VERSION})
doc.root = message_tag
user_tag = XmlPayload::XmlGenerator.user_tag vfirst_config
doc.root << user_tag

sms_tag = XmlPayload::XmlGenerator.create_node("SMS", attributes: { "UDH" => "0",
"CODING" => "1",
"TEXT" => message_content.to_s,
"PROPERTY" => "0",
"ID" => "1",
})
phone_number_array.each do |phone_number|
address_tag = XmlPayload::XmlGenerator.create_node("ADDRESS", attributes: { "FROM" => sender_id.to_s || vfirst_config.default_sender.to_s,
"TO" => phone_number.to_s,
"SEQ" => "",
"TAG" => "",
})

sms_tag << address_tag
doc.root << sms_tag
end

doc
end
end
end
17 changes: 17 additions & 0 deletions spec/valuefirst/valuefirst_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@
end
end

describe "#multicast_message" do
let! (:valuefirst_obj) { Valuefirst::Valuefirst.new(username: "user_name", password: "password", default_sender: "default_sender") }
let!(:payload) { XmlPayload::MulticastMessage.multicastmessage(valuefirst_obj.config, "message_content", ["XXXXXXXX01", "XXXXXXXX02", "XXXXXXXX03", "XXXXXXXX04"], "sender_id") }

before do
allow_any_instance_of(Valuefirst::Valuefirst).to receive(:call_api).with(payload, "send")
allow(XmlPayload::MulticastMessage).to receive(:multicastmessage).with(valuefirst_obj.config, "message_content", ["XXXXXXXX01", "XXXXXXXX02", "XXXXXXXX03", "XXXXXXXX04"], "sender_id")
.and_return(payload)
end

it "makes message send request to valuefirst_api" do
expect(XmlPayload::MulticastMessage).to receive(:multicastmessage).with(valuefirst_obj.config, "message_content", ["XXXXXXXX01", "XXXXXXXX02", "XXXXXXXX03", "XXXXXXXX04"], "sender_id")
expect_any_instance_of(Valuefirst::Valuefirst).to receive(:call_api).with(payload, "send")
valuefirst_obj.multicast_message "message_content", ["XXXXXXXX01", "XXXXXXXX02", "XXXXXXXX03", "XXXXXXXX04"], "sender_id"
end
end

describe "#api_call" do
it "raises ArgumentError when called with invalid action" do
expect{ valuefirst_obj.send(:call_api, "payload", "invalidStatus") }.to raise_error(ArgumentError)
Expand Down
15 changes: 15 additions & 0 deletions spec/valuefirst/xml_payload/multicast_message_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'
require 'valuefirst.rb'

describe "MulticastMessage" do

describe "::multicastmessage" do
it "it return a dtd compliant xml document" do
payload = XmlPayload::MulticastMessage.multicastmessage(vfirst_config,
"Sample mesage",
["XXXXXXXX01", "XXXXXXXX02", "XXXXXXXX03", "XXXXXXXX04"])
expect(payload.class).to be XML::Document
expect(payload.validate(load_dtd_from_file("messagev12"))).to eq true
end
end
end

0 comments on commit 0422793

Please sign in to comment.