Skip to content

Commit 9475048

Browse files
thewheatchoran
authored andcommitted
Support Event Summaries (#244)
1 parent dd8ece4 commit 9475048

File tree

6 files changed

+265
-3
lines changed

6 files changed

+265
-3
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,19 @@ EventCollection events = Event.list(params);
389389
while (events.hasNext()) {
390390
System.out.println(events.next().getEventName());
391391
}
392+
393+
// List event summaries of a user
394+
Map<String, String> params = Maps.newHashMap();
395+
params.put("type", "user");
396+
params.put("user_id", "1");
397+
// Alternatively list by Intercom ID
398+
// params.put("intercom_user_id", "541a144b201ebf2ec5000001");
399+
// Or by email
400+
// params.put("email", "[email protected]");
401+
EventSummaryCollection eventSummaryCollection = Event.listSummary(params);
402+
for(EventSummary eventSummary : eventSummaryCollection.getEventSummaries()){
403+
System.out.println(eventSummary);
404+
}
392405
```
393406

394407
### Tags

intercom-java/src/main/java/io/intercom/api/Event.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
import com.google.common.collect.Lists;
99
import com.google.common.collect.Maps;
1010

11-
import java.net.URI;
1211
import java.util.ArrayList;
13-
import java.util.Arrays;
14-
import java.util.HashMap;
1512
import java.util.List;
1613
import java.util.Map;
1714

@@ -60,6 +57,14 @@ public static EventCollection list(Map<String, String> params) throws InvalidExc
6057
return DataResource.list(params, "events", EventCollection.class);
6158
}
6259

60+
public static EventSummaryCollection listSummary(Map<String, String> params) throws InvalidException, AuthorizationException {
61+
if ((!params.containsKey("email")) && (!params.containsKey("user_id")) && (!params.containsKey("intercom_user_id"))) {
62+
throw new InvalidException("an event query must include an email, user_id or intercom_user_id parameter");
63+
}
64+
params.put("summary", "true");
65+
return DataResource.list(params, "events", EventSummaryCollection.class);
66+
}
67+
6368
@VisibleForTesting
6469
static List<JobItem<Event>> validateJobItems(List<JobItem<Event>> items) {
6570
final JobSupport jobSupport = new JobSupport();
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package io.intercom.api;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
7+
import java.text.ParseException;
8+
import java.text.SimpleDateFormat;
9+
import java.util.Date;
10+
11+
@SuppressWarnings("UnusedDeclaration")
12+
@JsonIgnoreProperties(ignoreUnknown = true)
13+
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
14+
public class EventSummary {
15+
16+
@JsonProperty("name")
17+
private String name;
18+
19+
@JsonProperty("first")
20+
private String first;
21+
22+
@JsonProperty("last")
23+
private String last;
24+
25+
@JsonProperty("count")
26+
private int count;
27+
28+
@JsonProperty("description")
29+
private String description;
30+
31+
public EventSummary() {
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public String getFirstOccurredAtString() {
39+
return first;
40+
}
41+
42+
public Date getFirstOccurredAt() throws ParseException {
43+
return stringToDate(first);
44+
}
45+
46+
public String getLastOccurredAtString() {
47+
return last;
48+
}
49+
50+
public Date getLastOccurredAt() throws ParseException {
51+
return stringToDate(last);
52+
}
53+
54+
public int getCount() {
55+
return count;
56+
}
57+
58+
public String getDescription() {
59+
return description;
60+
}
61+
62+
private Date stringToDate(String dateString) throws ParseException {
63+
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX").parse(dateString);
64+
}
65+
66+
@Override
67+
public boolean equals(Object o) {
68+
if (this == o) return true;
69+
if (o == null || getClass() != o.getClass()) return false;
70+
71+
EventSummary eventSummary = (EventSummary) o;
72+
73+
if (name != null ? !name.equals(eventSummary.name) : eventSummary.name != null) return false;
74+
if (first != null ? !first.equals(eventSummary.first) : eventSummary.first != null) return false;
75+
if (last != null ? !last.equals(eventSummary.last) : eventSummary.last != null) return false;
76+
if (count != eventSummary.count) return false;
77+
if (description != null ? !description.equals(eventSummary.description) : eventSummary.description != null) return false;
78+
79+
return true;
80+
}
81+
82+
@Override
83+
public int hashCode() {
84+
int result = (name!= null ? name.hashCode() : 0);
85+
result = 31 * result + (description != null ? description.hashCode() : 0);
86+
result = 31 * result + (first != null ? first.hashCode() : 0);
87+
result = 31 * result + (last != null ? last.hashCode() : 0);
88+
result = 31 * result + count;
89+
return result;
90+
}
91+
@Override
92+
public String toString() {
93+
return "EventSummaryCollection{" +
94+
"name='" + name+ '\'' +
95+
", count=" + count +
96+
", description='" + description + '\'' +
97+
", first='" + first + '\'' +
98+
", last='" + last + '\'' +
99+
"} " + super.toString();
100+
}
101+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package io.intercom.api;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
7+
import java.util.List;
8+
9+
@SuppressWarnings("UnusedDeclaration")
10+
@JsonIgnoreProperties(ignoreUnknown = true)
11+
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
12+
public class EventSummaryCollection extends TypedData {
13+
14+
@JsonProperty("type")
15+
private final String type = "event.summary";
16+
17+
@JsonProperty("email")
18+
private String email;
19+
20+
@JsonProperty("user_id")
21+
private String userID;
22+
23+
@JsonProperty("intercom_user_id")
24+
private String intercomUserID;
25+
26+
@JsonProperty("events")
27+
private List<EventSummary> eventSummaries;
28+
29+
public EventSummaryCollection() {
30+
}
31+
32+
public String getType() {
33+
return type;
34+
}
35+
36+
public String getEmail() {
37+
return email;
38+
}
39+
40+
public String getUserID() {
41+
return userID;
42+
}
43+
44+
public String getIntercomUserID() {
45+
return intercomUserID ;
46+
}
47+
48+
public List<EventSummary> getEventSummaries() {
49+
return eventSummaries;
50+
}
51+
52+
@Override
53+
public boolean equals(Object o) {
54+
if (this == o) return true;
55+
if (o == null || getClass() != o.getClass()) return false;
56+
57+
EventSummaryCollection eventSummaryCollection = (EventSummaryCollection) o;
58+
59+
if (email != null ? !email.equals(eventSummaryCollection.email) : eventSummaryCollection.email != null) return false;
60+
if (userID != null ? !userID.equals(eventSummaryCollection.userID) : eventSummaryCollection.userID != null) return false;
61+
if (intercomUserID!= null ? !intercomUserID.equals(eventSummaryCollection.intercomUserID) : eventSummaryCollection.intercomUserID != null) return false;
62+
if (eventSummaries!= null ? !eventSummaries.equals(eventSummaryCollection.eventSummaries) : eventSummaryCollection.eventSummaries != null) return false;
63+
if (!type.equals(eventSummaryCollection.type)) return false;
64+
65+
return true;
66+
}
67+
68+
@Override
69+
public int hashCode() {
70+
int result = type.hashCode();
71+
result = 31 * result + (email != null ? email.hashCode() : 0);
72+
result = 31 * result + (userID != null ? userID.hashCode() : 0);
73+
result = 31 * result + (intercomUserID != null ? intercomUserID.hashCode() : 0);
74+
result = 31 * result + (eventSummaries != null ? eventSummaries.hashCode() : 0);
75+
return result;
76+
}
77+
78+
@Override
79+
public String toString() {
80+
return "Event{" +
81+
"type='" + type + '\'' +
82+
", email='" + email + '\'' +
83+
", userID='" + userID + '\'' +
84+
", intercomUserID='" + intercomUserID + '\'' +
85+
", eventSummaries=" + eventSummaries +
86+
"} " + super.toString();
87+
}
88+
}

intercom-java/src/test/java/io/intercom/api/EventTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.junit.BeforeClass;
66
import org.junit.Test;
77

8+
import java.util.List;
9+
810
import static io.intercom.api.TestSupport.load;
911
import static org.junit.Assert.*;
1012

@@ -214,5 +216,29 @@ public void testListing() throws Exception {
214216
assertEquals("ADDAFRIEND", eventWithMetadata.getMetadata().get("invite_code"));
215217
assertEquals(null, eventWithMetadata.getMetadata().get("non_existing_key"));
216218
}
219+
220+
@Test
221+
public void testEventSummaryParsing() throws Exception {
222+
String json = load("event_summary.json");
223+
final EventSummaryCollection eventSummaryCollection = mapper.readValue(json, EventSummaryCollection.class);
224+
assertEquals("event.summary", eventSummaryCollection.getType());
225+
assertEquals("[email protected]", eventSummaryCollection.getEmail());
226+
assertEquals("530370b477ad7120001d", eventSummaryCollection.getIntercomUserID());
227+
assertEquals("25",eventSummaryCollection.getUserID());
228+
List<EventSummary> eventSummaries = eventSummaryCollection.getEventSummaries();
229+
assertEquals(3, eventSummaries.size());
230+
231+
assertEquals("test-event", eventSummaries.get(0).getName());
232+
assertEquals("2016-12-22T03:54:57.000Z", eventSummaries.get(0).getFirstOccurredAtString());
233+
assertEquals("2018-10-10T06:51:02.000Z", eventSummaries.get(0).getLastOccurredAtString());
234+
assertEquals(8, eventSummaries.get(0).getCount());
235+
assertEquals(null, eventSummaries.get(0).getDescription());
236+
237+
assertEquals("clicked-button", eventSummaries.get(1).getName());
238+
assertEquals("2018-02-20T06:40:16.000Z", eventSummaries.get(1).getFirstOccurredAtString());
239+
assertEquals("2018-02-20T06:40:16.000Z", eventSummaries.get(1).getLastOccurredAtString());
240+
assertEquals(1, eventSummaries.get(1).getCount());
241+
assertEquals("Clicking home page button", eventSummaries.get(1).getDescription());
242+
}
217243
}
218244

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"type": "event.summary",
3+
"email": "[email protected]",
4+
"intercom_user_id": "530370b477ad7120001d",
5+
"user_id": "25",
6+
"events": [
7+
{
8+
"name": "test-event",
9+
"first": "2016-12-22T03:54:57.000Z",
10+
"last": "2018-10-10T06:51:02.000Z",
11+
"count": 8,
12+
"description": null
13+
},
14+
{
15+
"name": "clicked-button",
16+
"first": "2018-02-20T06:40:16.000Z",
17+
"last": "2018-02-20T06:40:16.000Z",
18+
"count": 1,
19+
"description": "Clicking home page button"
20+
},
21+
{
22+
"name": "completed purchase",
23+
"first": "2018-03-13T02:52:53.000Z",
24+
"last": "2018-03-13T02:58:31.000Z",
25+
"count": 4,
26+
"description": null
27+
}
28+
]
29+
}

0 commit comments

Comments
 (0)