From 04ab27d7b410ea6124bf04ee4f88c1d424813bcd Mon Sep 17 00:00:00 2001 From: Jacob Rothstein Date: Mon, 30 Nov 2009 16:21:26 -0800 Subject: [PATCH] sms handler and tests --- lib/sms_handler.rb | 19 ++++++++++++ test/unit/sms_handler_test.rb | 54 +++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 lib/sms_handler.rb create mode 100644 test/unit/sms_handler_test.rb diff --git a/lib/sms_handler.rb b/lib/sms_handler.rb new file mode 100644 index 0000000..4e3ad61 --- /dev/null +++ b/lib/sms_handler.rb @@ -0,0 +1,19 @@ +class SmsHandler + attr_reader :message, :connection + + def initialize(message) + @connection = Clickatell::API.authenticate settings["api_id"], + settings["username"], settings["password"] + + @message = message + end + + def settings + BREAKGLASS_SETTINGS['sms'] + end + + def contact(person) + Rails.logger.info "Sending sms: #{message.text} to: #{person.sms}" + connection.send_message "1#{person.sms}", message.text + end +end \ No newline at end of file diff --git a/test/unit/sms_handler_test.rb b/test/unit/sms_handler_test.rb new file mode 100644 index 0000000..7be3f25 --- /dev/null +++ b/test/unit/sms_handler_test.rb @@ -0,0 +1,54 @@ +require 'test_helper' + +class SmsHandlerTest < ActiveSupport::TestCase + context 'with test settings' do + setup do + @settings = BREAKGLASS_SETTINGS['sms'] = { + 'api_id' => 'my id', + 'username' => 'username', + 'password' => 'password' + } + + @message = messages :hello_world + @person = people :lara + end + + context 'initialization' do + should 'authenticate with clickatell' do + flexmock(Clickatell::API).should_receive(:authenticate).once. + with @settings['api_id'], @settings['username'], @settings['password'] + SmsHandler.new @message + end + end + + context 'with a handler' do + setup do + flexmock(Clickatell::API).should_receive(:authenticate). + and_return(@connection = :connection) + @handler = SmsHandler.new @message + end + + should 'have the right message' do + assert_message @message, @handler + end + + should 'have the right connection' do + assert_connection @connection, @handler + end + end + + context 'contact' do + should 'contact clickatell with the right number and text' do + @connection = flexmock do |conn| + conn.should_receive(:send_message).once. + with "1#{@person.sms}", @message.text + end + + flexmock(Clickatell::API).should_receive(:authenticate). + and_return(@connection) + + SmsHandler.new(@message).contact @person + end + end + end +end