Class: Lolcommits::LolTwitter

Inherits:
Plugin
  • Object
show all
Defined in:
lib/lolcommits/plugins/lol_twitter.rb

Constant Summary collapse

TWITTER_CONSUMER_KEY =
'qc096dJJCxIiqDNUqEsqQ'
TWITTER_CONSUMER_SECRET =
'rvjNdtwSr1H0TvBvjpk6c4bvrNydHmmbvv7gXZQI'
TWITTER_RETRIES =
2
TWITTER_PIN_REGEX =

4 or more digits

/^\d{4,}$/

Instance Attribute Summary

Attributes inherited from Plugin

#options, #runner

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

#configuration, #debug, #execute, #initialize, #is_enabled?, #puts, #valid_configuration?

Constructor Details

This class inherits a constructor from Lolcommits::Plugin

Class Method Details

.nameObject



121
122
123
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 121

def self.name
  'twitter'
end

Instance Method Details

#build_tweet(commit_message, tag = "#lolcommits") ⇒ Object



39
40
41
42
43
44
45
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 39

def build_tweet(commit_message, tag = "#lolcommits")
  available_commit_msg_size = max_tweet_size - (tag.length + 1)
  if commit_message.length > available_commit_msg_size
    commit_message = "#{commit_message[0..(available_commit_msg_size-3)]}..."
  end
  "#{commit_message} #{tag}"
end

#clientObject



108
109
110
111
112
113
114
115
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 108

def client
  @client ||= Twitter::Client.new(
    :consumer_key       => TWITTER_CONSUMER_KEY,
    :consumer_secret    => TWITTER_CONSUMER_SECRET,
    :oauth_token        => configuration['access_token'],
    :oauth_token_secret => configuration['secret']
  )
end

#configure_auth!Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 61

def configure_auth!
  puts "---------------------------"
  puts "Need to grab twitter tokens"
  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

  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(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

  if access_token.token && access_token.secret
    print "\n3) Thanks! Twitter Auth Succeeded\n"
    return { 'access_token' => access_token.token,
             'secret'       => access_token.secret }
  end
end

#configure_options!Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 47

def configure_options!
  options = super
  # ask user to configure tokens if enabling
  if options['enabled'] == true
    if auth_config = configure_auth!
      options.merge!(auth_config)
    else
      # return nil if configure_auth failed
      return
    end
  end
  return options
end

#is_configured?Boolean

Returns:

  • (Boolean)


102
103
104
105
106
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 102

def is_configured?
  !configuration['enabled'].nil? &&
    configuration['access_token'] &&
    configuration['secret']
end

#max_tweet_sizeObject



117
118
119
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 117

def max_tweet_size
  139 - client.configuration.characters_reserved_per_media
end

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 17

def run
  return unless valid_configuration?

  attempts = 0

  begin
    attempts += 1
    tweet = build_tweet(self.runner.message)
    puts "Tweeting: #{tweet}"
    debug "--> Tweeting! (attempt: #{attempts}, tweet size: #{tweet.length} chars)"
    if client.update_with_media(tweet, File.open(self.runner.main_image, 'r'))
      puts "\t--> Tweet Sent!"
    end
  rescue Twitter::Error::InternalServerError,
           Twitter::Error::BadRequest,
           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