Permalink
Browse files

support posting to twitter via oauth1

  • Loading branch information...
atmos committed Sep 8, 2010
1 parent 434e488 commit e25d92a7dd88a9f4c6a3b600108fddbbcc1eeb9f
Showing with 56 additions and 13 deletions.
  1. +14 −4 docs/twitter
  2. +2 −0 github-services.rb
  3. +40 −9 services/twitter.rb
View
@@ -4,16 +4,26 @@ Twitter
Install Notes
-------------
- 1. email or username will work in username field
-
+1. Get an OAuth Token for Your Twitter Account from the services hooks edit page for your repo.
+2. You're going to be redirected to twitter to allow GitHub to tweet on your behalf.
+3. Be sure that you're logged in as the twitter user you would like to tweet from.
Developer Notes
---------------
data
- - username
- - password
+ - token
+ - secret
- digest (boolean)
payload
- refer to docs/github_payload
+
+Deployment Notes
+----------------
+
+The secrets.yml file should include an entry for the consumer keys github needs to post for people.
+
+ twitter:
+ key: <get it from twitter>
+ secret: <get it from twitter>
View
@@ -24,6 +24,7 @@
require 'xmpp4r'
require 'xmpp4r-simple'
require 'rubyforge'
+require 'oauth'
set :run, true
set :environment, :production
@@ -146,6 +147,7 @@ def report_exception(exception, other)
rescue => boom
$stderr.puts "reporting exception failed:"
$stderr.puts "#{boom.class}: #{boom}"
+ $stderr.puts "#{boom.backtrace.join("\n")}"
# swallow errors
end
end
View
@@ -1,24 +1,55 @@
+class TwitterOAuth
+ def initialize(token, secret)
+ @token = token
+ @secret = secret
+ @secrets = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'config', 'secrets.yml'))
+ end
+
+ def consumer_key
+ @secrets['twitter']['key']
+ end
+
+ def consumer_secret
+ @secrets['twitter']['secret']
+ end
+
+ def consumer
+ @consumer ||= ::OAuth::Consumer.new(consumer_key, consumer_secret,
+ {:site => "http://api.twitter.com"})
+ end
+
+ def post(status)
+ params = { 'status' => status, 'source' => 'github' }
+
+ access_token = ::OAuth::AccessToken.new(consumer, @token, @secret)
+ oauth_response = consumer.request(:post, "/1/statuses/update.json",
+ access_token, { :scheme => :query_string }, params)
+ case oauth_response
+ when Net::HTTPSuccess
+ JSON.parse(oauth_response.body)
+ else
+ nil
+ end
+ end
+end
+
service :twitter do |data, payload|
+ statuses = [ ]
repository = payload['repository']['name']
- url = URI.parse("http://twitter.com/statuses/update.xml")
- statuses = Array.new

This comment has been minimized.

Show comment Hide comment
@technoweenie

technoweenie Sep 8, 2010

Contributor

lol

@technoweenie

technoweenie Sep 8, 2010

Contributor

lol

if data['digest'] == '1'
commit = payload['commits'][-1]
tiny_url = shorten_url(payload['repository']['url'] + '/commits/' + payload['ref_name'])
- statuses.push "[#{repository}] #{tiny_url} #{commit['author']['name']} - #{payload['commits'].length} commits"
+ statuses << "[#{repository}] #{tiny_url} #{commit['author']['name']} - #{payload['commits'].length} commits"
else
payload['commits'].each do |commit|
tiny_url = shorten_url(commit['url'])
- statuses.push "[#{repository}] #{tiny_url} #{commit['author']['name']} - #{commit['message']}"
+ statuses << "[#{repository}] #{tiny_url} #{commit['author']['name']} - #{commit['message']}"
end
end
+ twitter_oauth = TwitterOAuth.new(data['token'], data['secret'])
statuses.each do |status|
- req = Net::HTTP::Post.new(url.path)
- req.basic_auth(data['username'], data['password'])
- req.set_form_data('status' => status, 'source' => 'github')
-
- Net::HTTP.new(url.host, url.port).start { |http| http.request(req) }
+ twitter_oauth.post(status)
end
end

0 comments on commit e25d92a

Please sign in to comment.