Skip to content
This repository has been archived by the owner on Jan 31, 2019. It is now read-only.

Commit

Permalink
Add option to only notify on commits to master for Twilio.
Browse files Browse the repository at this point in the history
  • Loading branch information
theycallmeswift committed Oct 31, 2011
1 parent 344366e commit f09faed
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
4 changes: 3 additions & 1 deletion docs/twilio
Expand Up @@ -13,6 +13,7 @@ Install Notes
2. You can find your account_sid and auth_token on your account page at https://www.twilio.com/user/account
3. from_phone must be a "Twilio phone number enabled for SMS. Only phone numbers or short codes purchased from Twilio work here"
4. to_phone is the "destination phone number. Format with a '+' and country code e.g., +16175551212 (E.164 format)."
5. Check master_only if you only want to recieve updates for master

Developer Notes
---------------
Expand All @@ -22,6 +23,7 @@ data
- auth_token
- from_phone
- to_phone
- master_only

payload
- refer to docs/github_payload
- refer to docs/github_payload
35 changes: 24 additions & 11 deletions services/twilio.rb
@@ -1,25 +1,38 @@
class Service::Twilio < Service
string :account_sid, :from_phone, :to_phone
boolean :master_only
password :auth_token

def receive_push
check_configuration_options(data)

sms_body = "#{payload['pusher']['name']} has pushed #{payload['commits'].size} commit(s) to #{payload['repository']['name']}"
client = ::Twilio::REST::Client.new(data['account_sid'], data['auth_token'])
client.account.sms.messages.create(
:from => data['from_phone'],
:to => data['to_phone'],
:body => sms_body
)
send_message(data, sms_body) if send_notification?(data)
end

private


def send_notification?(data)
notify_user = true
if data['master_only'].to_i == 1 && branch_name != 'master'
notify_user = false
end
notify_user
end

def check_configuration_options(data)
raise_config_error 'Account SID must be set' if data['account_sid'].blank?
raise_config_error 'Authorization token must be set' if data['auth_token'].blank?
raise_config_error 'Twilio-enabled phone number or short code must be set' if data['from_phone'].blank?
raise_config_error 'Destination phone number must be set' if data['to_phone'].blank?
end
end

def send_message(data, message)
client = ::Twilio::REST::Client.new(data['account_sid'], data['auth_token'])
client.account.sms.messages.create(
:from => data['from_phone'],
:to => data['to_phone'],
:body => message
)
end
end
37 changes: 32 additions & 5 deletions test/twilio_test.rb
Expand Up @@ -31,7 +31,8 @@ def setup
'account_sid' => 'account_sid',
'auth_token' => 'auth_token',
'from_phone' => '+12223334444',
'to_phone' => '+15556667777'
'to_phone' => '+15556667777',
'master_only' => '0'
}
@payload = {
"after" => "a47fd41f3aa4610ea527dcc1669dfdb9c15c5425",
Expand Down Expand Up @@ -67,17 +68,43 @@ def setup
]
}
end
def test_push

def test_push
svc = service(@data, @payload)
assert_equal 1, @payload['commits'].size
assert_equal 'rtomayko', @payload['pusher']['name']
assert_equal 'grit', @payload['repository']['name']


@stubs.post "/2010-04-01/Accounts/account_sid/SMS/Messages.json" do |env|
[200, {}, '']
end

twilio_response = svc.receive_push
assert twilio_response.is_a?(Twilio::REST::Message)
assert_equal 'rtomayko has pushed 1 commit(s) to grit', twilio_response.body
end

def test_push_master_only_on_non_master
non_master_payload = @payload
non_master_payload["ref"] = "refs/heads/non-master"

data_with_master_only = @data
data_with_master_only['master_only'] = 1

svc = service(data_with_master_only, non_master_payload)
twilio_response = svc.receive_push
assert_equal twilio_response, nil
end

def test_push_master_only_on_master
data_with_master_only = @data
data_with_master_only['master_only'] = 1

@stubs.post "/2010-04-01/Accounts/account_sid/SMS/Messages.json" do |env|
[200, {}, '']
end


svc = service(data_with_master_only, @payload)
twilio_response = svc.receive_push
assert twilio_response.is_a?(Twilio::REST::Message)
assert_equal 'rtomayko has pushed 1 commit(s) to grit', twilio_response.body
Expand Down

0 comments on commit f09faed

Please sign in to comment.