From 726923c18f2a605889b4d63071145498016a4374 Mon Sep 17 00:00:00 2001 From: Felipe Oliveira Date: Fri, 7 Dec 2012 16:38:50 -0200 Subject: [PATCH] Message class, responsible to return a hash. --- lib/nsca/client/message.rb | 27 +++++++++++++++++++++++++++ spec/nsca/client/message_spec.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 lib/nsca/client/message.rb create mode 100644 spec/nsca/client/message_spec.rb diff --git a/lib/nsca/client/message.rb b/lib/nsca/client/message.rb new file mode 100644 index 0000000..c210c50 --- /dev/null +++ b/lib/nsca/client/message.rb @@ -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 diff --git a/spec/nsca/client/message_spec.rb b/spec/nsca/client/message_spec.rb new file mode 100644 index 0000000..730cbfe --- /dev/null +++ b/spec/nsca/client/message_spec.rb @@ -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