-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from bobtfish/fix_integers_in_output
Make integers come out in JSON as integers.
- Loading branch information
Showing
6 changed files
with
90 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Puppet_X | ||
module Sensu | ||
module Totype | ||
def to_type(value) | ||
if value.is_a?(Hash) | ||
new = Hash.new | ||
value.each { |k,v| new[k] = to_type v } | ||
new | ||
elsif value.is_a?(Array) | ||
value.collect { |v| to_type v } | ||
else | ||
case value | ||
when true, 'true', 'True', :true | ||
true | ||
when false, 'false', 'False', :false | ||
false | ||
when /^([0-9])+$/ | ||
value.to_i | ||
else | ||
value | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require 'spec_helper' | ||
require 'puppet_x/sensu/to_type' | ||
|
||
class TotypeFixtureClass | ||
include Puppet_X::Sensu::Totype | ||
end | ||
|
||
describe TotypeFixtureClass do | ||
let(:helper) { TotypeFixtureClass.new } | ||
it 'should be callable with int, and return int' do | ||
helper.to_type(1).should == 1 | ||
end | ||
it 'should be callable with string int, and return int' do | ||
helper.to_type('1').should == 1 | ||
end | ||
it 'should be callable with string, and return string' do | ||
helper.to_type('1 foo').should == '1 foo' | ||
end | ||
it 'should be callable with false' do | ||
helper.to_type(false).should == false | ||
end | ||
it 'should be callable with true' do | ||
helper.to_type(true).should == true | ||
end | ||
it 'should be callable with nil' do | ||
helper.to_type(nil).should == nil | ||
end | ||
it 'should be callable with array and return munged array' do | ||
helper.to_type([1, '1', '1 foo', false, true, nil]).should == [1, 1, '1 foo', false, true, nil] | ||
end | ||
it 'should be callable with hash and return munged hash' do | ||
helper.to_type({:a => 1, :b => '1', :c => '1 foo', :d => false, :e => true, :f => nil}).should == {:a => 1, :b => 1, :c => '1 foo', :d => false, :e => true, :f => nil} | ||
end | ||
it 'should be able to recurse' do | ||
helper.to_type({:a => 1, :b => '1', :c => '1 foo', :d => false, :e => true, :f => nil, :g => {:a => 1, :b => '1', :c => '1 foo', :d => false, :e => true, :f => nil}, :h => [1, '1', '1 foo', false, true, nil]}).should == {:a => 1, :b => 1, :c => '1 foo', :d => false, :e => true, :f => nil, :g => {:a => 1, :b => 1, :c => '1 foo', :d => false, :e => true, :f => nil}, :h => [1, 1, '1 foo', false, true, nil]} | ||
end | ||
end | ||
|