sensu-json 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 605a98cc6ae77de3609a10fc69a586df9ed6538e
4
- data.tar.gz: 0a7372801934d75dabe29e9bf3d76b7133838858
3
+ metadata.gz: d71ac5cd61fae3fa7d70aa641dbddb4cef5828d9
4
+ data.tar.gz: d63c376a21897c6b5a19c13143a6e0813b9157b6
5
5
  SHA512:
6
- metadata.gz: 50518f61efe074a066bc1a7fd3fbdba8c349d962a301773ff797d02308ef56b9872be49e616ef33430e2cc844be950646bcd117286498017651e8b634230451d
7
- data.tar.gz: 4be401bb0594582ac01af703987769cb2fb713f091b5f82cac6229c82b540e5bcfe42e2d81f1082c5e111acfa9254ebb9e7aed2ca741ec659a0ef215df57c2dc
6
+ metadata.gz: 7a8f27abd7e39f4a7bbf9fbc883bf5c548ff340fff9e959c8ad7179d6f95f64ce327b674ae8df8ee790231b85e092137364d873cda2d1649ba80b12512a62908
7
+ data.tar.gz: c077f7ef60e6ad64c569b0cc2e398337a9d2253cca4a6dcb8bd10ab394775b1d649ae0bfc7b8f1080089d99dc45f5d22e821ef11a48579dcfe4085a59b11aaab
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -2,11 +2,20 @@ require "json"
2
2
 
3
3
  module Sensu
4
4
  module JSON
5
+ # The Sensu JSON parser when running on JRuby.
5
6
  class Java
7
+ # Load (and parse) a JSON string. Sensu expects symbolized keys.
8
+ #
9
+ # @param string [String]
10
+ # @return [Object]
6
11
  def load(string)
7
12
  ::JSON.parse(string, :symbolize_names => true)
8
13
  end
9
14
 
15
+ # Dump (generate) a JSON string from a Ruby object.
16
+ #
17
+ # @param object [Object]
18
+ # @param options [Hash]
10
19
  def dump(object, options={})
11
20
  if options[:pretty]
12
21
  ::JSON.pretty_generate(object)
data/lib/sensu/json/oj.rb CHANGED
@@ -2,13 +2,24 @@ gem "oj", "2.14.6"
2
2
 
3
3
  require "oj"
4
4
 
5
+ Oj.default_options = {:mode => :compat}
6
+
5
7
  module Sensu
6
8
  module JSON
9
+ # The Sensu JSON parser when running on MRI.
7
10
  class Oj
11
+ # Load (and parse) a JSON string. Sensu expects symbolized keys.
12
+ #
13
+ # @param string [String]
14
+ # @return [Object]
8
15
  def load(string)
9
16
  ::Oj.load(string, :symbol_keys => true)
10
17
  end
11
18
 
19
+ # Dump (generate) a JSON string from a Ruby object.
20
+ #
21
+ # @param object [Object]
22
+ # @param options [Hash]
12
23
  def dump(object, options={})
13
24
  options.merge!(:indent => 2) if options[:pretty]
14
25
  ::Oj.dump(object, options)
@@ -1,8 +1,14 @@
1
1
  module Sensu
2
2
  module JSON
3
+ # The Sensu JSON parser error abstraction.
3
4
  class ParseError < StandardError
4
5
  attr_reader :data, :cause
5
6
 
7
+ # Produce an encapsulating error for a parser error, maintaining
8
+ # the backtrace.
9
+ #
10
+ # @param original_error [Object]
11
+ # @param data [Object] (such as a JSON string).
6
12
  def self.build(original_error, data)
7
13
  new(original_error.message).tap do |error|
8
14
  error.instance_eval do
data/lib/sensu/json.rb CHANGED
@@ -1,9 +1,17 @@
1
1
  require "sensu/json/parse_error"
2
2
 
3
+ # Sensu, monitoring for today's infrastructure.
3
4
  module Sensu
5
+ # The Sensu JSON parser abstraction library.
4
6
  module JSON
7
+ # The Sensu JSON parser abstraction API.
5
8
  class << self
6
- def setup
9
+ # Set up the JSON parser. This method must be called before any
10
+ # attempt to use the parser. The appropriate JSON parser will be
11
+ # loaded for the current platform.
12
+ #
13
+ # @return [Object] parser.
14
+ def setup!
7
15
  @@parser = case
8
16
  when RUBY_PLATFORM =~ /java/
9
17
  require "sensu/json/java"
@@ -14,6 +22,10 @@ module Sensu
14
22
  end
15
23
  end
16
24
 
25
+ # Load (and parse) a JSON string.
26
+ #
27
+ # @param string [String]
28
+ # @return [Object]
17
29
  def load(string)
18
30
  begin
19
31
  @@parser.load(string)
@@ -22,6 +34,10 @@ module Sensu
22
34
  end
23
35
  end
24
36
 
37
+ # Dump (generate) a JSON string from a Ruby object.
38
+ #
39
+ # @param object [Object]
40
+ # @param options [Hash]
25
41
  def dump(object, options={})
26
42
  @@parser.dump(object, options)
27
43
  end
data/sensu-json.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "sensu-json"
5
- spec.version = "0.0.1"
5
+ spec.version = "1.0.0"
6
6
  spec.authors = ["Sean Porter"]
7
7
  spec.email = ["portertech@gmail.com"]
8
8
  spec.summary = "The Sensu JSON parser abstraction library"
data/spec/json_spec.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "sensu/json"
2
+
3
+ describe "Sensu::JSON" do
4
+ it "can load a parser for the current platform" do
5
+ Sensu::JSON.setup!
6
+ hash = Sensu::JSON.load('{"foo":"bar"}')
7
+ expect(hash).to eq({:foo => "bar"})
8
+ string = Sensu::JSON.dump({:baz => "qux"})
9
+ expect(string).to eq('{"baz":"qux"}')
10
+ end
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensu-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Porter
@@ -102,6 +102,7 @@ files:
102
102
  - lib/sensu/json/oj.rb
103
103
  - lib/sensu/json/parse_error.rb
104
104
  - sensu-json.gemspec
105
+ - spec/json_spec.rb
105
106
  homepage: https://github.com/sensu/sensu-json
106
107
  licenses:
107
108
  - MIT
@@ -126,4 +127,5 @@ rubygems_version: 2.4.5.1
126
127
  signing_key:
127
128
  specification_version: 4
128
129
  summary: The Sensu JSON parser abstraction library
129
- test_files: []
130
+ test_files:
131
+ - spec/json_spec.rb