Skip to content

Commit aff8a01

Browse files
committed
Remove the hash validation and remove them from the payload on save
1 parent 65d32ac commit aff8a01

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
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/lib/flat_store_spec.rb

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
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
1418
data = Intercom::Lib::FlatStore.new
15-
_(proc { data[1] = 'something' }).must_raise ArgumentError
19+
_(proc { data[1] = 'something' }).must_raise ArgumentErrorca
1620
end
1721

1822
it 'sets and merges valid entries' 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

0 commit comments

Comments
 (0)