Skip to content

Commit fa3d55a

Browse files
committed
dont hoist to objects for to/from message params
1 parent d71dcc7 commit fa3d55a

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lib/intercom/traits/api_resource.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def accessors_already_defined?(attribute)
6767
end
6868

6969
def set_property(attribute, value)
70-
if typed_value?(value) && !custom_attribute_field?(attribute)
70+
if typed_value?(value) && !custom_attribute_field?(attribute) && !message_from_field?(attribute, value) && !message_to_field?(attribute, value)
7171
value_to_set = Intercom::Lib::TypedJsonDeserializer.new(value).deserialize
7272
elsif flat_store_attribute?(attribute)
7373
value_to_set = Intercom::Lib::FlatStore.new(value)
@@ -80,6 +80,14 @@ def set_property(attribute, value)
8080
def custom_attribute_field?(attribute)
8181
attribute == 'custom_attributes'
8282
end
83+
84+
def message_from_field?(attribute, value)
85+
attribute.to_s == 'from' && value.is_a?(Hash) && value['type']
86+
end
87+
88+
def message_to_field?(attribute, value)
89+
attribute.to_s == 'to' && value.is_a?(Hash) && value['type']
90+
end
8391

8492
def typed_value?(value)
8593
value.is_a? Hash and !!value['type']

spec/unit/intercom/message_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'spec_helper'
2+
3+
describe "Intercom::Message" do
4+
5+
let (:user) {Intercom::User.new("email" => "[email protected]", :user_id => "12345", :created_at => Time.now, :name => "Jim Bob")}
6+
7+
it 'creates an user message with symbol keys' do
8+
Intercom.expects(:post).with('/messages', {'from' => { :type => 'user', :email => '[email protected]'}, 'body' => 'halp'}).returns(:status => 200)
9+
Intercom::Message.create(:from => { :type => "user", :email => "[email protected]" }, :body => "halp")
10+
end
11+
12+
it "creates an user message with string keys" do
13+
Intercom.expects(:post).with('/messages', {'from' => { 'type' => 'user', 'email' => '[email protected]'}, 'body' => 'halp'}).returns(:status => 200)
14+
Intercom::Message.create('from' => { 'type' => "user", 'email' => "[email protected]" }, 'body' => "halp")
15+
end
16+
17+
it "creates a admin message" do
18+
Intercom.expects(:post).with('/messages', {'from' => { 'type' => "admin", 'id' => "1234" }, 'to' => { 'type' => 'user', 'id' => '5678' }, 'body' => 'halp', 'message_type' => 'inapp'}).returns(:status => 200)
19+
Intercom::Message.create('from' => { 'type' => "admin", 'id' => "1234" }, :to => { 'type' => 'user', 'id' => '5678' }, 'body' => "halp", 'message_type' => 'inapp')
20+
end
21+
end

0 commit comments

Comments
 (0)