From 9b60d035c522444d6e41d190a27ea4856d58c3f6 Mon Sep 17 00:00:00 2001 From: "Evgenii.S.Semenchuk" Date: Thu, 11 Jun 2020 10:23:08 +0300 Subject: [PATCH] normalize hash before parse the objects --- lib/json-api-vanilla/parser.rb | 15 ++++++ spec/json-api-vanilla/diff_spec.rb | 74 ++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/lib/json-api-vanilla/parser.rb b/lib/json-api-vanilla/parser.rb index 0140b85..7a5972a 100644 --- a/lib/json-api-vanilla/parser.rb +++ b/lib/json-api-vanilla/parser.rb @@ -32,6 +32,8 @@ def self.parse(json) # @param hash [Hash] parsed JSONĀ API payload. # @return [JSON::Api::Vanilla::Document] a wrapper for the objects. def self.build(hash) + hash = normalize_hash(hash) + naive_validate(hash) # Object storage. container = Module.new @@ -212,6 +214,19 @@ def self.naive_validate_relationship_object(hash) end end + def self.normalize_hash(object) + case object + when Hash + object.each_with_object({}) do |(key, value), result| + result[key.to_s] = normalize_hash(value) + end + when Array + object.map { |e| normalize_hash(e) } + else + object + end + end + class Document # @return [Object, Array] the content of the JSONĀ API data. attr_reader :data diff --git a/spec/json-api-vanilla/diff_spec.rb b/spec/json-api-vanilla/diff_spec.rb index 62e948c..6c0db1b 100644 --- a/spec/json-api-vanilla/diff_spec.rb +++ b/spec/json-api-vanilla/diff_spec.rb @@ -171,4 +171,78 @@ class TestClasses expect(subject.name).to eql(data['attributes']['name']) end end + + describe '.build(hash)' do + context 'with stringified keys' do + let(:hash) { + { + 'data' => [ + { + 'id' => 'a123', + 'type' => 'user', + 'attributes' => { + 'activated_at' => '2020-06-11' + } + } + ] + } + } + subject { described_class.build(hash) } + + it 'ceates a Document with data' do + expect(subject.data).to be_a(Array) + expect(subject.data[0].id).to eql(hash['data'][0]['id']) + expect(subject.data[0].type).to eql(hash['data'][0]['type']) + expect(subject.data[0].activated_at).to eql(hash['data'][0]['attributes']['activated_at']) + end + end + + context 'with symbolized in keys' do + let(:hash) { + { + data: [ + { + id: 'a123', + type: 'user', + attributes: { + activated_at: '2020-06-11' + } + } + ] + } + } + subject { described_class.build(hash) } + + it 'ceates a Document with data' do + expect(subject.data).to be_a(Array) + expect(subject.data[0].id).to eql(hash[:data][0][:id]) + expect(subject.data[0].type).to eql(hash[:data][0][:type]) + expect(subject.data[0].activated_at).to eql(hash[:data][0][:attributes][:activated_at]) + end + end + + end + + describe '.normalize_hash' do + let(:hash) { + { + 'data' => [ + { + id: 1, + type: 'user', + 'attributes' => { + 'activated_at' => '2020-06-11' + } + } + ] + } + } + + subject { described_class.normalize_hash(hash) } + + it 'returns hash with stringified keys' do + expect(subject['data'][0]['id']).to eql(hash['data'][0][:id]) + expect(subject['data'][0]['type']).to eql(hash['data'][0][:type]) + end + end end