lib/lolcommits/plugin/twitter.rb in lolcommits-twitter-0.0.4 vs lib/lolcommits/plugin/twitter.rb in lolcommits-twitter-0.0.5
- old
+ new
@@ -7,19 +7,10 @@
class Twitter < Base
DEFAULT_SUFFIX = '#lolcommits'.freeze
##
- # Returns the name of the plugin. Identifies the plugin to lolcommits.
- #
- # @return [String] the plugin name
- #
- def self.name
- 'twitter'
- end
-
- ##
# Returns position(s) of when this plugin should run during the capture
# process. We want to post to Twitter when the capture is ready (after all
# image processing is complete).
#
# @return [Array] the position(s)
@@ -27,16 +18,16 @@
def self.runner_order
[:capture_ready]
end
##
- # Plugin is configured when a token and token secret exist
+ # Indicate if the plugin is configured correctly.
#
- # @return [Boolean] true/false if the plugin has been configured
+ # @return [Boolean] true/false
#
- def configured?
- !!(configuration['token'] && configuration['token_secret'])
+ def valid_configuration?
+ !!(configuration[:token] && configuration[:token_secret])
end
##
# Prompts the user to configure plugin options.
# Options are enabled (true/false), Twitter auth, and prefix/suffix text.
@@ -44,11 +35,11 @@
# @return [Hash] a hash of configured plugin options
#
def configure_options!
options = super
# ask user to configure all options (if enabling)
- if options['enabled']
+ if options[:enabled]
auth_config = configure_auth!
return unless auth_config
options = options.merge(auth_config).
merge(configure_prefix_suffix).
merge(configure_open_tweet_url)
@@ -72,22 +63,22 @@
print "Tweeting ... "
begin
client = twitter_client.new(
- configuration['token'],
- configuration['token_secret']
+ configuration[:token],
+ configuration[:token_secret]
)
debug "--> Uploading media (#{file.size} bytes)"
media_id = client.upload_media(file)
debug "--> Posting status update (#{status.length} chars, media_id: #{media_id})"
status_response = client.update_status(status, media_ids: [media_id])
tweet_url = status_response['entities']['media'][0]['url']
print "#{tweet_url}\n"
- open_url(tweet_url) if configuration['open_tweet_url']
+ open_url(tweet_url) if configuration[:open_tweet_url]
rescue StandardError => e
puts "ERROR: Tweeting FAILED! - #{e.message}"
end
end
@@ -96,14 +87,14 @@
def twitter_client
Lolcommits::Twitter::Client
end
def build_tweet(commit_message)
- prefix = configuration['prefix'].to_s
- suffix = configuration['suffix'].to_s
- prefix = "#{configuration['prefix']} " unless prefix.empty?
- suffix = " #{configuration['suffix']}" unless suffix.empty?
+ prefix = configuration[:prefix].to_s
+ suffix = configuration[:suffix].to_s
+ prefix = "#{configuration[:prefix]} " unless prefix.empty?
+ suffix = " #{configuration[:suffix]}" unless suffix.empty?
available_commit_msg_size = twitter_client::MAX_TWEET_CHARS - (prefix.length + suffix.length)
if commit_message.length > available_commit_msg_size
commit_message = "#{commit_message[0..(available_commit_msg_size - 3)]}..."
end
@@ -116,23 +107,21 @@
return default if yes_or_no.nil?
!!(yes_or_no =~ /^y/i)
end
def configure_auth!
- if configured?
+ if valid_configuration?
print "\n* Reset Twitter Auth ? (y/N): "
- return configuration.select {|k,v| k =~ /^token/ } if !ask_yes_or_no?
+ return configuration.select {|k,v| k.to_s =~ /^token/ } if !ask_yes_or_no?
end
puts ''
puts '-----------------------------------'
puts ' OK, lets setup Twitter Auth '
puts '-----------------------------------'
request_token = twitter_client.oauth_consumer.get_request_token
- rtoken = request_token.token
- rsecret = request_token.secret
authorize_url = request_token.authorize_url
open_url(authorize_url)
print "\n* Grab a PIN from this url:\n\n"
puts " #{authorize_url}"
@@ -154,29 +143,29 @@
puts '-----------------------------------'
puts ' Thanks, Twitter Auth Succeeded! '
puts '-----------------------------------'
{
- 'token' => access_token.token,
- 'token_secret' => access_token.secret
+ token: access_token.token,
+ token_secret: access_token.secret
}
end
def configure_prefix_suffix
print "\n* Prefix all tweets with something? e.g. @user (default: nothing): "
prefix = gets.strip
print "\n* End all tweets with something? e.g. #hashtag (default: #{DEFAULT_SUFFIX}): "
suffix = gets.strip
config = {}
- config['prefix'] = prefix.empty? ? '' : prefix
- config['suffix'] = suffix.empty? ? DEFAULT_SUFFIX : suffix
+ config[:prefix] = prefix.empty? ? '' : prefix
+ config[:suffix] = suffix.empty? ? DEFAULT_SUFFIX : suffix
config
end
def configure_open_tweet_url
print "\n* Automatically open Tweet URL after posting (y/N): "
- { 'open_tweet_url' => ask_yes_or_no? }
+ { open_tweet_url: ask_yes_or_no? }
end
def open_url(url)
Lolcommits::CLI::Launcher.open_url(url)
end