From dfd0e38e52e4fdeb8f068f2dc37a53eb3d16e5b7 Mon Sep 17 00:00:00 2001 From: Misha Gorodnitzky Date: Tue, 28 Jun 2022 09:47:22 +0100 Subject: [PATCH] handle data that have JSON-friendly representation When converting event data into the JSON payload to send to BQ, have special handling for Hash objects. However, some data will come in as on object that isn't immediately convertable to something JSON-friendly, such as models (e.g. StoreModel or ActiveModel objects). This change converts everything to a more standard JSON-friendly representation before processing for sending to BQ, meaning that models are converted to a Hash object which we can then to_json as needed. --- lib/dfe/analytics/event.rb | 2 ++ spec/dfe/analytics/event_spec.rb | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/dfe/analytics/event.rb b/lib/dfe/analytics/event.rb index e1f97f5b..50c74d7b 100644 --- a/lib/dfe/analytics/event.rb +++ b/lib/dfe/analytics/event.rb @@ -101,6 +101,8 @@ def with_request_uuid(request_id) def hash_to_kv_pairs(hash) hash.map do |(key, value)| + value = value.try(:as_json) + if value.in? [true, false] value = value.to_s elsif value.is_a?(Hash) diff --git a/spec/dfe/analytics/event_spec.rb b/spec/dfe/analytics/event_spec.rb index af74a03f..722cdb7b 100644 --- a/spec/dfe/analytics/event_spec.rb +++ b/spec/dfe/analytics/event_spec.rb @@ -62,6 +62,17 @@ end describe 'data pairs' do + let(:has_as_json_class) do + Struct.new(:colour, :is_cat) do + def as_json + { + colour: colour, + is_cat: is_cat + } + end + end + end + it 'converts booleans to strings' do event = described_class.new output = event.with_data(key: true).as_json @@ -73,6 +84,12 @@ output = event.with_data(key: { equality_and_diversity: { ethnic_background: 'Irish' } }).as_json expect(output['data'].first['value']).to eq ['{"equality_and_diversity":{"ethnic_background":"Irish"}}'] end + + it 'handles objects that have JSON-friendly structures' do + event = described_class.new + output = event.with_data(as_json_object: has_as_json_class.new(:green, true)).as_json + expect(output['data'].first['value']).to eq ['{"colour":"green","is_cat":true}'] + end end def fake_request(overrides = {})