Skip to content

Commit 0d3e6a9

Browse files
Remove the hash validation and remove them from the payload on save (#619)
* Remove the hash validation and remove them from the payload on save * Update tests for contact and user * Fix typo
1 parent 65d32ac commit 0d3e6a9

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

lib/intercom/lib/flat_store.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Intercom
22
module Lib
33

44
# Sub-class of {Hash} for storing custom data attributes.
5-
# Doesn't allow nested Hashes or Arrays. And requires {String} or {Symbol} keys.
5+
# Doesn't allow Arrays. And requires {String} or {Symbol} keys.
66
class FlatStore < Hash
77

88
def initialize(attributes={})
@@ -21,9 +21,16 @@ def [](key)
2121
super(key.to_s)
2222
end
2323

24+
def to_submittable_hash
25+
# Filter out Custom Object references when submitting to API
26+
self.reject do |key, value|
27+
value.is_a?(Hash)
28+
end
29+
end
30+
2431
private
2532
def validate_key_and_value(key, value)
26-
raise ArgumentError.new("This does not support nested data structures (key: #{key}, value: #{value}") if value.is_a?(Array) || value.is_a?(Hash)
33+
raise ArgumentError.new("This does not support nested data structures (key: #{key}, value: #{value}") if value.is_a?(Array)
2734
raise ArgumentError.new("Key must be String or Symbol: #{key}") unless key.is_a?(String) || key.is_a?(Symbol)
2835
end
2936
end

spec/unit/intercom/contact_spec.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,10 @@
9090
_(contact.to_hash['custom_attributes']).must_equal 'mad' => 123, 'other' => now.to_i, 'thing' => 'yay'
9191
end
9292

93-
it 'rejects nested data structures in custom_attributes' do
93+
it 'rejects lists in custom_attributes' do
9494
contact = Intercom::Contact.new
9595

9696
_(proc { contact.custom_attributes['thing'] = [1] }).must_raise(ArgumentError)
97-
_(proc { contact.custom_attributes['thing'] = { 1 => 2 } }).must_raise(ArgumentError)
98-
_(proc { contact.custom_attributes['thing'] = { 1 => { 2 => 3 } } }).must_raise(ArgumentError)
9997

10098
contact = Intercom::Contact.new(test_contact)
10199
_(proc { contact.custom_attributes['thing'] = [1] }).must_raise(ArgumentError)

spec/unit/intercom/lib/flat_store_spec.rb

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
require 'spec_helper'
44

55
describe Intercom::Lib::FlatStore do
6-
it 'raises if you try to set or merge in nested hash structures' do
6+
it 'raises if you try to set arrays but allows hashes' do
77
data = Intercom::Lib::FlatStore.new
88
_(proc { data['thing'] = [1] }).must_raise ArgumentError
9-
_(proc { data['thing'] = { 1 => 2 } }).must_raise ArgumentError
10-
_(proc { Intercom::Lib::FlatStore.new(1 => { 2 => 3 }) }).must_raise ArgumentError
9+
10+
data['thing'] = { 'key' => 'value' }
11+
_(data['thing']).must_equal({ 'key' => 'value' })
12+
13+
flat_store = Intercom::Lib::FlatStore.new('custom_object' => { 'type' => 'Order.list', 'instances' => [{'id' => '123'}] })
14+
_(flat_store['custom_object']).must_equal({ 'type' => 'Order.list', 'instances' => [{'id' => '123'}] })
1115
end
1216

1317
it 'raises if you try to use a non string key' do
@@ -28,4 +32,30 @@
2832
_(data['b']).must_equal 2
2933
_(data[:b]).must_equal 2
3034
end
35+
36+
describe '#to_submittable_hash' do
37+
it 'filters out all hash values' do
38+
data = Intercom::Lib::FlatStore.new(
39+
'regular_attr' => 'value',
40+
'number_attr' => 42,
41+
'custom_object' => {
42+
'type' => 'Order.list',
43+
'instances' => [
44+
{ 'id' => '31', 'external_id' => 'ext_123' }
45+
]
46+
},
47+
'regular_hash' => { 'key' => 'value' },
48+
'metadata' => { 'source' => 'api', 'version' => 2 }
49+
)
50+
51+
submittable = data.to_submittable_hash
52+
53+
_(submittable['regular_attr']).must_equal 'value'
54+
_(submittable['number_attr']).must_equal 42
55+
56+
_(submittable.key?('custom_object')).must_equal false
57+
_(submittable.key?('regular_hash')).must_equal false
58+
_(submittable.key?('metadata')).must_equal false
59+
end
60+
end
3161
end

spec/unit/intercom/user_spec.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,10 @@
106106
_(user.to_hash['companies']).must_equal companies
107107
end
108108

109-
it 'rejects nested data structures in custom_attributes' do
109+
it 'rejects lists in custom_attributes' do
110110
user = Intercom::User.new
111111

112112
_(proc { user.custom_attributes['thing'] = [1] }).must_raise(ArgumentError)
113-
_(proc { user.custom_attributes['thing'] = { 1 => 2 } }).must_raise(ArgumentError)
114-
_(proc { user.custom_attributes['thing'] = { 1 => { 2 => 3 } } }).must_raise(ArgumentError)
115113

116114
user = Intercom::User.from_api(test_user)
117115
_(proc { user.custom_attributes['thing'] = [1] }).must_raise(ArgumentError)

0 commit comments

Comments
 (0)