Skip to content

Commit

Permalink
Merge pull request #597 from intercom/sp/bug_fix
Browse files Browse the repository at this point in the history
To fix events summary endpoint response
  • Loading branch information
suma-prakash authored Dec 7, 2022
2 parents 0bf8bbe + a6a2460 commit c32fe00
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ intercom.events.create(

# Retrieve event list for user with id:'123abc'
intercom.events.find_all("type" => "user", "intercom_user_id" => "123abc")

# Retrieve the event summary for user with id: 'abc' this will return an event object with the following characteristics:
# name - name of the event
# first - time when event first occured.
# last - time when event last occured
# count - number of times the event occured
# description - description of the event
events = intercom.events.find_all(type: 'user',intercom_user_id: 'abc',summary: true)
```

Metadata Objects support a few simple types that Intercom can present on your behalf
Expand Down Expand Up @@ -350,6 +358,8 @@ intercom.segments.all.each {|segment| puts "id: #{segment.id} name: #{segment.na
# Iterate over all conversations for your app
intercom.conversations.all.each { |convo| ... }

# The below method of finding conversations by using the find_all method work only for API versions 2.5 and below

# FINDING CONVERSATIONS FOR AN ADMIN
# Iterate over all conversations (open and closed) assigned to an admin
intercom.conversations.find_all(type: 'admin', id: '7').each {|convo| ... }
Expand Down
6 changes: 5 additions & 1 deletion lib/intercom/base_collection_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ def deserialize_response_hash(response_hash, block)
Utils.entity_key_from_type(top_level_type)
end
response_hash[top_level_entity_key].each do |object_json|
block.call Lib::TypedJsonDeserializer.new(object_json, @client).deserialize
if top_level_type == 'event.summary'
block.call Lib::TypedJsonDeserializer.new(object_json, @client, top_level_type).deserialize
else
block.call Lib::TypedJsonDeserializer.new(object_json, @client).deserialize
end
end
end

Expand Down
11 changes: 8 additions & 3 deletions lib/intercom/lib/typed_json_deserializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ module Lib
class TypedJsonDeserializer
attr_reader :json

def initialize(json, client)
def initialize(json, client, type = nil)
@json = json
@client = client
@type = type
end

def deserialize
Expand All @@ -27,7 +28,7 @@ def deserialize
private

def blank_object_type?(object_type)
object_type.nil? || object_type == ''
object_type.nil? || object_type == '' && @type.nil?
end

def list_object_type?(object_type)
Expand All @@ -48,7 +49,11 @@ def deserialize_object(object_json)
end

def object_type
@object_type ||= json['type']
if !@type.nil?
@object_type = @type
else
@object_type ||= json['type']
end
end

def object_entity_key
Expand Down
1 change: 1 addition & 0 deletions lib/intercom/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def entity_key_from_type(type)

is_list = type.split('.')[1] == 'list'
entity_name = type.split('.')[0]
return Utils.pluralize(entity_name) if entity_name == 'event'
is_list ? Utils.pluralize(entity_name) : entity_name
end
end
Expand Down
7 changes: 7 additions & 0 deletions spec/unit/intercom/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
_(event_names).must_equal %W(invited-friend)
end

it "able to fetch event summary" do
client.expects(:get).with("/events", type: 'user', email: '[email protected]', summary: true).returns(page_of_events(false))
event_names = []
client.events.find_all(type: 'user', email: '[email protected]',summary: true).each { |event| event_names << event.event_name }
_(event_names).must_equal %W(invited-friend)
end

it "keeps iterating if next link" do
client.expects(:get).with("/events", type: 'user', email: '[email protected]').returns(page_of_events(true))
client.expects(:get).with("https://api.intercom.io/events?type=user&intercom_user_id=55a3b&before=144474756550", {}).returns(page_of_events(false))
Expand Down

0 comments on commit c32fe00

Please sign in to comment.