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

.nameString

Returns the name of the plugin. Identifies the plugin to lolcommits.

Returns:

  • (String)

    the plugin name



16
17
18
# File 'lib/lolcommits/plugin/twitter.rb', line 16

def self.name
  'twitter'
end

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



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

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/lolcommits/plugin/twitter.rb', line 46

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

#configured?Boolean

Plugin is configured when a token and token secret exist

Returns:

  • (Boolean)

    true/false if the plugin has been configured



36
37
38
# File 'lib/lolcommits/plugin/twitter.rb', line 36

def configured?
  !!(configuration['token'] && configuration['token_secret'])
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.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/lolcommits/plugin/twitter.rb', line 69

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