Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
Remove Unisend and BatchSend classes
Move the methods to send single message and bulk messages
to base class Valuefirst::Valuefirst
Relevant changes to spec
  • Loading branch information
spidergears committed May 17, 2015
1 parent 4b86eb6 commit 8b676bf
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 101 deletions.
2 changes: 0 additions & 2 deletions lib/valuefirst.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require "valuefirst/version"
require "valuefirst/valuefirst.rb"
require "valuefirst/uni_send.rb"
require "valuefirst/batch_send.rb"
require "valuefirst/xml_payload/xml_generator.rb"
require "valuefirst/xml_payload/request_credit.rb"
require "valuefirst/xml_payload/batchtext.rb"
Expand Down
23 changes: 0 additions & 23 deletions lib/valuefirst/batch_send.rb

This file was deleted.

20 changes: 0 additions & 20 deletions lib/valuefirst/uni_send.rb

This file was deleted.

12 changes: 12 additions & 0 deletions lib/valuefirst/valuefirst.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ def status_request guid_seq_hash
call_api payload, "status"
end

def send_message message_content, phone_number, sender_id
payload = XmlPayload::TextMessage.textmessage @config, message_content, phone_number, sender_id
call_api payload, "send"
end

def bulksend_message file_path
raise ArgumentError, "File does not exist." unless File.exists? file_path.to_s
raise ArgumentError, "File is not readable." unless File.readable? file_path.to_s
payload = XmlPayload::Batchtext.batchtext @config, file_path
call_api payload, "send"
end

private

def call_api payload, action
Expand Down
34 changes: 0 additions & 34 deletions spec/valuefirst/batch_send_spec.rb

This file was deleted.

22 changes: 0 additions & 22 deletions spec/valuefirst/uni_send_spec.rb

This file was deleted.

46 changes: 46 additions & 0 deletions spec/valuefirst/valuefirst_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,52 @@
end
end

describe "#bulksend_message" do
let! (:file_path) { File.absolute_path("spec/support/csv_sample.csv") }
let! (:valuefirst_obj) { Valuefirst::Valuefirst.new(username: "user_name", password: "password", default_sender: "default_sender") }
let!(:payload) { XmlPayload::Batchtext.batchtext(valuefirst_obj.config, file_path) }

before do
FileUtils.chmod "-r", "spec/support/non_readable_file"
allow_any_instance_of(Valuefirst::Valuefirst).to receive(:call_api).with(payload, "send")
allow(XmlPayload::Batchtext).to receive(:batchtext).with(valuefirst_obj.config, file_path)
.and_return(payload)
end
after do
FileUtils.chmod "+r", "spec/support/non_readable_file"
end
it "raises error when file does not exist" do
expect{valuefirst_obj.bulksend_message "non_existent_file_path"}.to raise_error(ArgumentError).with_message("File does not exist.")
end

it "raises error when file is not readable" do
expect{valuefirst_obj.bulksend_message File.absolute_path("spec/support/non_readable_file")}.to raise_error(ArgumentError).with_message("File is not readable.")
end

it "makes message send request to valuefirst_api" do
expect(XmlPayload::Batchtext).to receive(:batchtext).with(valuefirst_obj.config, file_path)
expect_any_instance_of(Valuefirst::Valuefirst).to receive(:call_api).with(payload, "send")
valuefirst_obj.bulksend_message File.absolute_path(file_path)
end
end

describe "#send_message" do
let! (:valuefirst_obj) { Valuefirst::Valuefirst.new(username: "user_name", password: "password", default_sender: "default_sender") }
let!(:payload) { XmlPayload::TextMessage.textmessage(valuefirst_obj.config, "message_content", "phone_number", "sender_id") }

before do
allow_any_instance_of(Valuefirst::Valuefirst).to receive(:call_api).with(payload, "send")
allow(XmlPayload::TextMessage).to receive(:textmessage).with(valuefirst_obj.config, "message_content", "phone_number", "sender_id")
.and_return(payload)
end

it "makes message send request to valuefirst_api" do
expect(XmlPayload::TextMessage).to receive(:textmessage).with(valuefirst_obj.config, "message_content", "phone_number", "sender_id")
expect_any_instance_of(Valuefirst::Valuefirst).to receive(:call_api).with(payload, "send")
valuefirst_obj.send_message "message_content", "phone_number", "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

0 comments on commit 8b676bf

Please sign in to comment.