Class: GridCLI::MessageCommand

Inherits:
BaseCommand show all
Defined in:
lib/gridcli/commands/message.rb

Instance Attribute Summary

Attributes inherited from BaseCommand

#cmd, #desc

Instance Method Summary collapse

Methods inherited from BaseCommand

#add_format_option, #add_option, #error, #log, #output_format, #parse_dates, #parse_opts, #pop_arg, #pprint

Constructor Details

#initializeMessageCommand

Returns a new instance of MessageCommand.



6
7
8
9
10
11
12
# File 'lib/gridcli/commands/message.rb', line 6

def initialize
  super "message", "Send a message to a friend or group of friends."
  add_option("-s subject", "--subject subject", "Subject for message") { |s| @opts[:subject] = s }
  add_option("-f file", "--file file", "File to use for message body") { |f| @opts[:file] = f }
  add_option("-b body", "--body body", "Message body. If -b/-f aren't used, $EDITOR will be opened.") { |b| @opts[:body] = b }
  add_format_option
end

Instance Method Details

#run(args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gridcli/commands/message.rb', line 18

def run(args)
  usage if args.length == 0
  recipients = args.shift
  parse_opts args

  body = nil
  if not @opts[:file].nil?
    error("File '#{@opts[:file]}' does not exist.") unless File.exists? @opts[:file]
    body = File.read(@opts[:file])
  elsif not @opts[:body].nil?
    body = @opts[:body]
  else
    editor = ENV['EDITOR'] || 'vi'
    tfile = Tempfile.new('gridmessage')
    tfile.close
    system("#{editor} #{tfile.path}")
    body = File.read(tfile.path)
    tfile.unlink
  end
  
  error("Body cannot be blank. Use one of -f or -b") if body.nil?
  subject = @opts[:subject].nil? ? body.truncate(30) : @opts[:subject]

  begin
    log "Trying to send a message '#{subject}' to '#{recipients}'"
    post = Post::Message.create :subject => subject, :body => body, :recipients => recipients, :posttype => 'message'
    puts "Message to #{recipients} send successfully."
  rescue ActiveResource::ClientError
    puts "There was an error sending your message.  Please make sure everyone in your recipient list is a friend, and the message body isn't empty."
    return
  end
  
end

#usageObject



14
15
16
# File 'lib/gridcli/commands/message.rb', line 14

def usage
  super "<username>[,<username>,...]"
end