Class: Lolcommits::Plugin::Twitter

Inherits:
Base
  • Object
show all
Defined in:
lib/lolcommits/plugin/twitter.rb

Constant Summary collapse

DEFAULT_SUFFIX =
'#lolcommits'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.runner_orderArray

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).

Returns:

  • (Array)

    the position(s)



18
19
20
# File 'lib/lolcommits/plugin/twitter.rb', line 18

def self.runner_order
  [:capture_ready]
end

Instance Method Details

#configure_options!Hash

Prompts the user to configure plugin options. Options are enabled (true/false), Twitter auth, and prefix/suffix text.

Returns:

  • (Hash)

    a hash of configured plugin options



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lolcommits/plugin/twitter.rb', line 37

def configure_options!
  options = super
  # ask user to configure all options (if enabling)
  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)
  else
    # retain config when disabling
    options = configuration.merge(options)
  end
  options
end

#run_capture_readyObject

Capture ready hook, runs after lolcommits captures a snapshot and image processing has completed.

Posts the lolcommit to Twitter, first uploading the capture media, then posting a new Tweet with the media_id attached.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/lolcommits/plugin/twitter.rb', line 60

def run_capture_ready
  status = build_tweet(runner.message)
  file   = File.open(runner.main_image, 'rb')

  print "Tweeting ... "

  begin
    client = twitter_client.new(
      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]
  rescue StandardError => e
    puts "ERROR: Tweeting FAILED! - #{e.message}"
  end
end

#valid_configuration?Boolean

Indicate if the plugin is configured correctly.

Returns:

  • (Boolean)

    true/false



27
28
29
# File 'lib/lolcommits/plugin/twitter.rb', line 27

def valid_configuration?
  !!(configuration[:token] && configuration[:token_secret])
end