forked from github/github-services
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Twilio GitHub service and test
* Adds twilio-ruby gem * Adds Twilio service hook, test and documentation * Updates .gitignore to add docs/payload_data because this file gets written out with varying data
- Loading branch information
David Czarnecki
committed
Oct 24, 2011
1 parent
b047ef9
commit a0ac7da
Showing
10 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ config/services.json | |
/bin | ||
vendor/gems | ||
.bundle | ||
docs/payload_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Twilio | ||
====== | ||
|
||
Allows you to setup a GitHub repository to send out SMS messages on git pushes. | ||
Currently the SMS message is built to say: | ||
|
||
"#{payload['pusher']['name']} has pushed #{payload['commits'].size} commit(s) to #{payload['repository']['name']}" | ||
|
||
Install Notes | ||
------------- | ||
|
||
1. Signup for a Twilio account at http://www.twilio.com/try-twilio?home-page | ||
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)." | ||
|
||
Developer Notes | ||
--------------- | ||
|
||
data | ||
- account_sid | ||
- auth_token | ||
- from_phone | ||
- to_phone | ||
|
||
payload | ||
- refer to docs/github_payload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class Service::Twilio < Service | ||
string :account_sid, :from_phone, :to_phone | ||
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 | ||
) | ||
end | ||
|
||
private | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
require File.expand_path('../helper', __FILE__) | ||
|
||
module Twilio | ||
module REST | ||
class Client | ||
def connect_and_send(request) | ||
{ | ||
"account_sid" => "account_sid", | ||
"api_version" => "2010-04-01", | ||
"body" => "rtomayko has pushed 1 commit(s) to grit", | ||
"date_created" => "Wed, 23 Oct 2011 20 =>01 =>40 +0000", | ||
"date_sent" => nil, | ||
"date_updated" => "Wed, 18 Aug 2010 20 =>01 =>40 +0000", | ||
"direction" => "outbound-api", | ||
"from" => "+12223334444", | ||
"price" => nil, | ||
"sid" => "SM90c6fc909d8504d45ecdb3a3d5b3556e", | ||
"status" => "queued", | ||
"to" => "+15556667777", | ||
"uri" => "/2010-04-01/Accounts/account_sid/SMS/Messages.json" | ||
} | ||
end | ||
end | ||
end | ||
end | ||
|
||
class TwilioTest < Service::TestCase | ||
def setup | ||
@stubs = Faraday::Adapter::Test::Stubs.new | ||
@data = { | ||
'account_sid' => 'account_sid', | ||
'auth_token' => 'auth_token', | ||
'from_phone' => '+12223334444', | ||
'to_phone' => '+15556667777' | ||
} | ||
@payload = { | ||
"after" => "a47fd41f3aa4610ea527dcc1669dfdb9c15c5425", | ||
"ref" => "refs/heads/master", | ||
"before" => "4c8124ffcf4039d292442eeccabdeca5af5c5017", | ||
"compare" => "http://github.com/mojombo/grit/compare/4c8124ffcf4039d292442eeccabdeca5af5c5017...a47fd41f3aa4610ea527dcc1669dfdb9c15c5425", | ||
"forced" => false, | ||
"created" => false, | ||
"deleted" => false, | ||
|
||
"repository" => { | ||
"name" => "grit", | ||
"url" => "http://github.com/mojombo/grit", | ||
"owner" => { "name" => "mojombo", "email" => "tom@mojombo.com" } | ||
}, | ||
|
||
"pusher" => { | ||
"name" => "rtomayko" | ||
}, | ||
|
||
"commits" => [ | ||
{ | ||
"distinct" => true, | ||
"removed" => [], | ||
"message" => "[#WEB-249 status:31 resolution:1] stub git call for Grit#heads test", | ||
"added" => [], | ||
"timestamp" => "2007-10-10T00:11:02-07:00", | ||
"modified" => ["lib/grit/grit.rb", "test/helper.rb", "test/test_grit.rb"], | ||
"url" => "http://github.com/mojombo/grit/commit/06f63b43050935962f84fe54473a7c5de7977325", | ||
"author" => { "name" => "Tom Preston-Werner", "email" => "tom@mojombo.com" }, | ||
"id" => "06f63b43050935962f84fe54473a7c5de7977325" | ||
} | ||
] | ||
} | ||
end | ||
|
||
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 service(*args) | ||
super Service::Twilio, *args | ||
end | ||
end | ||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.