Class: MemeGenerator::Campfire

Inherits:
Object
  • Object
show all
Defined in:
lib/meme_generator/campfire.rb

Constant Summary collapse

MEMEGEN_PATH =
File.expand_path("~/.memegen")
CONFIG_PATH =
File.join(MEMEGEN_PATH, ".campfire")

Class Method Summary collapse

Class Method Details

.configObject



9
10
11
12
# File 'lib/meme_generator/campfire.rb', line 9

def config
  return unless File.exists?(CONFIG_PATH)
  @config ||= read_config
end

.prompt_configObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/meme_generator/campfire.rb', line 14

def prompt_config
  require "readline"
  puts "Set your Campfire credentials..." unless config

  subdomain = Readline.readline("Subdomain : ").strip
  token     = Readline.readline("Token     : ").strip
  room      = Readline.readline("Room      : ").strip
  message   = Readline.readline("Message (optional) : ").strip

  write_config([subdomain, token, room, message])

  puts "Config saved successfully!"
end

.upload(path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/meme_generator/campfire.rb', line 28

def upload(path)
  prompt_config unless config

  puts "Uploading... "
  silence_stream(STDERR) do
    begin
      campfire = Tinder::Campfire.new config[:subdomain], :token => config[:token]
      room = campfire.rooms.detect { |room| room.name == config[:room] }
      room.speak(config[:message]) unless config[:message].nil? || config.empty?
      room.upload(path)
    rescue Tinder::AuthenticationFailed
      puts "Your campfire credentials are incorrect. Please enter them again."
      prompt_config
      upload(path)
    end
  end
  puts "Done!"
end