Skip to content

Commit

Permalink
fix: handle json parser dependencies (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-ashwinkumar authored Jul 14, 2021
1 parent 07e1f13 commit c61e801
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 33 deletions.
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
PATH
remote: .
specs:
smartcar (3.0.0)
smartcar (3.0.1)
oauth2 (~> 1.4)
recursive-open-struct (~> 1.1.3)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -53,6 +54,7 @@ GEM
readapt (1.3.0)
backport (~> 1.1)
thor (~> 1.0)
recursive-open-struct (1.1.3)
redcarpet (3.5.1)
regexp_parser (2.1.1)
rexml (3.2.5)
Expand Down
21 changes: 0 additions & 21 deletions lib/open_struct_extensions.rb

This file was deleted.

1 change: 1 addition & 0 deletions lib/smartcar.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'recursive_open_struct'
require 'smartcar_error'
require 'smartcar/utils'
require 'smartcar/version'
Expand Down
4 changes: 2 additions & 2 deletions lib/smartcar/auth_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def exchange_code(code, options = {})
.get_token(code, redirect_uri: redirect_uri)
.to_hash

JSON.parse(token_hash.to_json, object_class: OpenStruct)
json_to_ostruct(token_hash)
rescue OAuth2::Error => e
raise build_error(e.response.status, e.response.body, e.response.headers)
end
Expand All @@ -89,7 +89,7 @@ def exchange_refresh_token(token, options = {})
token_object = OAuth2::AccessToken.from_hash(client, { refresh_token: token })
token_object = token_object.refresh!

JSON.parse(token_object.to_hash.to_json, object_class: OpenStruct)
json_to_ostruct(token_object.to_hash)
rescue OAuth2::Error => e
raise build_error(e.response.status, e.response.body, e.response.headers)
end
Expand Down
16 changes: 14 additions & 2 deletions lib/smartcar/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ def get_config(config_name)
ENV[config_name]
end

# Converts a hash to RecursiveOpenStruct (a powered up OpenStruct object).
# NOTE - Do not replace with the more elegant looking
# JSON.parse(meta_hash.to_json, object_class: OpenStruct)
# this is because we had an app using OJ as their json parser which led to an issue using the
# above mentioned method. Source : https://github.com/ohler55/oj/issues/239
# @param hash [Hash] json object as hash
#
# @return [RecursiveOpenStruct]
def json_to_ostruct(hash)
RecursiveOpenStruct.new(hash)
end

def build_meta(headers)
meta_hash = {
'sc-data-age' => :data_age,
Expand All @@ -33,14 +45,14 @@ def build_meta(headers)
}.each_with_object({}) do |(header_name, key), meta|
meta[key] = headers[header_name] if headers[header_name]
end
meta = JSON.parse(meta_hash.to_json, object_class: OpenStruct)
meta = json_to_ostruct(meta_hash)
meta.data_age &&= DateTime.parse(meta.data_age)

meta
end

def build_response(body, headers)
response = JSON.parse(body.to_json, object_class: OpenStruct)
response = json_to_ostruct(body)
response.meta = build_meta(headers)
response
end
Expand Down
2 changes: 1 addition & 1 deletion lib/smartcar/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module Smartcar
# Gem current version number
VERSION = '3.0.0'
VERSION = '3.0.1'
end
1 change: 1 addition & 0 deletions ruby-sdk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'selenium-webdriver', '~> 3.142'
spec.add_development_dependency 'webmock', '~> 3.13'
spec.add_dependency 'oauth2', '~> 1.4'
spec.add_dependency 'recursive-open-struct', '~> 1.1.3'
end
8 changes: 4 additions & 4 deletions spec/smartcar/e2e/vehicle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,17 @@
it 'should return hash of objects with attribute requested as keys' do
attributes = ['/charge', '/battery', '/odometer']
result = @vehicle.batch(attributes)
expect(result.instance_of?(OpenStruct)).to eq(true)
expect(result.charge.instance_of?(OpenStruct)).to eq(true)
expect(result.is_a?(OpenStruct)).to eq(true)
expect(result.charge.is_a?(OpenStruct)).to eq(true)
expect(result.charge.is_plugged_in?).not_to be_nil
expect(result.charge.state).not_to be_nil
expect(result.charge.meta).not_to be_nil
expect(result.charge.meta.request_id.length).to eq(36)
expect(result.battery.instance_of?(OpenStruct)).to eq(true)
expect(result.battery.is_a?(OpenStruct)).to eq(true)
expect(result.battery.percentage_remaining).not_to be_nil
expect(result.battery.range).not_to be_nil
expect(result.battery.meta).not_to be_nil
expect(result.odometer.instance_of?(OpenStruct)).to eq(true)
expect(result.odometer.is_a?(OpenStruct)).to eq(true)
expect(result.odometer.meta).not_to be_nil
expect(result.odometer.distance).not_to be_nil
end
Expand Down
4 changes: 2 additions & 2 deletions spec/smartcar/integration/vehicle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@
)
expected_description = 'The vehicle was unable to perform your request because it is currently unreachable.'
result = subject.batch(attributes)
expect(result.instance_of?(OpenStruct)).to eq(true)
expect(result.odometer.instance_of?(OpenStruct)).to eq(true)
expect(result.is_a?(OpenStruct)).to eq(true)
expect(result.odometer.is_a?(OpenStruct)).to eq(true)
expect { result.location }.to(raise_error do |error|
expect(error.status_code).to eq(409)
expect(error.type).to eq('VEHICLE_STATE')
Expand Down

0 comments on commit c61e801

Please sign in to comment.