Class: Lolcommits::LolTwitter
- Inherits:
-
Plugin
- Object
- Plugin
- Lolcommits::LolTwitter
show all
- Defined in:
- lib/lolcommits/plugins/lol_twitter.rb
Constant Summary
collapse
'https://api.twitter.com'.freeze
'qc096dJJCxIiqDNUqEsqQ'.freeze
'rvjNdtwSr1H0TvBvjpk6c4bvrNydHmmbvv7gXZQI'.freeze
2
/^\d{4,}$/
- DEFAULT_SUFFIX =
'#lolcommits'.freeze
Instance Attribute Summary
Attributes inherited from Plugin
#options, #runner
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Plugin
#configuration, #debug, #enabled?, #execute_postcapture, #execute_precapture, #initialize, #log_error, #parse_user_input, #puts, #run_precapture, #valid_configuration?
Class Method Details
.name ⇒ Object
147
148
149
|
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 147
def self.name
'twitter'
end
|
.runner_order ⇒ Object
151
152
153
|
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 151
def self.runner_order
:postcapture
end
|
Instance Method Details
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 35
def (commit_message)
prefix = config_with_default('prefix', '')
suffix = " #{config_with_default('suffix', DEFAULT_SUFFIX)}"
prefix = "#{prefix} " unless prefix.empty?
available_commit_msg_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 ⇒ Object
116
117
118
119
120
121
122
123
|
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 116
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) ⇒ Object
135
136
137
138
139
140
141
|
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 135
def config_with_default(key, default = nil)
if configuration[key]
configuration[key].strip.empty? ? default : configuration[key]
else
default
end
end
|
59
60
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
|
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 59
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: "
= STDIN.gets.strip.downcase.to_s
unless =~ TWITTER_PIN_REGEX
puts "\nERROR: '#{}' is not a valid Twitter Auth PIN"
return
end
begin
debug "Requesting Twitter OAuth Token with PIN: #{}"
OAuth::RequestToken.new(oauth_consumer, rtoken, rsecret)
access_token = request_token.get_access_token(:oauth_verifier => )
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
|
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 48
def configure_options!
options = super
if options['enabled']
auth_config = configure_auth!
return unless auth_config
options = options.merge(auth_config).merge(configure_prefix_suffix)
end
options
end
|
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 98
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
|
110
111
112
113
114
|
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 110
def configured?
!configuration['enabled'].nil? &&
configuration['access_token'] &&
configuration['secret']
end
|
143
144
145
|
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 143
def
139 - client.configuration.characters_reserved_per_media
end
|
#run_postcapture ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/lolcommits/plugins/lol_twitter.rb', line 15
def run_postcapture
return unless valid_configuration?
= (runner.message)
attempts = 0
begin
attempts += 1
puts "Tweeting: #{}"
debug "--> Tweeting! (attempt: #{attempts}, tweet length: #{.length} chars)"
if client.update_with_media(, 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
|