Message class, responsible to return a hash.
This commit is contained in:
parent
ca40c4a229
commit
726923c18f
2 changed files with 58 additions and 0 deletions
27
lib/nsca/client/message.rb
Normal file
27
lib/nsca/client/message.rb
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
module NSCA
|
||||||
|
module Client
|
||||||
|
class Message
|
||||||
|
CODE = { :ok => 0, :warn => 1, :critical => 2 }
|
||||||
|
|
||||||
|
attr_reader :return_code
|
||||||
|
|
||||||
|
def initialize(alert, status, service, remote_server)
|
||||||
|
@return_code = CODE[alert]
|
||||||
|
@status = status
|
||||||
|
@service = service
|
||||||
|
@remote_server = remote_server
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_h
|
||||||
|
{
|
||||||
|
:nscahost => @remote_server.host,
|
||||||
|
:port => @remote_server.port,
|
||||||
|
:hostname => @service.host.hostname,
|
||||||
|
:service => @service.name,
|
||||||
|
:return_code => @return_code,
|
||||||
|
:status => @status
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
31
spec/nsca/client/message_spec.rb
Normal file
31
spec/nsca/client/message_spec.rb
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe NSCA::Client::Message do
|
||||||
|
let(:server) { NSCA::Client::RemoteServer.new(:host => '172.0.0.1') }
|
||||||
|
let(:host) { NSCA::Client::Host.new(:hostname => 'dummy') }
|
||||||
|
let(:service) { NSCA::Client::Service.new(:name => 'TestMessage', :host => host) }
|
||||||
|
|
||||||
|
subject { described_class.new(:ok, 'OK', service, server).to_h }
|
||||||
|
|
||||||
|
its([:nscahost]) { should eq '172.0.0.1' }
|
||||||
|
its([:port]) { should eq 5667 }
|
||||||
|
its([:hostname]) { should eq 'dummy' }
|
||||||
|
its([:service]) { should eq 'TestMessage' }
|
||||||
|
its([:return_code]) { should eq 0 }
|
||||||
|
its([:status]) { should eq 'OK' }
|
||||||
|
|
||||||
|
context "ok message" do
|
||||||
|
subject { described_class.new(:ok, 'OK', service, server) }
|
||||||
|
its(:return_code) { should eq 0 }
|
||||||
|
end
|
||||||
|
|
||||||
|
context "warn message" do
|
||||||
|
subject { described_class.new(:warn, 'WARN', service, server) }
|
||||||
|
its(:return_code) { should eq 1 }
|
||||||
|
end
|
||||||
|
|
||||||
|
context "critical message" do
|
||||||
|
subject { described_class.new(:critical, 'CRITICAL', service, server) }
|
||||||
|
its(:return_code) { should eq 2 }
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue