Skip to content

Commit

Permalink
Disable response keys conversion for Customer resource (#91)
Browse files Browse the repository at this point in the history
* Allow json parser to skip key conversion

* Skip keys conversion for Customer resource

* Update version and change log
  • Loading branch information
kshilovskiy authored Sep 9, 2020
1 parent 6816e3d commit 5d4e63c
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 17 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# chartmogul-ruby Change Log

## Version 1.6.7 - 8 September 2020
- Allow adding Customer custom attributes in camel case

## Version 1.5.0 - 20 February 2020
- Add support for plan groups API

Expand Down

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

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

4 changes: 2 additions & 2 deletions lib/chartmogul/api/actions/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def create!
req.body = JSON.dump(serialize_for_write)
end
end
json = ChartMogul::Utils::JSONParser.parse(resp.body)
json = ChartMogul::Utils::JSONParser.parse(resp.body, skip_case_conversion: self.class.skip_case_conversion)

assign_all_attributes(json)
end
Expand All @@ -30,7 +30,7 @@ def create!(attributes = {})
req.body = JSON.dump(resource.serialize_for_write)
end
end
json = ChartMogul::Utils::JSONParser.parse(resp.body)
json = ChartMogul::Utils::JSONParser.parse(resp.body, skip_case_conversion: skip_case_conversion)

new_from_json(json)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/chartmogul/api/actions/custom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def custom_without_assign!(http_method, http_path, body_data = {})
req.body = JSON.dump(body_data)
end
end
ChartMogul::Utils::JSONParser.parse(resp.body)
ChartMogul::Utils::JSONParser.parse(resp.body, skip_case_conversion: skip_case_conversion)
end

def custom!(http_method, http_path, body_data = {})
Expand Down
2 changes: 1 addition & 1 deletion lib/chartmogul/api/actions/retrieve.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def retrieve(uuid, options = {})
req.headers['Content-Type'] = 'application/json'
end
end
json = ChartMogul::Utils::JSONParser.parse(resp.body)
json = ChartMogul::Utils::JSONParser.parse(resp.body, skip_case_conversion: skip_case_conversion)
new_from_json(json)
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/chartmogul/api/actions/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def update!
req.body = JSON.dump(serialize_for_write)
end
end
json = ChartMogul::Utils::JSONParser.parse(resp.body)

json = ChartMogul::Utils::JSONParser.parse(resp.body, skip_case_conversion: self.class.skip_case_conversion)

assign_all_attributes(json)
end
Expand All @@ -30,7 +31,7 @@ def update!(uuid, attributes = {})
req.body = JSON.dump(resource.serialize_for_write)
end
end
json = ChartMogul::Utils::JSONParser.parse(resp.body)
json = ChartMogul::Utils::JSONParser.parse(resp.body, skip_case_conversion: skip_case_conversion)

new_from_json(json)
end
Expand Down
7 changes: 6 additions & 1 deletion lib/chartmogul/api_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class APIResource < ChartMogul::Object
MAX_INTERVAL = 60
THREAD_CONNECTION_KEY = 'chartmogul_ruby.api_resource.connection'

class << self; attr_reader :resource_path, :resource_name, :resource_root_key end
class << self; attr_reader :resource_path, :resource_name, :resource_root_key, :skip_case_conversion end

def self.set_resource_path(path)
@resource_path = ChartMogul::ResourcePath.new(path)
Expand All @@ -31,6 +31,11 @@ def self.set_resource_root_key(root_key)
@resource_root_key = root_key
end

# When true, response hash keys won't be converted to snake case
def self.set_skip_case_conversion(value)
@skip_case_conversion = value
end

def self.connection
Thread.current[THREAD_CONNECTION_KEY] ||= build_connection
end
Expand Down
1 change: 1 addition & 0 deletions lib/chartmogul/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module ChartMogul
class Customer < APIResource
set_resource_name 'Customer'
set_resource_path '/v1/customers'
set_skip_case_conversion true

readonly_attr :uuid
readonly_attr :id
Expand Down
4 changes: 3 additions & 1 deletion lib/chartmogul/utils/json_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ module ChartMogul
module Utils
class JSONParser
class << self
def parse(json_string)
def parse(json_string, skip_case_conversion: false)
hash = JSON.parse(json_string, symbolize_names: true)
return hash if skip_case_conversion

HashSnakeCaser.new(hash).to_snake_keys
end

Expand Down
2 changes: 1 addition & 1 deletion lib/chartmogul/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ChartMogul
VERSION = '1.6.6'
VERSION = '1.6.7'
end
8 changes: 4 additions & 4 deletions spec/chartmogul/customer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,13 @@
it 'adds custom attributes', uses_api: true do
customer = described_class.retrieve('cus_07393ece-aab1-4255-8bcd-0ef11e24b047')
customer.add_custom_attributes!(
{ type: 'String', key: 'string_key', value: 'String Value' },
{ type: 'String', key: 'StringKey', value: 'String Value' },
{ type: 'Integer', key: 'integer_key', value: 1234 },
{ type: 'Timestamp', key: 'timestamp_key', value: Time.utc(2016, 0o1, 31) },
type: 'Boolean', key: 'boolean_key', value: true
)
expect(customer.custom_attributes).to eq(
string_key: 'String Value',
StringKey: 'String Value',
integer_key: 1234,
timestamp_key: Time.utc(2016, 0o1, 31),
boolean_key: true
Expand All @@ -226,13 +226,13 @@
it 'updates custom attributes', uses_api: true do
customer = described_class.retrieve('cus_07393ece-aab1-4255-8bcd-0ef11e24b047')
customer.update_custom_attributes!(
string_key: 'Another String Value',
StringKey: 'Another String Value',
integer_key: 5678,
timestamp_key: Time.utc(2016, 0o2, 1),
boolean_key: false
)
expect(customer.custom_attributes).to eq(
string_key: 'Another String Value',
StringKey: 'Another String Value',
integer_key: 5678,
timestamp_key: Time.utc(2016, 0o2, 1),
boolean_key: false
Expand Down
6 changes: 6 additions & 0 deletions spec/chartmogul/utils/json_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@
result = described_class.typecast_custom_attributes(attr: '2016-02-01T0000:00.000Z')
expect(result[:attr]).to be_instance_of(String)
end

it 'allows skipping camel case conversion' do
json = {'aKey': 'a_value', 'bKey': 'b_value'}.to_json
result = described_class.parse(json, skip_case_conversion: true)
expect(result.keys).to contain_exactly(:aKey,:bKey)
end
end
end

0 comments on commit 5d4e63c

Please sign in to comment.