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

#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