class Lolcommits::Snapgit

Constants

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/snapgit.rb, line 163
def self.name
  'snapgit'
end
runner_order() click to toggle source
# File lib/lolcommits/plugins/snapgit.rb, line 167
def self.runner_order
  :postcapture
end

Public Instance Methods

client() click to toggle source
# File lib/lolcommits/plugins/snapgit.rb, line 136
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/snapgit.rb, line 155
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

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize

# File lib/lolcommits/plugins/snapgit.rb, line 74
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

  print "\n3) Your Gravatar email address: "
  gravatar_email = STDIN.gets.strip.downcase.to_s
  print "\n4) Your Gravatar password: "
  gravatar_password = STDIN.gets.strip.downcase.to_s

  print "\n5) Do you want to show the commit message on the picture? This is recommended for open source projects (y/n) "
  show_commit_messages = (STDIN.gets.strip == "y")

  puts ''
  puts '------------------------------'
  puts 'Successfully set up snapgit'
  puts '------------------------------'
  {
    'access_token' => access_token.token,
    'secret'       => access_token.secret,
    'email'        => gravatar_email,
    'password'     => gravatar_password,
    'show_commit_messages' => show_commit_messages
  }
end
configure_options!() click to toggle source
Calls superclass method Lolcommits::Plugin#configure_options!
# File lib/lolcommits/plugins/snapgit.rb, line 61
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)
  end
  options
end
configured?() click to toggle source

rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize

# File lib/lolcommits/plugins/snapgit.rb, line 127
def configured?
  !configuration['enabled'].nil? &&
    configuration['access_token'] &&
    configuration['secret'] &&
    # configuration['show_commit_messages'] && # we don't do that, since it might be false
    configuration['email'] &&
    configuration['password']
end
oauth_consumer() click to toggle source
# File lib/lolcommits/plugins/snapgit.rb, line 145
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/snapgit.rb, line 12
def run_postcapture
  return unless valid_configuration?

  upload_twitter
  upload_gravatar
end
upload_gravatar() click to toggle source
# File lib/lolcommits/plugins/snapgit.rb, line 38
def upload_gravatar
  return if configuration['email'].to_s.empty?
  return if configuration['password'].to_s.empty?

  puts 'Uploading to Gravatar...'

  # First we need to follow the redirects
  url = "https://twitter.com/#{@twitter_user}/profile_image?size=original"
  url = `curl -I #{url}`.match(/location: (.*)/)[1].strip

  require 'gravatar-ultimate'

  api = Gravatar.new(configuration['email'], :password => configuration['password'])
  raise 'Could not login to Gravatar' unless api.exists? && api.addresses.count > 0

  handle = api.save_url!(0, url) # upload the image (0 being the rating)

  api.addresses.each do |email, _value|
    api.use_user_image!(handle, email) # set it for all available email addresses
    puts "Successfully updated Gravatar image for '#{email}' 🔑"
  end
end
upload_twitter() click to toggle source
# File lib/lolcommits/plugins/snapgit.rb, line 19
def upload_twitter
  require 'twitter'

  attempts = 0
  begin
    attempts += 1
    puts 'Updating profile picture...'
    image = File.open(runner.main_image)
    client.update_profile_image(image)
    @twitter_user = client.user.screen_name # to be used with gravatar
    puts "Successfully uploaded new profile picture 🌴"
  rescue Twitter::Error::ServerError,
         Twitter::Error::ClientError => e
    debug "Upading avatar failed! #{e.class} - #{e.message}"
    retry if attempts < TWITTER_RETRIES
    puts "ERROR: Updating avatar FAILED! (after #{attempts} attempts) - #{e.message}"
  end
end