Class: Lolcommits::Snapgit
- Inherits:
-
Plugin
- Object
- Plugin
- Lolcommits::Snapgit
show all
- Defined in:
- lib/lolcommits/plugins/snapgit.rb
Constant Summary
collapse
'https://api.twitter.com'.freeze
'qc096dJJCxIiqDNUqEsqQ'.freeze
'rvjNdtwSr1H0TvBvjpk6c4bvrNydHmmbvv7gXZQI'.freeze
2
/^\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, #enabled?, #execute_postcapture, #execute_precapture, #initialize, #log_error, #parse_user_input, #puts, #run_precapture, #valid_configuration?
Class Method Details
.name ⇒ Object
164
165
166
|
# File 'lib/lolcommits/plugins/snapgit.rb', line 164
def self.name
'snapgit'
end
|
.runner_order ⇒ Object
168
169
170
|
# File 'lib/lolcommits/plugins/snapgit.rb', line 168
def self.runner_order
:postcapture
end
|
Instance Method Details
#client ⇒ Object
137
138
139
140
141
142
143
144
|
# File 'lib/lolcommits/plugins/snapgit.rb', line 137
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
156
157
158
159
160
161
162
|
# File 'lib/lolcommits/plugins/snapgit.rb', line 156
def config_with_default(key, default = nil)
if configuration[key]
configuration[key].strip.empty? ? default : configuration[key]
else
default
end
end
|
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize
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
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/lolcommits/plugins/snapgit.rb', line 75
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
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
|
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/lolcommits/plugins/snapgit.rb', line 62
def configure_options!
options = super
if options['enabled']
auth_config = configure_auth!
return unless auth_config
options = options.merge(auth_config)
end
options
end
|
rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize
128
129
130
131
132
133
134
135
|
# File 'lib/lolcommits/plugins/snapgit.rb', line 128
def configured?
!configuration['enabled'].nil? &&
configuration['access_token'] &&
configuration['secret'] &&
configuration['email'] &&
configuration['password']
end
|
#run_postcapture ⇒ Object
13
14
15
16
17
18
|
# File 'lib/lolcommits/plugins/snapgit.rb', line 13
def run_postcapture
return unless valid_configuration?
upload_gravatar
end
|
#upload_gravatar ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/lolcommits/plugins/snapgit.rb', line 39
def upload_gravatar
return if configuration['email'].to_s.empty?
return if configuration['password'].to_s.empty?
puts 'Uploading to Gravatar...'
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)
api.addresses.each do |email, _value|
api.use_user_image!(handle, email) puts "Successfully updated Gravatar image for '#{email}' 🔑"
end
end
|
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/lolcommits/plugins/snapgit.rb', line 20
def
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 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
|