Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/intercom/lib/flat_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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={})
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions spec/unit/intercom/contact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 33 additions & 3 deletions spec/unit/intercom/lib/flat_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
4 changes: 1 addition & 3 deletions spec/unit/intercom/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down