Class: Sendhipchat::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/sendhipchat/runner.rb

Constant Summary collapse

YELLOW =
'yellow'
RED =
'red'
GREEN =
'green'
PURPLE =
'purple'
BLUE =
'blue'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Runner

Returns a new instance of Runner.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sendhipchat/runner.rb', line 16

def initialize(argv)
  @argv = argv
  @options = {
    :api_token => nil,
    :rooms     => [],
    :from      => nil,
    :notify    => false,
    :color     => YELLOW
  }

  parse!
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



14
15
16
# File 'lib/sendhipchat/runner.rb', line 14

def options
  @options
end

Instance Method Details

#parse!Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sendhipchat/runner.rb', line 76

def parse!
  parser.parse! @argv

  if @options[:api_token] == nil
    puts "ERROR: Must specify API token."
    puts
    puts @parser
    exit 1
  elsif @options[:from] == nil
    puts 'ERROR: Must provide a "from" alias.'
    puts
    puts @parser
    exit 1
  elsif @options[:rooms].empty?
    puts 'ERROR: Must specify at least one room.'
    puts
    puts @parser
    exit 1
  end
end

#parserObject



29
30
31
32
33
34
35
36
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
70
71
72
73
74
# File 'lib/sendhipchat/runner.rb', line 29

def parser
  @parser ||= OptionParser.new do |opts|
    opts.banner = <<-EOS
Usage: sendhipchat [options]

Messages are read in from STDIN
    EOS
    opts.separator ''
    opts.on('-a TOKEN', '--api-token TOKEN', 'HipChat API token.') do |tk|
      @options[:api_token] = tk
    end
    opts.on('-f ALIAS', '--from ALIAS', 'Message "from".') do |f|
      if f.length > 15
        puts 'ERROR: from name must be <= 15 characters'
        puts
        puts @parser
        exit 1
      end
      @options[:from] = f
    end
    opts.on('--color COLOR', 'Message color: (yellow|red|blue|green|purple)') do |c|
      if color_valid?(c)
        @options[:color] = color_value(c)
      else
        puts 'ERROR: not a valid color choice'
        puts
        puts opts
        exit 1
      end
    end
    opts.on('--notify', 'Alert room members to message.') do
      @options[:notify] = true
    end
    opts.on('--rooms ROOMS', 'Comma separated room names.') do |rooms|
      @options[:rooms] = rooms.split(',').map { |r| r.strip }
    end
    opts.separator ''
    opts.on_tail('-h', '--help', 'Show this message.') do
      puts opts
      exit 0
    end
    opts.on_tail('-v', '--version', 'Show version') do
      puts Sendhipchat::VERSION; exit
    end
  end
end

#run!Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/sendhipchat/runner.rb', line 97

def run!
  hp = HipChat::API.new(@options[:api_token])

  rooms_to_id = rooms_flip(hp.rooms_list['rooms'])

  rooms = rooms_to_id.keys
  if !( (@options[:rooms] - rooms).empty? )
    puts 'ERROR: Only these rooms exist.'
    puts
    rooms.each do |r|
      puts r
    end
    puts
    puts @parser
    exit 1
  end

  msg = STDIN.read
  if msg.length > 5000
    puts 'ERROR: message must be <= 5000 characters'
    puts
    puts @parser
    exit 1
  end

  @options[:rooms].each do |r|
    hp.rooms_message(room_id=rooms_to_id[r], from=@options[:from],
      message=msg, notify=@options[:notify], color=@options[:color])
  end
end