Skip to content

Commit

Permalink
Merge pull request #52 from cowboyrushforth/twitter
Browse files Browse the repository at this point in the history
adding twitter support
  • Loading branch information
mroth committed Sep 29, 2012
2 parents 0f29097 + 32a21c6 commit 72911b4
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/lolcommits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require 'lolcommits/plugins/dot_com'
require 'lolcommits/plugins/tranzlate'
require 'lolcommits/plugins/statsd'
require 'lolcommits/plugins/lol_twitter'

# require runner after all the plugins have been required
require 'lolcommits/runner'
19 changes: 12 additions & 7 deletions lib/lolcommits/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def puts_plugins
puts "Available plugins: #{names.join(', ')}"
end

def do_configure!(plugin)
def do_configure!(plugin, forced_options=nil)
if plugin.nil? || plugin.strip == ''
puts_plugins
print "Name of plugin to configure: "
Expand All @@ -90,14 +90,19 @@ def do_configure!(plugin)
return
end

options = plugin_object.options.inject(Hash.new) do |acc, option|
print "#{option}: "
val = STDIN.gets.strip
val = true if val == 'true'
val = false if val == 'false'
if forced_options.nil?
options = plugin_object.options.inject(Hash.new) do |acc, option|
print "#{option}: "
val = STDIN.gets.strip
val = true if val == 'true'
val = false if val == 'false'

acc.merge(option => val)
acc.merge(option => val)
end
else
options = forced_options
end

config = self.user_configuration || Hash.new
config[plugin] = options
File.open(self.user_configuration_file, 'w') do |f|
Expand Down
99 changes: 99 additions & 0 deletions lib/lolcommits/plugins/lol_twitter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
require 'yaml'
require 'twitter'
require 'oauth'

TWITTER_CONSUMER_KEY = 'qc096dJJCxIiqDNUqEsqQ'
TWITTER_CONSUMER_SECRET = 'rvjNdtwSr1H0TvBvjpk6c4bvrNydHmmbvv7gXZQI'

module Lolcommits

class LolTwitter < Plugin

def initialize(runner)
super
self.name = 'twitter'
self.default = false
end

def initial_twitter_auth
puts "\n--------------------------------------------"
puts "Need to grab twitter tokens (first time only)"
puts "---------------------------------------------"

consumer = OAuth::Consumer.new(TWITTER_CONSUMER_KEY,
TWITTER_CONSUMER_SECRET,
:site => 'http://api.twitter.com',
:request_endpoint => 'http://api.twitter.com',
:sign_in => true)

request_token = consumer.get_request_token
rtoken = request_token.token
rsecret = request_token.secret

puts "\n1.) Open the following url in your browser, get the PIN:\n\n"
puts request_token.authorize_url
puts "\n2.) Enter PIN, then press enter:"

begin
STDOUT.flush
twitter_pin = STDIN.gets.chomp
rescue
end

if (twitter_pin.nil?) || (twitter_pin.length == 0)
puts "\n\tERROR: Could not read PIN, auth fail"
return
end

begin
OAuth::RequestToken.new(consumer, rtoken, rsecret)
access_token = request_token.get_access_token(:oauth_verifier => twitter_pin)
rescue Twitter::Unauthorized
puts "> FAIL!"
return
end

# saves the config back to yaml file.
self.runner.config.do_configure!('twitter', { 'enabled' => true,
'access_token' => access_token.token,
'secret' => access_token.secret })
end

def run
commit_msg = self.runner.message
available_commit_msg_size = 128
tweet_msg = commit_msg.length > available_commit_msg_size ? "#{commit_msg[0..(available_commit_msg_size-3)]}..." : commit_msg
tweet_text = "#{tweet_msg} #lolcommits"
puts "Tweeting: #{tweet_text}"

if configuration['access_token'].nil? || configuration['secret'].nil?
initial_twitter_auth()
end

if configuration['access_token'].nil? || configuration['secret'].nil?
puts "Missing Twitter Credentials - Skipping The Tweet"
return
end

Twitter.configure do |config|
config.consumer_key = TWITTER_CONSUMER_KEY
config.consumer_secret = TWITTER_CONSUMER_SECRET
end

client = Twitter::Client.new(
:oauth_token => configuration['access_token'],
:oauth_token_secret => configuration['secret']
)
retries = 2
begin
if client.update_with_media(tweet_text, File.open(self.runner.main_image, 'r'))
puts "\t--> Tweet Sent!"
end
rescue Twitter::Error::InternalServerError
retries -= 1
retry if retries > 0
puts "\t ! --> Tweet 500 Error - Tweet Not Posted"
end
end
end
end
1 change: 1 addition & 0 deletions lib/lolcommits/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Runner

# Executed Last
set_callback :run, :after, :cleanup!
set_callback :run, :after, :execute_lolcommits_lol_twitter
set_callback :run, :after, :execute_lolcommits_stats_d
set_callback :run, :after, :execute_lolcommits_dot_com
set_callback :run, :after, :execute_lolcommits_loltext
Expand Down
2 changes: 2 additions & 0 deletions lolcommits.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Gem::Specification.new do |s|
s.add_runtime_dependency('git', '~> 1.2.5')
s.add_runtime_dependency('choice', '~> 0.1.6')
s.add_runtime_dependency('launchy', '~> 2.1.1')
s.add_runtime_dependency("twitter")
s.add_runtime_dependency("oauth")

s.add_development_dependency('rdoc')
s.add_development_dependency('aruba')
Expand Down

0 comments on commit 72911b4

Please sign in to comment.