Class: Lolcommits::Plugin::LolTwitter
- Inherits:
-
Base
- Object
- Base
- Lolcommits::Plugin::LolTwitter
show all
- Defined in:
- lib/lolcommits/plugin/lol_twitter.rb
Constant Summary
collapse
'https://api.twitter.com'.freeze
'qc096dJJCxIiqDNUqEsqQ'.freeze
'rvjNdtwSr1H0TvBvjpk6c4bvrNydHmmbvv7gXZQI'.freeze
24
2
/^\d{4,}$/
- DEFAULT_SUFFIX =
'#lolcommits'.freeze
Instance Attribute Summary
Attributes inherited from Base
#options, #runner
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#configuration, #debug, #enabled?, #execute_postcapture, #execute_precapture, #initialize, #log_error, #parse_user_input, #puts, #run_precapture, #valid_configuration?
Class Method Details
.name ⇒ Object
167
168
169
|
# File 'lib/lolcommits/plugin/lol_twitter.rb', line 167
def self.name
'twitter'
end
|
.runner_order ⇒ Object
171
172
173
|
# File 'lib/lolcommits/plugin/lol_twitter.rb', line 171
def self.runner_order
:postcapture
end
|
Instance Method Details
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/lolcommits/plugin/lol_twitter.rb', line 50
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
|
#config_with_default(key, default = nil) ⇒ Object
155
156
157
158
159
160
161
|
# File 'lib/lolcommits/plugin/lol_twitter.rb', line 155
def config_with_default(key, default = nil)
if configuration[key]
configuration[key].strip.empty? ? default : configuration[key]
else
default
end
end
|
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
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/lolcommits/plugin/lol_twitter.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: "
= gets.strip.downcase.to_s
unless =~
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: )
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
|
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/lolcommits/plugin/lol_twitter.rb', line 63
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
|
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/lolcommits/plugin/lol_twitter.rb', line 113
def configure_prefix_suffix
print "\n3) Prefix all tweets with something? e.g. @user (leave blank for no prefix): "
prefix = gets.strip
print "\n4) End all tweets with something? e.g. #hashtag (leave blank for default suffix #{DEFAULT_SUFFIX}): "
suffix = gets.strip
config = {}
config['prefix'] = prefix unless prefix.empty?
config['suffix'] = suffix unless suffix.empty?
config
end
|
125
126
127
128
129
|
# File 'lib/lolcommits/plugin/lol_twitter.rb', line 125
def configured?
!configuration['enabled'].nil? &&
configuration['access_token'] &&
configuration['secret']
end
|
163
164
165
|
# File 'lib/lolcommits/plugin/lol_twitter.rb', line 163
def
139 -
end
|
#oauth_consumer ⇒ Object
145
146
147
148
149
150
151
152
153
|
# File 'lib/lolcommits/plugin/lol_twitter.rb', line 145
def oauth_consumer
@oauth_consumer ||= OAuth::Consumer.new(
,
,
site: ,
request_endpoint: ,
sign_in: true
)
end
|
#oauth_credentials ⇒ Object
136
137
138
139
140
141
142
143
|
# File 'lib/lolcommits/plugin/lol_twitter.rb', line 136
def oauth_credentials
{
consumer_key: ,
consumer_secret: ,
token: configuration['access_token'],
token_secret: configuration['secret']
}
end
|
131
132
133
134
|
# File 'lib/lolcommits/plugin/lol_twitter.rb', line 131
def
uri = Addressable::URI.parse(post_url)
SimpleOAuth::.new(:post, uri, {}, oauth_credentials)
end
|
40
41
42
43
44
45
46
47
48
|
# File 'lib/lolcommits/plugin/lol_twitter.rb', line 40
def (status, media)
RestClient.post(
post_url,
{
'media[]' => media,
'status' => status
}, Authorization:
)
end
|
#post_url ⇒ Object
34
35
36
37
38
|
# File 'lib/lolcommits/plugin/lol_twitter.rb', line 34
def post_url
@post_url ||= + '/1.1/statuses/update_with_media.json'
end
|
#run_postcapture ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/lolcommits/plugin/lol_twitter.rb', line 18
def run_postcapture
= (runner.message)
attempts = 0
begin
attempts += 1
puts "Tweeting: #{tweet}"
debug "--> Tweeting! (attempt: #{attempts}, tweet length: #{tweet.length} chars)"
(, File.open(runner.main_image, 'r'))
rescue StandardError => e
debug "Tweet FAILED! #{e.class} - #{e.message}"
retry if attempts <
puts "ERROR: Tweet FAILED! (after #{attempts} attempts) - #{e.message}"
end
end
|