Skip to content

Commit

Permalink
Add convert_na_to_nil option
Browse files Browse the repository at this point in the history
Closes #21

LGTM given by: @ivdma
Squashed commit of the following:

commit 2589c43
Author: timraasveld <ttimitri@gmail.com>
Date:   Wed Feb 3 09:05:57 2016 +0100

    Remove extra space

commit 6e7cb0e
Author: timraasveld <ttimitri@gmail.com>
Date:   Fri Jan 29 13:44:49 2016 +0100

    Process feedback

commit 31a9550
Author: timraasveld <ttimitri@gmail.com>
Date:   Wed Jan 27 09:00:08 2016 +0100

    Remove extra vcr recording

commit 48ece1e
Author: timraasveld <ttimitri@gmail.com>
Date:   Wed Jan 27 08:56:12 2016 +0100

    Update bundler for travis

commit 512876e
Author: timraasveld <ttimitri@gmail.com>
Date:   Tue Jan 26 17:21:26 2016 +0100

    Add changelog

commit 062a2ca
Author: timraasveld <ttimitri@gmail.com>
Date:   Tue Jan 26 17:21:09 2016 +0100

    Add convert na to nil option
  • Loading branch information
timraasveld committed Feb 5, 2016
1 parent b4fca45 commit ea2ae40
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ rvm:
- 2.0.0-p576
- 2.1.3
script: bundle exec rspec spec
before_install:
- gem install bundler
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.9.1

* Added `convert_na_to_nil=true/false` option to Client#execute

# 0.9.0

* Added support for Github R repos through OpenCPU
Expand Down
27 changes: 22 additions & 5 deletions lib/opencpu/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,29 @@ def initialize
end

def execute(package, function, options = {})
user = options.fetch :user, :system
data = options.fetch :data, {}
format = options.fetch :format, :json
github_remote = options.fetch :github_remote, false
user = options.fetch :user, :system
data = options.fetch :data, {}
format = options.fetch :format, :json
github_remote = options.fetch :github_remote, false
should_convert_na_to_nil = options.fetch :convert_na_to_nil, false

process_query package_url(package, function, user, github_remote, :json), data, format do |response|
JSON.parse(response.body)
output = JSON.parse(response.body)
output = convert_na_to_nil(output) if should_convert_na_to_nil
output
end
end

def convert_na_to_nil(data)
case data
when 'NA'
nil
when Hash
data.each { |k, v| data[k] = convert_na_to_nil(v) }
when Array
data.map! { |v| convert_na_to_nil(v) }
else
data
end
end

Expand Down
1 change: 0 additions & 1 deletion spec/fixtures/vcr_cassettes/animation_flip_coin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3004,4 +3004,3 @@ http_interactions:
\ \n"
http_version:
recorded_at: Tue, 14 Oct 2014 18:51:02 GMT
recorded_with: VCR 2.9.3
61 changes: 61 additions & 0 deletions spec/fixtures/vcr_cassettes/response_with_na_values.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions spec/lib/opencpu/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@
end
end

context 'na_to_nil = true option is given' do
it 'converts "na" values to nil' do
VCR.use_cassette :response_with_na_values do |cassette|
response = client.execute(:base, :identity, format: nil, data: {}, convert_na_to_nil: true)
expect(response).to eq [{"x"=>nil, "y"=>"not_na"}]
end
end
end

context 'multipart form / file uploads' do
it "works" do
skip # vcr is broken for file uploads https://github.com/vcr/vcr/issues/441
Expand Down Expand Up @@ -190,6 +199,25 @@
end
end

describe "#convert_na_to_nil" do
let(:client) { described_class.new }

it "converts 'NA' values in hashes in arrays" do
res = client.convert_na_to_nil([4, {foo: 'NA'}])
expect(res[1][:foo]).to be_nil
end

it "converts 'NA' values in arrays in hashes" do
res = client.convert_na_to_nil(foo: [1, 'NA'])
expect(res[:foo][1]).to be_nil
end

it 'leaves other values alone' do
res = client.convert_na_to_nil(foo: [1, 'NOTNA'])
expect(res[:foo][1]).to eq 'NOTNA'
end
end

describe '#request_options' do
it 'uses verify_ssl setting' do
expect(described_class.new.send(:request_options, {}, nil)[:verify]).to be_truthy
Expand Down

0 comments on commit ea2ae40

Please sign in to comment.