Skip to content

Commit

Permalink
sms handler and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Rothstein committed Dec 1, 2009
1 parent c94bbbe commit 04ab27d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
19 changes: 19 additions & 0 deletions 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
54 changes: 54 additions & 0 deletions 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

0 comments on commit 04ab27d

Please sign in to comment.