Skip to content

Commit

Permalink
Use inline Oj settings when necessary (#2)
Browse files Browse the repository at this point in the history
Setting `Oj.default_options` global setting causes unexpected sid effects for applications where gem is being included. Instead the `bigdecimal_load: :bigdecimal` mode can be applied inline.
  • Loading branch information
blid authored Apr 26, 2021
2 parents e86466e + 40f39ec commit f59a942
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
1 change: 1 addition & 0 deletions avatax.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Gem::Specification.new do |s|
s.add_development_dependency('rake', '~> 12.0.0')
s.add_development_dependency('rspec', '~> 3.5.0')
s.add_development_dependency('webmock', '>= 2.0.0')
s.add_development_dependency('pry')
s.add_runtime_dependency('faraday', '>= 0.10')
s.add_runtime_dependency('faraday_middleware', '>= 0.10')
s.add_runtime_dependency('multi_json', '>= 1.0.3')
Expand Down
1 change: 0 additions & 1 deletion lib/avatax/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

module AvaTax
class API

attr_accessor *Configuration::VALID_OPTIONS_KEYS

def initialize(options={})
Expand Down
26 changes: 19 additions & 7 deletions lib/avatax/connection.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
require 'faraday_middleware/response_middleware'
require 'faraday_middleware/parse_oj'

module AvaTax

module Connection
module FaradayMiddleware
class ParseOjWithBigDecimal < ::FaradayMiddleware::ResponseMiddleware
dependency 'oj'

define_parser do |body|
Oj.load(body, mode: :compat, bigdecimal_load: :bigdecimal) unless body.strip.empty?
end
end
end

Faraday::Response.register_middleware oj_with_bigdecimal: FaradayMiddleware::ParseOjWithBigDecimal

private
AUTHORIZATION_FILTER_REGEX = /(Authorization\:\ \"Basic\ )(\w+)\=/
REMOVED_LABEL = '\1[REMOVED]'
Expand All @@ -22,13 +34,13 @@ def connection
}.merge(connection_options)

Faraday.new(options) do |faraday|
if Gem::Version.new(RUBY_VERSION) > Gem::Version.new('2.2.2') and response_big_decimal_conversion
Oj.default_options = {
bigdecimal_load: :bigdecimal
}
end
faraday_response_parser = if Gem::Version.new(RUBY_VERSION) > Gem::Version.new('2.2.2') and response_big_decimal_conversion
:oj_with_bigdecimal
else
:oj
end

faraday.response :oj, content_type: /\bjson$/
faraday.response faraday_response_parser, content_type: /\bjson$/
faraday.basic_auth(username, password)

if logger
Expand Down
30 changes: 30 additions & 0 deletions spec/avatax/api_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require_relative "../spec_helper"

describe AvaTax::API do
context "connection" do

it "uses custom ParseOj for regular config" do
faraday_instance_double = double.as_null_object

expect(Faraday).to receive(:new).and_yield(faraday_instance_double)
expect(faraday_instance_double).to receive(:response).with(:oj, { content_type: /\bjson$/ })

# default setting
expect(Oj.default_options[:bigdecimal_load]).to eq(:auto)

AvaTax::API.new.send(:connection)
end

it "uses custom ParseOjWithBigDecimal when response_big_decimal_conversion provided" do
faraday_instance_double = double.as_null_object

expect(Faraday).to receive(:new).and_yield(faraday_instance_double)
expect(faraday_instance_double).to receive(:response).with(:oj_with_bigdecimal, { content_type: /\bjson$/ })

# default setting
expect(Oj.default_options[:bigdecimal_load]).to eq(:auto)

AvaTax::API.new(response_big_decimal_conversion: true).send(:connection)
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
RSpec.configure do |config|
config.before {
@client = client
@company_code = companies["value"][0]["companyCode"]
@company_code = companies.dig("value", 0, "companyCode")
}
end

0 comments on commit f59a942

Please sign in to comment.