shoutbox-client 0.0.15 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +7 -2
- data/lib/shoutbox/configuration.rb +3 -1
- data/lib/shoutbox/shout.rb +10 -9
- data/lib/shoutbox_client.rb +8 -6
- data/spec/shoutbox_client_spec.rb +33 -22
- metadata +3 -3
data/README.md
CHANGED
@@ -8,12 +8,15 @@ Examples
|
|
8
8
|
|
9
9
|
You can use it via ruby
|
10
10
|
|
11
|
-
ruby-1.9.2-p0 > ShoutboxClient.shout :
|
11
|
+
ruby-1.9.2-p0 > ShoutboxClient.shout :name => "My Rake Task", :status => :red, :message => 'failed miserably'
|
12
12
|
true
|
13
|
+
|
14
|
+
ruby-1.9.2-p0 > ShoutboxClient.shout :name => "My Other Task", :status => :green, :expires_in => 1.hour
|
15
|
+
true
|
13
16
|
|
14
17
|
or as a shell script
|
15
18
|
|
16
|
-
benjamin@Jiji % shout green important
|
19
|
+
benjamin@Jiji % shout green 'important task'
|
17
20
|
|
18
21
|
Configuration
|
19
22
|
---
|
@@ -23,6 +26,8 @@ in YAML format, like this
|
|
23
26
|
|
24
27
|
host: localhost
|
25
28
|
port: 3001
|
29
|
+
auth_token: my-shoutbox-auth-token
|
30
|
+
|
26
31
|
|
27
32
|
Otherwise you can set the configuration manually via ruby
|
28
33
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Shoutbox
|
2
2
|
class Configuration
|
3
|
-
attr_accessor :host, :port, :proxy_host, :proxy_port, :default_group
|
3
|
+
attr_accessor :host, :port, :proxy_host, :proxy_port, :default_group, :auth_token
|
4
4
|
|
5
5
|
def initialize
|
6
6
|
config_file_exists? ? read_config_file : default_config
|
@@ -25,6 +25,7 @@ module Shoutbox
|
|
25
25
|
@host = 'localhost'
|
26
26
|
@port = 3000
|
27
27
|
@default_group = 'default group'
|
28
|
+
@auth_token = 'invalid-auth-token'
|
28
29
|
@proxy_host = nil
|
29
30
|
@proxy_port = nil
|
30
31
|
end
|
@@ -34,6 +35,7 @@ module Shoutbox
|
|
34
35
|
@host = config_data["host"]
|
35
36
|
@port = config_data["port"]
|
36
37
|
@default_group = config_data["default_group"] || 'default group'
|
38
|
+
@auth_token = config_data["auth_token"]
|
37
39
|
@proxy_host = config_data["proxy_host"]
|
38
40
|
@proxy_port = config_data["proxy_port"]
|
39
41
|
end
|
data/lib/shoutbox/shout.rb
CHANGED
@@ -12,10 +12,10 @@ module Shoutbox
|
|
12
12
|
def parse_options
|
13
13
|
@conf = Trollop::options do
|
14
14
|
banner <<-EOS
|
15
|
-
usage: shout <subcommand> <
|
15
|
+
usage: shout <subcommand> <name>
|
16
16
|
|
17
17
|
<subcommand> must be one of red, green, remove
|
18
|
-
<
|
18
|
+
<name> is any given string you can think of
|
19
19
|
|
20
20
|
If you want to delete a status, you must provide
|
21
21
|
a message.
|
@@ -27,33 +27,34 @@ EOS
|
|
27
27
|
opt :proxy_host, "The proxy host to use", :type => String
|
28
28
|
opt :proxy_port, "The proxy port to use", :type => Integer
|
29
29
|
opt :group, "The group to use", :type => String
|
30
|
+
opt :expires_in, "Number of seconds this status should expire in", :type => Integer
|
30
31
|
end
|
31
32
|
|
32
|
-
@conf[:status]
|
33
|
-
@conf[:
|
33
|
+
@conf[:status] = ARGV.shift
|
34
|
+
@conf[:name] = ARGV.shift
|
35
|
+
@conf[:message] = ARGV.shift
|
34
36
|
|
35
37
|
case @conf[:status]
|
36
38
|
when "red"
|
37
|
-
@conf[:message]
|
38
|
-
Trollop::die "provide <statusId> and <message> when shouting red" if @conf[:message].to_s.empty?
|
39
|
+
Trollop::die "provide <name> and <message> when shouting red" if @conf[:message].to_s.empty?
|
39
40
|
when "green"
|
40
41
|
# noop
|
41
42
|
when "yellow"
|
42
|
-
|
43
|
+
Trollop::die "provide <name> and <message> when shouting red" if @conf[:message].to_s.empty?
|
43
44
|
when "remove"
|
44
45
|
# noop
|
45
46
|
else
|
46
47
|
Trollop::die "unknown <subcommand> #{ @conf[:status] }"
|
47
48
|
end
|
48
49
|
|
49
|
-
Trollop::die "provide <status> and <
|
50
|
+
Trollop::die "provide <status> and <name>" unless @conf[:status] and @conf[:name]
|
50
51
|
|
51
52
|
ShoutboxClient.configuration.host = @conf[:host] if @conf[:host]
|
52
53
|
ShoutboxClient.configuration.port = @conf[:port] if @conf[:port]
|
53
54
|
end
|
54
55
|
|
55
56
|
def do_it
|
56
|
-
ShoutboxClient.shout :name => @conf[:name], :
|
57
|
+
ShoutboxClient.shout :name => @conf[:name], :status => @conf[:status], :group => @conf[:group], :message => @conf[:message], :expires_in => @conf[:expires_in]
|
57
58
|
end
|
58
59
|
end
|
59
60
|
end
|
data/lib/shoutbox_client.rb
CHANGED
@@ -31,8 +31,9 @@ class ShoutboxClient
|
|
31
31
|
response = Net::HTTP.Proxy(configuration.proxy_host, configuration.proxy_port).start(configuration.host, configuration.port) do |http|
|
32
32
|
req = Net::HTTP::Put.new( request_url(options) )
|
33
33
|
default_headers(req)
|
34
|
-
body_data = { :
|
35
|
-
body_data[:message]
|
34
|
+
body_data = { :name => options[:name], :group => (options[:group] || configuration.default_group), :status => options[:status].to_s }
|
35
|
+
body_data[:message] = options[:message] if options[:message]
|
36
|
+
body_data[:expires_in] = options[:expires_in] if options[:expires_in]
|
36
37
|
raise ArgumentError if (options[:status] == :red or options[:status] == :yellow) and body_data[:message].to_s.empty?
|
37
38
|
req.body = body_data.to_json
|
38
39
|
http.request(req)
|
@@ -43,7 +44,7 @@ class ShoutboxClient
|
|
43
44
|
def self.delete_status( options )
|
44
45
|
response = Net::HTTP.Proxy(configuration.proxy_host, configuration.proxy_port).start(configuration.host, configuration.port) do |http|
|
45
46
|
req = Net::HTTP::Delete.new( request_url(options) )
|
46
|
-
req.body = { :
|
47
|
+
req.body = { :name => options[:name], :group => (options[:group] || configuration.default_group) }.to_json
|
47
48
|
default_headers(req)
|
48
49
|
http.request(req)
|
49
50
|
end
|
@@ -51,9 +52,10 @@ class ShoutboxClient
|
|
51
52
|
end
|
52
53
|
|
53
54
|
def self.default_headers( request )
|
54
|
-
request['User-Agent']
|
55
|
-
request['Content-Type']
|
56
|
-
request['Accept']
|
55
|
+
request['User-Agent'] = 'Ruby shoutbox-client'
|
56
|
+
request['Content-Type'] = 'application/json'
|
57
|
+
request['Accept'] = 'application/json'
|
58
|
+
request['X-Shoutbox-Auth-Token'] = configuration.auth_token
|
57
59
|
end
|
58
60
|
|
59
61
|
def self.request_url( options )
|
@@ -12,11 +12,12 @@ describe "ShoutboxClient" do
|
|
12
12
|
ShoutboxClient.configuration.config_file = '/i/dont/exist'
|
13
13
|
ShoutboxClient.configuration.host.should == 'localhost'
|
14
14
|
ShoutboxClient.configuration.port.should == 3000
|
15
|
+
ShoutboxClient.configuration.auth_token.should == 'invalid-auth-token'
|
15
16
|
end
|
16
17
|
|
17
18
|
it 'should use the values of the config file' do
|
18
19
|
tempfile = Tempfile.new( '.shoutbox' )
|
19
|
-
tempfile << { "host" => "example.com", "port" => 89, "proxy_host" => "prx", "proxy_port" => 8080 }.to_yaml
|
20
|
+
tempfile << { "host" => "example.com", "port" => 89, "proxy_host" => "prx", "proxy_port" => 8080, "auth_token" => 'abc' }.to_yaml
|
20
21
|
tempfile.close
|
21
22
|
ShoutboxClient.configuration.config_file = tempfile.path
|
22
23
|
ShoutboxClient.configuration.host.should == 'example.com'
|
@@ -24,6 +25,7 @@ describe "ShoutboxClient" do
|
|
24
25
|
ShoutboxClient.configuration.proxy_host.should == "prx"
|
25
26
|
ShoutboxClient.configuration.proxy_port.should == 8080
|
26
27
|
ShoutboxClient.configuration.default_group.should == 'default group'
|
28
|
+
ShoutboxClient.configuration.auth_token.should == 'abc'
|
27
29
|
end
|
28
30
|
|
29
31
|
it 'should use the configured default group' do
|
@@ -39,75 +41,84 @@ describe "ShoutboxClient" do
|
|
39
41
|
|
40
42
|
it 'should return false when it cant connect to the host' do
|
41
43
|
ShoutboxClient.should_receive(:update_status).and_raise(Errno::ECONNREFUSED)
|
42
|
-
ShoutboxClient.shout( :group => "my_group", :
|
44
|
+
ShoutboxClient.shout( :group => "my_group", :name => "test_status", :status => :green ).should == false
|
43
45
|
end
|
44
46
|
|
45
47
|
|
46
48
|
it 'should create a valid PUT request to the shoutbox' do
|
47
49
|
stub_request(:put, "http://localhost:3000/status").
|
48
|
-
with(:body => "{\"
|
49
|
-
:headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby shoutbox-client'}).
|
50
|
+
with(:body => "{\"name\":\"test_status\",\"group\":\"my_group\",\"status\":\"green\"}",
|
51
|
+
:headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby shoutbox-client', 'X-Shoutbox-Auth-Token' => 'invalid-auth-token'}).
|
50
52
|
to_return(:status => 200, :body => "OK", :headers => {})
|
51
53
|
|
52
|
-
ShoutboxClient.shout( :group => "my_group", :
|
54
|
+
ShoutboxClient.shout( :group => "my_group", :name => "test_status", :status => :green ).should == true
|
53
55
|
end
|
54
56
|
|
55
57
|
it 'should create use group default if no group given' do
|
56
58
|
stub_request(:put, "http://localhost:3000/status").
|
57
|
-
with(:body => "{\"
|
58
|
-
:headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby shoutbox-client'}).
|
59
|
+
with(:body => "{\"name\":\"test_status\",\"group\":\"default group\",\"status\":\"green\"}",
|
60
|
+
:headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby shoutbox-client', 'X-Shoutbox-Auth-Token' => 'invalid-auth-token'}).
|
59
61
|
to_return(:status => 200, :body => "OK", :headers => {})
|
60
62
|
|
61
|
-
ShoutboxClient.shout( :
|
63
|
+
ShoutboxClient.shout( :name => "test_status", :status => :green ).should == true
|
62
64
|
end
|
63
65
|
|
64
66
|
it 'should include a message when status is yellow and message is given' do
|
65
67
|
stub_request(:put, "http://localhost:3000/status").
|
66
|
-
with(:body => "{\"
|
67
|
-
:headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby shoutbox-client'}).
|
68
|
+
with(:body => "{\"name\":\"test_status\",\"group\":\"default group\",\"status\":\"yellow\",\"message\":\"This is what you should do now..\"}",
|
69
|
+
:headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby shoutbox-client', 'X-Shoutbox-Auth-Token' => 'invalid-auth-token'}).
|
68
70
|
to_return(:status => 200, :body => "OK", :headers => {})
|
69
71
|
|
70
|
-
ShoutboxClient.shout( :
|
72
|
+
ShoutboxClient.shout( :name => "test_status", :status => :yellow, :message => "This is what you should do now.." ).should == true
|
71
73
|
end
|
72
74
|
|
73
75
|
it 'should include a message when status is red' do
|
74
76
|
stub_request(:put, "http://localhost:3000/status").
|
75
|
-
with(:body => "{\"
|
76
|
-
:headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby shoutbox-client'}).
|
77
|
+
with(:body => "{\"name\":\"test_status\",\"group\":\"default group\",\"status\":\"red\",\"message\":\"This is what you should do now..\"}",
|
78
|
+
:headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby shoutbox-client', 'X-Shoutbox-Auth-Token' => 'invalid-auth-token'}).
|
77
79
|
to_return(:status => 200, :body => "OK", :headers => {})
|
78
80
|
|
79
|
-
ShoutboxClient.shout( :
|
81
|
+
ShoutboxClient.shout( :name => "test_status", :status => :red, :message => "This is what you should do now.." ).should == true
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'should include an expiration time when given' do
|
85
|
+
stub_request(:put, "http://localhost:3000/status").
|
86
|
+
with(:body => "{\"name\":\"test_status\",\"group\":\"default group\",\"status\":\"green\",\"expires_in\":3600}",
|
87
|
+
:headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby shoutbox-client', 'X-Shoutbox-Auth-Token' => 'invalid-auth-token'}).
|
88
|
+
to_return(:status => 200, :body => "OK", :headers => {})
|
89
|
+
|
90
|
+
ShoutboxClient.shout( :name => "test_status", :status => :green, :expires_in => 3600 ).should == true
|
80
91
|
end
|
81
92
|
|
82
93
|
it 'should deny red update if message is missing' do
|
83
94
|
lambda {
|
84
|
-
ShoutboxClient.shout( :
|
95
|
+
ShoutboxClient.shout( :name => "test_status", :status => :red )
|
85
96
|
}.should raise_error(ArgumentError)
|
86
97
|
end
|
87
98
|
|
88
99
|
it 'should deny yellow update if message is missing' do
|
89
100
|
lambda {
|
90
|
-
ShoutboxClient.shout( :
|
101
|
+
ShoutboxClient.shout( :name => "test_status", :status => :yellow )
|
91
102
|
}.should raise_error(ArgumentError)
|
92
103
|
end
|
93
104
|
|
94
105
|
it 'should send optional status on green update' do
|
95
106
|
stub_request(:put, "http://localhost:3000/status").
|
96
|
-
with(:body => "{\"
|
97
|
-
:headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby shoutbox-client'}).
|
107
|
+
with(:body => "{\"name\":\"test_status\",\"group\":\"default group\",\"status\":\"green\",\"message\":\"everything's ok!\"}",
|
108
|
+
:headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby shoutbox-client', 'X-Shoutbox-Auth-Token' => 'invalid-auth-token'}).
|
98
109
|
to_return(:status => 200, :body => "OK", :headers => {})
|
99
110
|
lambda {
|
100
|
-
ShoutboxClient.shout( :
|
111
|
+
ShoutboxClient.shout( :name => "test_status", :status => :green, :message => "everything's ok!" )
|
101
112
|
}.should_not raise_error(ArgumentError)
|
102
113
|
end
|
103
114
|
|
104
115
|
it 'should delete a status' do
|
105
116
|
stub_request(:delete, "http://localhost:3000/status").
|
106
|
-
with(:body => "{\"
|
107
|
-
:headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby shoutbox-client'}).
|
117
|
+
with(:body => "{\"name\":\"test_status\",\"group\":\"default group\"}",
|
118
|
+
:headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby shoutbox-client', 'X-Shoutbox-Auth-Token' => 'invalid-auth-token'}).
|
108
119
|
to_return(:status => 200, :body => "OK", :headers => {})
|
109
120
|
|
110
|
-
ShoutboxClient.shout( :
|
121
|
+
ShoutboxClient.shout( :name => "test_status", :status => :remove ).should == true
|
111
122
|
end
|
112
123
|
|
113
124
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: shoutbox-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0
|
5
|
+
version: 0.1.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Benjamin Krause
|
@@ -10,7 +10,7 @@ autorequire: shoutbox_client
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-04-04 00:00:00 +02:00
|
14
14
|
default_executable: shout
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -111,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
111
111
|
requirements:
|
112
112
|
- - ">="
|
113
113
|
- !ruby/object:Gem::Version
|
114
|
-
hash:
|
114
|
+
hash: 592437698402898647
|
115
115
|
segments:
|
116
116
|
- 0
|
117
117
|
version: "0"
|