fnichol/campy

View on GitHub
bin/campy

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby

$:.unshift File.join(File.dirname(__FILE__), %w{.. lib})
require 'campy'
require 'yaml'

def usage
  <<-USAGE.gsub(/^ {4}/, '')

    Usage

      campy <action> <message>

    Actions

      help  - Display CLI help (this output)
      speak - Speak a message into the campfire room
      paste - Paste a message into the campfire room
      play  - Play a sound into the campfire room

  USAGE
end

def options
  yaml_file = File.expand_path(ENV['CAMPFIRE_YAML_FILE'] || '~/.campfire.yml')
  if !File.exists?(yaml_file)
    abort "File '#{yaml_file}' does not exist with campfire configuration."
  end
  YAML.load_file(yaml_file)
end

def actions
  %w{speak paste play}
end

def action
  @action ||= begin
    abort usage if ARGV.empty?
    ARGV.shift
  end
end

def message
  @message ||= begin
    if ARGV.empty?
      ARGF.read.chomp
    else
      ARGV.shift
    end
 end
end

case action
when *actions
  Campy::Room.new(options).send(action, message)
when "help"
  puts usage
  exit
else
  abort usage
end