-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore MultiJson and add custom option modules. Fix #303
In order to support JRuby it'd be better not force installing Oj since it cannot be used in JRuby if C extensions are not enabled, and they are not supported by default. So, we add MultiJson again to the project. This will create more different scenarios but since it could be the best option according to previous paragraph. We've added in this change support for custom options depending on the loaded adapter in MultiJson. We can add more adapter options modules in this way: module Rollbar module JSON module AnyMultiJsonAdapterName def self.options { # here comes the options we need for that adapter } end end end end We have, for now, just the Oj module that will return the options we had before this commit. The specs run with Oj by default and it's the recommended adapter to use with Rollbar.
- Loading branch information
Jon de Andres
committed
Sep 18, 2015
1 parent
67f130a
commit 63db163
Showing
9 changed files
with
148 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Rollbar | ||
module JSON | ||
module Default | ||
extend self | ||
|
||
def options | ||
{} | ||
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,15 @@ | ||
module Rollbar | ||
module JSON | ||
module Oj | ||
extend self | ||
|
||
def options | ||
{ :mode=> :compat, | ||
:use_to_json => false, | ||
:symbol_keys => false, | ||
:circular => false | ||
} | ||
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
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,18 @@ | ||
require 'spec_helper' | ||
|
||
require 'rollbar/json/oj' | ||
|
||
describe Rollbar::JSON::Oj do | ||
let(:options) do | ||
{ | ||
:mode => :compat, | ||
:use_to_json => false, | ||
:symbol_keys => false, | ||
:circular => false | ||
} | ||
end | ||
|
||
it 'returns correct options' do | ||
expect(described_class.options).to be_eql(options) | ||
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 |
---|---|---|
@@ -1,43 +1,89 @@ | ||
require 'spec_helper' | ||
|
||
require 'multi_json' | ||
require 'rollbar/json' | ||
require 'rollbar/configuration' | ||
|
||
describe Rollbar::JSON do | ||
before do | ||
Rollbar::JSON.setup | ||
class Rollbar::JSON::MockAdapter | ||
def self.options | ||
{ 'mock' => 'adapter' } | ||
end | ||
end | ||
|
||
let(:payload) do | ||
{ :foo => :bar } | ||
module MultiJson | ||
module Adapters | ||
module MockAdapter | ||
end | ||
end | ||
end | ||
|
||
let(:options) do | ||
{ | ||
:mode => :compat, | ||
:use_to_json => false, | ||
:symbol_keys => false, | ||
:circular => false | ||
} | ||
module MultiJson | ||
module Adapters | ||
module MissingCustomOptions | ||
end | ||
end | ||
end | ||
|
||
|
||
describe Rollbar::JSON do | ||
let(:payload) do | ||
{ :foo => :bar } | ||
end | ||
let(:adapter_options) { { 'option' => 'value' } } | ||
|
||
describe '.dump' do | ||
it 'has JSON as backend' do | ||
expect(Rollbar::JSON.backend_name).to be_eql(:oj) | ||
before do | ||
allow(described_class).to receive(:adapter_options).and_return(adapter_options) | ||
end | ||
|
||
it 'calls MultiJson.dump' do | ||
expect(::MultiJson).to receive(:dump).once.with(payload, adapter_options) | ||
|
||
it 'calls JSON.generate' do | ||
expect(::Oj).to receive(:dump).once.with(payload, options) | ||
|
||
Rollbar::JSON.dump(payload) | ||
described_class.dump(payload) | ||
end | ||
end | ||
|
||
describe '.load' do | ||
before do | ||
allow(described_class).to receive(:adapter_options).and_return(adapter_options) | ||
end | ||
|
||
it 'calls MultiJson.load' do | ||
expect(::Oj).to receive(:load).once.with(payload, options) | ||
expect(::MultiJson).to receive(:load).once.with(payload, adapter_options) | ||
|
||
described_class.load(payload) | ||
end | ||
end | ||
|
||
describe '.adapter_options' do | ||
it 'calls .options in adapter module' do | ||
expect(described_class.adapter_module).to receive(:options) | ||
|
||
described_class.adapter_options | ||
end | ||
end | ||
|
||
describe '.adapter_module' do | ||
before { described_class.adapter_module = nil } | ||
|
||
context 'with a defined rollbar adapter' do | ||
let(:expected_adapter) { Rollbar::JSON::MockAdapter } | ||
|
||
it 'returns the correct options' do | ||
MultiJson.with_adapter(MultiJson::Adapters::MockAdapter) do | ||
expect(described_class.adapter_module).to be(expected_adapter) | ||
end | ||
end | ||
end | ||
|
||
context 'without a defined rollbar adapter' do | ||
let(:expected_adapter) { Rollbar::JSON::Default } | ||
|
||
Rollbar::JSON.load(payload) | ||
it 'returns the correct options' do | ||
MultiJson.with_adapter(MultiJson::Adapters::MissingCustomOptions) do | ||
expect(described_class.adapter_module).to be(expected_adapter) | ||
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
63db163
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a great solution man!