shoutbox-client 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -8,12 +8,12 @@ Examples
8
8
 
9
9
  You can use it via ruby
10
10
 
11
- ruby-1.9.2-p0 > ShoutboxClient.shout :name => "nightly-rake-task", :status => :red
11
+ ruby-1.9.2-p0 > ShoutboxClient.shout :statusId => "nightly-rake-task", :status => :red
12
12
  true
13
13
 
14
14
  or as a shell script
15
15
 
16
- benjamin@Jiji % shout -n important-task -s red
16
+ benjamin@Jiji % shout green important-task
17
17
 
18
18
  Configuration
19
19
  ---
@@ -33,7 +33,7 @@ Otherwise you can set the configuration manually via ruby
33
33
 
34
34
  The shell script accepts host and port parameters
35
35
 
36
- benjamin@Jiji % shout -n important-task -s red -h shoutbox.moviepilot.com -p 80
36
+ benjamin@Jiji % shout red important-task -h shoutbox.moviepilot.com -p 80
37
37
 
38
38
 
39
39
  Contributing to shoutbox-client
@@ -11,29 +11,32 @@ module Shoutbox
11
11
  def parse_options
12
12
  @conf = Trollop::options do
13
13
  banner <<-EOS
14
- shout is a simple command line client to update your Shoutbox
14
+ usage: shout <status> <statusId> [options]
15
15
 
16
- Usage:
17
- shout -n <name> -s <red|green>
18
-
19
- where [options] are:
16
+ <status> must be one of red, green, remove
17
+ <statusId> is any given id you can think of
18
+
19
+ shout allows the folling options:
20
20
  EOS
21
21
 
22
- opt :host, "The hostname of the Shoutbox", :type => String
23
- opt :group, "The group to use", :type => String, :default => "default"
24
- opt :port, "The port of the Shoutbox", :type => Integer
25
- opt :name, "", :type => String
26
- opt :status, "", :type => String
22
+ opt :host, "The hostname of the Shoutbox", :type => String
23
+ opt :port, "The port of the Shoutbox", :type => Integer
24
+ opt :group, "The group to use", :type => String, :default => "default"
25
+ opt :name, "The display name for the statusId", :type => String
27
26
  end
27
+
28
+ Trollop::die "provide <status> and <statusId>" if ARGV.size != 2
29
+ @conf[:status] = ARGV.shift
30
+ Trollop::die "<status> must be one of 'green', 'red', 'destroy'" unless ShoutboxClient.valid_status?( @conf[:status] )
31
+
32
+ @conf[:statusId] = ARGV.shift
28
33
 
29
- Trollop::die :name, "name must be given" unless @conf[:name]
30
- Trollop::die :status, "status must be given" unless @conf[:status]
31
34
  ShoutboxClient.configuration.host = @conf[:host] if @conf[:host]
32
35
  ShoutboxClient.configuration.port = @conf[:port] if @conf[:port]
33
36
  end
34
37
 
35
38
  def do_it
36
- ShoutboxClient.shout :name => @conf[:name], :status => @conf[:status], :group => @conf[:group]
39
+ ShoutboxClient.shout :name => @conf[:name], :statusId => @conf[:statusId], :status => @conf[:status], :group => @conf[:group]
37
40
  end
38
41
  end
39
42
  end
@@ -5,20 +5,53 @@ require 'JSON'
5
5
  require 'yaml'
6
6
 
7
7
  class ShoutboxClient
8
+ VALID_STATUS = { 'red' => :update,
9
+ 'green' => :update,
10
+ 'destroy' => :delete }
11
+
8
12
 
9
13
  def self.configuration
10
14
  @configuration ||= Shoutbox::Configuration.new
11
15
  end
12
16
 
13
17
  def self.shout( options )
18
+ if valid_status?( options[:status] )
19
+ VALID_STATUS[options[:status].to_s] == :update ? update_status( options ) : delete_status( options )
20
+ else
21
+ false
22
+ end
23
+ end
24
+
25
+ def self.update_status( options )
14
26
  response = Net::HTTP.start(configuration.host, configuration.port) do |http|
15
- req = Net::HTTP::Put.new('/status')
16
- req['User-Agent'] = 'Ruby shoutbox-client'
27
+ req = Net::HTTP::Put.new( request_url(options) )
28
+ default_headers(req)
17
29
  req['Content-Type'] = 'application/json'
18
- req['Accept'] = 'application/json'
19
- req.body = { "group" => options[:group], "name" => options[:name], :status => options[:status].to_s }.to_json
30
+ req.body = { :status => options[:status].to_s }.to_json
31
+ http.request(req)
32
+ end
33
+ response.body == "OK"
34
+ end
35
+
36
+ def self.delete_status( options )
37
+ response = Net::HTTP.start(configuration.host, configuration.port) do |http|
38
+ req = Net::HTTP::Delete.new( request_url(options) )
39
+ default_headers(req)
20
40
  http.request(req)
21
41
  end
22
42
  response.body == "OK"
23
43
  end
44
+
45
+ def self.default_headers( request )
46
+ request['User-Agent'] = 'Ruby shoutbox-client'
47
+ request['Accept'] = 'application/json'
48
+ end
49
+
50
+ def self.request_url( options )
51
+ '/status/' + (options[:group] || 'default') + "/" + options[:statusId]
52
+ end
53
+
54
+ def self.valid_status?( status )
55
+ VALID_STATUS.keys.include?( status.to_s )
56
+ end
24
57
  end
@@ -23,12 +23,30 @@ describe "ShoutboxClient" do
23
23
 
24
24
  context 'http communication' do
25
25
  it 'should create a valid PUT request to the shoutbox' do
26
- stub_request(:put, "http://localhost:3000/status").
27
- with(:body => "{\"group\":\"default\",\"name\":\"test_status\",\"status\":\"green\"}",
26
+ stub_request(:put, "http://localhost:3000/status/my_group/test_status").
27
+ with(:body => "{\"status\":\"green\"}",
28
28
  :headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby shoutbox-client'}).
29
29
  to_return(:status => 200, :body => "OK", :headers => {})
30
30
 
31
- ShoutboxClient.shout( :group => "default", :name => "test_status", :status => :green ).should == true
31
+ ShoutboxClient.shout( :group => "my_group", :statusId => "test_status", :status => :green ).should == true
32
32
  end
33
+
34
+ it 'should create use group default if no group given' do
35
+ stub_request(:put, "http://localhost:3000/status/default/test_status").
36
+ with(:body => "{\"status\":\"green\"}",
37
+ :headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby shoutbox-client'}).
38
+ to_return(:status => 200, :body => "OK", :headers => {})
39
+
40
+ ShoutboxClient.shout( :statusId => "test_status", :status => :green ).should == true
41
+ end
42
+
43
+ it 'should delete a status' do
44
+ stub_request(:delete, "http://localhost:3000/status/default/test_status").
45
+ with(:headers => {'Accept'=>'application/json', 'User-Agent'=>'Ruby shoutbox-client'}).
46
+ to_return(:status => 200, :body => "OK", :headers => {})
47
+
48
+ ShoutboxClient.shout( :statusId => "test_status", :status => :destroy ).should == true
49
+ end
50
+
33
51
  end
34
52
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 3
9
+ version: 0.0.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Benjamin Krause
@@ -87,6 +87,21 @@ dependencies:
87
87
  type: :development
88
88
  prerelease: false
89
89
  version_requirements: *id005
90
+ - !ruby/object:Gem::Dependency
91
+ name: json
92
+ requirement: &id006 !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ~>
96
+ - !ruby/object:Gem::Version
97
+ segments:
98
+ - 1
99
+ - 4
100
+ - 0
101
+ version: 1.4.0
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: *id006
90
105
  description: " Simple Ruby Client to manage status on a node.js Shoutbox Server "
91
106
  email: bk@benjaminkrause.com
92
107
  executables:
@@ -119,7 +134,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
119
134
  requirements:
120
135
  - - ">="
121
136
  - !ruby/object:Gem::Version
122
- hash: -2791319397029463430
137
+ hash: -425631808268583408
123
138
  segments:
124
139
  - 0
125
140
  version: "0"