Class: Lolcommits::Plugin::LolTumblr

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

Constant Summary collapse

TUMBLR_API_ENDPOINT =
'https://www.tumblr.com'.freeze
TUMBLR_CONSUMER_KEY =
'2FtMEDpEPkxjoUdkpHh42h9wqTu9IVS7Ra0QyNZGixdCvhllN2'.freeze
TUMBLR_CONSUMER_SECRET =
'qWuvxgFUR2YyWKtbWOkDTMAiBEbj7ZGaNLaNQPba0PI1N4JpBs'.freeze

Instance Attribute Summary

Attributes inherited from Base

#options, #runner

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#configuration, #debug, #enabled?, #execute_postcapture, #execute_precapture, #initialize, #log_error, #parse_user_input, #puts, #run_precapture, #valid_configuration?

Constructor Details

This class inherits a constructor from Lolcommits::Plugin::Base

Class Method Details

.nameObject



109
110
111
# File 'lib/lolcommits/plugin/lol_tumblr.rb', line 109

def self.name
  'tumblr'
end

.runner_orderObject



113
114
115
# File 'lib/lolcommits/plugin/lol_tumblr.rb', line 113

def self.runner_order
  :postcapture
end

Instance Method Details

#clientObject



82
83
84
85
86
87
88
89
# File 'lib/lolcommits/plugin/lol_tumblr.rb', line 82

def client
  @client ||= Tumblr.new(
    consumer_key: TUMBLR_CONSUMER_KEY,
    consumer_secret: TUMBLR_CONSUMER_SECRET,
    oauth_token: configuration['access_token'],
    oauth_token_secret: configuration['secret']
  )
end

#config_with_default(key, default = nil) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/lolcommits/plugin/lol_tumblr.rb', line 101

def config_with_default(key, default = nil)
  if configuration[key]
    configuration[key].strip.empty? ? default : configuration[key]
  else
    default
  end
end

#configure_auth!Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/lolcommits/plugin/lol_tumblr.rb', line 37

def configure_auth!
  puts '---------------------------'
  puts 'Need to grab tumblr tokens'
  puts '---------------------------'

  request_token = oauth_consumer.get_request_token(exclude_callback: true)
  print "\n1) Please open this url in your browser to authorize lolcommits:\n\n"
  puts request_token.authorize_url
  print "\n2) Launching a local server to complete the OAuth authentication process:\n\n"
  begin
    server = WEBrick::HTTPServer.new Port: 3000
    server.mount_proc '/', server_callback(server)
    server.start
    debug "Requesting Tumblr OAuth Token with verifier: #{@verifier}"
    access_token = request_token.get_access_token(oauth_verifier: @verifier)
  rescue Errno::EADDRINUSE
    puts "\nERROR You have something running on port 3000. Please turn it off to complete the authorization process"
    return
  rescue OAuth::Unauthorized
    puts "\nERROR: Tumblr OAuth verification faile!"
    return
  end
  return unless access_token.token && access_token.secret
  puts ''
  puts '------------------------------'
  puts 'Thanks! Tumblr Auth Succeeded'
  puts '------------------------------'

  {
    'access_token' => access_token.token,
    'secret'       => access_token.secret
  }
end

#configure_options!Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/lolcommits/plugin/lol_tumblr.rb', line 26

def configure_options!
  options = super
  # ask user to configure tokens if enabling
  if options['enabled']
    auth_config = configure_auth!
    return unless auth_config
    options = options.merge(auth_config).merge(configure_tumblr_name)
  end
  options
end

#configure_tumblr_nameObject



71
72
73
74
# File 'lib/lolcommits/plugin/lol_tumblr.rb', line 71

def configure_tumblr_name
  print "\n3) What's your tumblr name? (i.e. 'http://[THIS PART HERE].tumblr.com'): "
  { 'tumblr_name' => gets.strip }
end

#configured?Boolean

Returns:

  • (Boolean)


76
77
78
79
80
# File 'lib/lolcommits/plugin/lol_tumblr.rb', line 76

def configured?
  !configuration['enabled'].nil? &&
    configuration['access_token'] &&
    configuration['secret']
end

#oauth_consumerObject



91
92
93
94
95
96
97
98
99
# File 'lib/lolcommits/plugin/lol_tumblr.rb', line 91

def oauth_consumer
  @oauth_consumer ||= OAuth::Consumer.new(
    TUMBLR_CONSUMER_KEY,
    TUMBLR_CONSUMER_SECRET,
    site: TUMBLR_API_ENDPOINT,
    request_endpoint: TUMBLR_API_ENDPOINT,
    http_method: :get
  )
end

#run_postcaptureObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/lolcommits/plugin/lol_tumblr.rb', line 14

def run_postcapture
  puts 'Posting to Tumblr'
  r = client.photo(configuration['tumblr_name'], data: runner.main_image)
  if r.key?('id')
    puts "\t--> Post successful!"
  else
    puts "Tumblr post FAILED! #{r}"
  end
rescue Faraday::Error => e
  puts "Tumblr post FAILED! #{e.message}"
end