Class: Lolcommits::Plugin::Twitter

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

Constant Summary collapse

DEFAULT_SUFFIX =
'#lolcommits'.freeze

Instance Method Summary collapse

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lolcommits/plugin/twitter.rb', line 29

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.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lolcommits/plugin/twitter.rb', line 52

def run_capture_ready
  if runner.capture_video && !runner.capture_gif
    debug "unable to post lolcommit videos, (gif's and jpgs only)"
    return
  end

  status = build_tweet(runner.message)
  file   = File.open(image_path, '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



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

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