class Lolcommits::LolTwitter

Constants

DEFAULT_SUFFIX
TWITTER_API_ENDPOINT
TWITTER_CONSUMER_KEY
TWITTER_CONSUMER_SECRET
TWITTER_PIN_REGEX
TWITTER_RETRIES

Public Class Methods

name() click to toggle source
# File lib/lolcommits/plugins/lol_twitter.rb, line 146
def self.name
  'twitter'
end
runner_order() click to toggle source
# File lib/lolcommits/plugins/lol_twitter.rb, line 150
def self.runner_order
  :postcapture
end

Public Instance Methods

build_tweet(commit_message) click to toggle source
# File lib/lolcommits/plugins/lol_twitter.rb, line 34
def build_tweet(commit_message)
  prefix = config_with_default('prefix', '')
  suffix = " #{config_with_default('suffix', DEFAULT_SUFFIX)}"
  prefix = "#{prefix} " unless prefix.empty?

  available_commit_msg_size = max_tweet_size - (prefix.length + suffix.length)
  if commit_message.length > available_commit_msg_size
    commit_message = "#{commit_message[0..(available_commit_msg_size - 3)]}..."
  end

  "#{prefix}#{commit_message}#{suffix}"
end
client() click to toggle source
# File lib/lolcommits/plugins/lol_twitter.rb, line 115
def client
  @client ||= Twitter::REST::Client.new do |config|
    config.consumer_key        = TWITTER_CONSUMER_KEY
    config.consumer_secret     = TWITTER_CONSUMER_SECRET
    config.access_token        = configuration['access_token']
    config.access_token_secret = configuration['secret']
  end
end
config_with_default(key, default = nil) click to toggle source
# File lib/lolcommits/plugins/lol_twitter.rb, line 134
def config_with_default(key, default = nil)
  if configuration[key]
    configuration[key].strip.empty? ? default : configuration[key]
  else
    default
  end
end
configure_auth!() click to toggle source
# File lib/lolcommits/plugins/lol_twitter.rb, line 58
def configure_auth!
  puts '---------------------------'
  puts 'Need to grab twitter tokens'
  puts '---------------------------'

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

  print "\n1) Please open this url in your browser to get a PIN for lolcommits:\n\n"
  puts request_token.authorize_url
  print "\n2) Enter PIN, then press enter: "
  twitter_pin = STDIN.gets.strip.downcase.to_s

  unless twitter_pin =~ TWITTER_PIN_REGEX
    puts "\nERROR: '#{twitter_pin}' is not a valid Twitter Auth PIN"
    return
  end

  begin
    debug "Requesting Twitter OAuth Token with PIN: #{twitter_pin}"
    OAuth::RequestToken.new(oauth_consumer, rtoken, rsecret)
    access_token = request_token.get_access_token(:oauth_verifier => twitter_pin)
  rescue OAuth::Unauthorized
    puts "\nERROR: Twitter PIN Auth FAILED!"
    return
  end

  return unless access_token.token && access_token.secret
  puts ''
  puts '------------------------------'
  puts 'Thanks! Twitter Auth Succeeded'
  puts '------------------------------'
  {
    'access_token' => access_token.token,
    'secret'       => access_token.secret
  }
end
configure_options!() click to toggle source
Calls superclass method Lolcommits::Plugin#configure_options!
# File lib/lolcommits/plugins/lol_twitter.rb, line 47
def configure_options!
  options = super
  # ask user to configure tokens if enabling
  if options['enabled']
    auth_config = configure_auth!
    return unless auth_config
    options = options.merge(auth_config).merge(configure_prefix_suffix)
  end
  options
end
configure_prefix_suffix() click to toggle source
# File lib/lolcommits/plugins/lol_twitter.rb, line 97
def configure_prefix_suffix
  print "\n3) Prefix all tweets with something? e.g. @user (leave blank for no prefix): "
  prefix = STDIN.gets.strip
  print "\n4) End all tweets with something? e.g. #hashtag (leave blank for default suffix #{DEFAULT_SUFFIX}): "
  suffix = STDIN.gets.strip

  config = {}
  config['prefix'] = prefix unless prefix.empty?
  config['suffix'] = suffix unless suffix.empty?
  config
end
configured?() click to toggle source
# File lib/lolcommits/plugins/lol_twitter.rb, line 109
def configured?
  !configuration['enabled'].nil? &&
    configuration['access_token'] &&
    configuration['secret']
end
max_tweet_size() click to toggle source
# File lib/lolcommits/plugins/lol_twitter.rb, line 142
def max_tweet_size
  139 - client.configuration.characters_reserved_per_media
end
oauth_consumer() click to toggle source
# File lib/lolcommits/plugins/lol_twitter.rb, line 124
def oauth_consumer
  @oauth_consumer ||= OAuth::Consumer.new(
    TWITTER_CONSUMER_KEY,
    TWITTER_CONSUMER_SECRET,
    :site             => TWITTER_API_ENDPOINT,
    :request_endpoint => TWITTER_API_ENDPOINT,
    :sign_in          => true
  )
end
run_postcapture() click to toggle source
# File lib/lolcommits/plugins/lol_twitter.rb, line 14
def run_postcapture
  return unless valid_configuration?
  tweet = build_tweet(runner.message)

  attempts = 0
  begin
    attempts += 1
    puts "Tweeting: #{tweet}"
    debug "--> Tweeting! (attempt: #{attempts}, tweet length: #{tweet.length} chars)"
    if client.update_with_media(tweet, File.open(runner.main_image, 'r'))
      puts "\t--> Tweet Sent!"
    end
  rescue Twitter::Error::ServerError,
         Twitter::Error::ClientError => e
    debug "Tweet FAILED! #{e.class} - #{e.message}"
    retry if attempts < TWITTER_RETRIES
    puts "ERROR: Tweet FAILED! (after #{attempts} attempts) - #{e.message}"
  end
end