Class: Lolcommits::LolTwitter

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

Instance Attribute Summary

Attributes inherited from Plugin

#default, #name, #options, #runner

Instance Method Summary collapse

Methods inherited from Plugin

#configuration, #execute, #is_enabled?, #plugdebug

Constructor Details

#initialize(runner) ⇒ LolTwitter



16
17
18
19
20
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 16

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

Instance Method Details

#initial_twitter_authObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 22

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

#runObject



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
101
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 66

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