Skip to content

Commit

Permalink
Adding Twilio GitHub service and test
Browse files Browse the repository at this point in the history
* 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
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ config/services.json
/bin
vendor/gems
.bundle
docs/payload_data
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ gem "thin", "1.2.2"
gem "faraday", "0.7.5.pre"
gem "rake", "0.8.7"
gem "statsd-ruby", "0.3.0.github.1"
gem "twilio-ruby", "3.4.2"
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ GEM
httparty (0.7.4)
crack (= 0.1.8)
i18n (0.5.0)
json (1.6.1)
jwt (0.1.3)
json (>= 1.2.4)
mash (0.1.1)
mime-types (1.16)
multi_json (1.0.3)
Expand Down Expand Up @@ -56,6 +59,10 @@ GEM
multipart-post (~> 1.1)
twitter-stream (~> 0.1)
tmail (1.2.3.1)
twilio-ruby (3.4.2)
builder (>= 2.1.2)
jwt (>= 0.1.2)
multi_json (>= 1.0.3)
twitter-stream (0.1.14)
eventmachine (>= 0.12.8)
http_parser.rb (~> 0.5.1)
Expand Down Expand Up @@ -85,6 +92,7 @@ DEPENDENCIES
tilt (~> 1.2.1)
tinder (= 1.7.0)
tmail (= 1.2.3.1)
twilio-ruby (= 3.4.2)
xml-simple (= 1.0.11)
xmpp4r-simple (= 0.8.4)
yajl-ruby (= 0.8.3)
1 change: 1 addition & 0 deletions config/load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
require 'yammer4r'
require 'mq'
require 'statsd'
require 'twilio-ruby'

# vendor
require 'basecamp'
Expand Down
27 changes: 27 additions & 0 deletions docs/twilio
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
25 changes: 25 additions & 0 deletions services/twilio.rb
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
90 changes: 90 additions & 0 deletions test/twilio_test.rb
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 added vendor/cache/json-1.6.1.gem
Binary file not shown.
Binary file added vendor/cache/jwt-0.1.3.gem
Binary file not shown.
Binary file added vendor/cache/twilio-ruby-3.4.2.gem
Binary file not shown.

0 comments on commit a0ac7da

Please sign in to comment.