shoutbox-client 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/shoutbox/shout.rb +32 -13
- data/lib/shoutbox_client.rb +4 -1
- data/spec/shoutbox_client_spec.rb +24 -0
- metadata +3 -3
data/lib/shoutbox/shout.rb
CHANGED
@@ -7,30 +7,49 @@ module Shoutbox
|
|
7
7
|
parse_options
|
8
8
|
do_it
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
|
+
|
11
12
|
def parse_options
|
13
|
+
|
14
|
+
sub_commands = %w(red green destroy)
|
15
|
+
|
16
|
+
|
12
17
|
@conf = Trollop::options do
|
13
18
|
banner <<-EOS
|
14
|
-
usage: shout <
|
19
|
+
usage: shout <subcommand> <statusId>
|
20
|
+
|
21
|
+
<subcommand> must be one of red, green, remove
|
22
|
+
<statusId> is any given string you can think of
|
23
|
+
|
24
|
+
If you want to delete a status, you must provide
|
25
|
+
a message.
|
15
26
|
|
16
|
-
<status> must be one of red, green, remove
|
17
|
-
<statusId> is any given id you can think of
|
18
|
-
|
19
27
|
shout allows the folling options:
|
20
|
-
|
21
|
-
|
28
|
+
EOS
|
22
29
|
opt :host, "The hostname of the Shoutbox", :type => String
|
23
30
|
opt :port, "The port of the Shoutbox", :type => Integer
|
24
31
|
opt :group, "The group to use", :type => String, :default => "default"
|
25
|
-
|
32
|
+
|
33
|
+
stop_on sub_commands
|
26
34
|
end
|
27
|
-
|
28
|
-
Trollop::die "provide <status> and <statusId>" if ARGV.size != 2
|
35
|
+
|
29
36
|
@conf[:status] = ARGV.shift
|
30
|
-
Trollop::die "<status> must be one of 'green', 'red', 'destroy'" unless ShoutboxClient.valid_status?( @conf[:status] )
|
31
|
-
|
32
37
|
@conf[:statusId] = ARGV.shift
|
33
|
-
|
38
|
+
|
39
|
+
cmd_opts = case @conf[:status]
|
40
|
+
when "red"
|
41
|
+
@conf[:message] = ARGV.shift
|
42
|
+
Trollop::die "provide <statusId> and <message> when shouting red" if @conf[:message].to_s.empty?
|
43
|
+
when "green"
|
44
|
+
# noop
|
45
|
+
when "remove"
|
46
|
+
# noop
|
47
|
+
else
|
48
|
+
Trollop::die "unknown <subcommand> #{ @conf[:status] }"
|
49
|
+
end
|
50
|
+
|
51
|
+
Trollop::die "provide <status> and <statusId>" unless @conf[:status] and @conf[:statusId]
|
52
|
+
|
34
53
|
ShoutboxClient.configuration.host = @conf[:host] if @conf[:host]
|
35
54
|
ShoutboxClient.configuration.port = @conf[:port] if @conf[:port]
|
36
55
|
end
|
data/lib/shoutbox_client.rb
CHANGED
@@ -26,7 +26,10 @@ class ShoutboxClient
|
|
26
26
|
response = Net::HTTP.start(configuration.host, configuration.port) do |http|
|
27
27
|
req = Net::HTTP::Put.new( request_url(options) )
|
28
28
|
default_headers(req)
|
29
|
-
|
29
|
+
body_data = { :statusId => options[:statusId], :group => (options[:group] || 'default'), :status => options[:status].to_s }
|
30
|
+
body_data[:message] = options[:message] if options[:status] == :red
|
31
|
+
raise ArgumentError if (options[:status] == :red) and body_data[:message].to_s.empty?
|
32
|
+
req.body = body_data.to_json
|
30
33
|
http.request(req)
|
31
34
|
end
|
32
35
|
response.body == "OK"
|
@@ -40,6 +40,30 @@ describe "ShoutboxClient" do
|
|
40
40
|
ShoutboxClient.shout( :statusId => "test_status", :status => :green ).should == true
|
41
41
|
end
|
42
42
|
|
43
|
+
it 'should include a message when status is red' do
|
44
|
+
stub_request(:put, "http://localhost:3000/status").
|
45
|
+
with(:body => "{\"statusId\":\"test_status\",\"group\":\"default\",\"status\":\"red\",\"message\":\"This is what you should do now..\"}",
|
46
|
+
:headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby shoutbox-client'}).
|
47
|
+
to_return(:status => 200, :body => "OK", :headers => {})
|
48
|
+
|
49
|
+
ShoutboxClient.shout( :statusId => "test_status", :status => :red, :message => "This is what you should do now.." ).should == true
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should not include a message when status is green' do
|
53
|
+
stub_request(:put, "http://localhost:3000/status").
|
54
|
+
with(:body => "{\"statusId\":\"test_status\",\"group\":\"default\",\"status\":\"green\"}",
|
55
|
+
:headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby shoutbox-client'}).
|
56
|
+
to_return(:status => 200, :body => "OK", :headers => {})
|
57
|
+
|
58
|
+
ShoutboxClient.shout( :statusId => "test_status", :status => :green, :message => "This is what you should do now.." ).should == true
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'should deny red update if message is missin' do
|
62
|
+
lambda {
|
63
|
+
ShoutboxClient.shout( :statusId => "test_status", :status => :red )
|
64
|
+
}.should raise_error(ArgumentError)
|
65
|
+
end
|
66
|
+
|
43
67
|
it 'should delete a status' do
|
44
68
|
stub_request(:delete, "http://localhost:3000/status").
|
45
69
|
with(:body => "{\"statusId\":\"test_status\",\"group\":\"default\"}",
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 6
|
9
|
+
version: 0.0.6
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Benjamin Krause
|
@@ -134,7 +134,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
134
134
|
requirements:
|
135
135
|
- - ">="
|
136
136
|
- !ruby/object:Gem::Version
|
137
|
-
hash: -
|
137
|
+
hash: -1817083746782898519
|
138
138
|
segments:
|
139
139
|
- 0
|
140
140
|
version: "0"
|