diff --git a/lib/intercom/lib/flat_store.rb b/lib/intercom/lib/flat_store.rb index 6971ca75..2077a2bd 100644 --- a/lib/intercom/lib/flat_store.rb +++ b/lib/intercom/lib/flat_store.rb @@ -2,7 +2,7 @@ module Intercom module Lib # Sub-class of {Hash} for storing custom data attributes. - # Doesn't allow nested Hashes or Arrays. And requires {String} or {Symbol} keys. + # Doesn't allow Arrays. And requires {String} or {Symbol} keys. class FlatStore < Hash def initialize(attributes={}) @@ -21,9 +21,16 @@ def [](key) super(key.to_s) end + def to_submittable_hash + # Filter out Custom Object references when submitting to API + self.reject do |key, value| + value.is_a?(Hash) + end + end + private def validate_key_and_value(key, value) - raise ArgumentError.new("This does not support nested data structures (key: #{key}, value: #{value}") if value.is_a?(Array) || value.is_a?(Hash) + raise ArgumentError.new("This does not support nested data structures (key: #{key}, value: #{value}") if value.is_a?(Array) raise ArgumentError.new("Key must be String or Symbol: #{key}") unless key.is_a?(String) || key.is_a?(Symbol) end end diff --git a/spec/unit/intercom/contact_spec.rb b/spec/unit/intercom/contact_spec.rb index a8ffd978..6361905d 100644 --- a/spec/unit/intercom/contact_spec.rb +++ b/spec/unit/intercom/contact_spec.rb @@ -90,12 +90,10 @@ _(contact.to_hash['custom_attributes']).must_equal 'mad' => 123, 'other' => now.to_i, 'thing' => 'yay' end - it 'rejects nested data structures in custom_attributes' do + it 'rejects lists in custom_attributes' do contact = Intercom::Contact.new _(proc { contact.custom_attributes['thing'] = [1] }).must_raise(ArgumentError) - _(proc { contact.custom_attributes['thing'] = { 1 => 2 } }).must_raise(ArgumentError) - _(proc { contact.custom_attributes['thing'] = { 1 => { 2 => 3 } } }).must_raise(ArgumentError) contact = Intercom::Contact.new(test_contact) _(proc { contact.custom_attributes['thing'] = [1] }).must_raise(ArgumentError) diff --git a/spec/unit/intercom/lib/flat_store_spec.rb b/spec/unit/intercom/lib/flat_store_spec.rb index 3188b579..412ed717 100644 --- a/spec/unit/intercom/lib/flat_store_spec.rb +++ b/spec/unit/intercom/lib/flat_store_spec.rb @@ -3,11 +3,15 @@ require 'spec_helper' describe Intercom::Lib::FlatStore do - it 'raises if you try to set or merge in nested hash structures' do + it 'raises if you try to set arrays but allows hashes' do data = Intercom::Lib::FlatStore.new _(proc { data['thing'] = [1] }).must_raise ArgumentError - _(proc { data['thing'] = { 1 => 2 } }).must_raise ArgumentError - _(proc { Intercom::Lib::FlatStore.new(1 => { 2 => 3 }) }).must_raise ArgumentError + + data['thing'] = { 'key' => 'value' } + _(data['thing']).must_equal({ 'key' => 'value' }) + + flat_store = Intercom::Lib::FlatStore.new('custom_object' => { 'type' => 'Order.list', 'instances' => [{'id' => '123'}] }) + _(flat_store['custom_object']).must_equal({ 'type' => 'Order.list', 'instances' => [{'id' => '123'}] }) end it 'raises if you try to use a non string key' do @@ -28,4 +32,30 @@ _(data['b']).must_equal 2 _(data[:b]).must_equal 2 end + + describe '#to_submittable_hash' do + it 'filters out all hash values' do + data = Intercom::Lib::FlatStore.new( + 'regular_attr' => 'value', + 'number_attr' => 42, + 'custom_object' => { + 'type' => 'Order.list', + 'instances' => [ + { 'id' => '31', 'external_id' => 'ext_123' } + ] + }, + 'regular_hash' => { 'key' => 'value' }, + 'metadata' => { 'source' => 'api', 'version' => 2 } + ) + + submittable = data.to_submittable_hash + + _(submittable['regular_attr']).must_equal 'value' + _(submittable['number_attr']).must_equal 42 + + _(submittable.key?('custom_object')).must_equal false + _(submittable.key?('regular_hash')).must_equal false + _(submittable.key?('metadata')).must_equal false + end + end end diff --git a/spec/unit/intercom/user_spec.rb b/spec/unit/intercom/user_spec.rb index fa6deddb..a570abfe 100644 --- a/spec/unit/intercom/user_spec.rb +++ b/spec/unit/intercom/user_spec.rb @@ -106,12 +106,10 @@ _(user.to_hash['companies']).must_equal companies end - it 'rejects nested data structures in custom_attributes' do + it 'rejects lists in custom_attributes' do user = Intercom::User.new _(proc { user.custom_attributes['thing'] = [1] }).must_raise(ArgumentError) - _(proc { user.custom_attributes['thing'] = { 1 => 2 } }).must_raise(ArgumentError) - _(proc { user.custom_attributes['thing'] = { 1 => { 2 => 3 } } }).must_raise(ArgumentError) user = Intercom::User.from_api(test_user) _(proc { user.custom_attributes['thing'] = [1] }).must_raise(ArgumentError)