Skip to content

Commit d1fe329

Browse files
committed
Support for Notes.
Fixes intercomgh-9.
1 parent b8baa5e commit d1fe329

File tree

5 files changed

+192
-2
lines changed

5 files changed

+192
-2
lines changed

intercom/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ def from_timestamp_property(func_to_decorate):
1717
def wrapper(instance):
1818
""" Closure that converts from timestamp to datetime. """
1919
value = func_to_decorate(instance)
20-
return datetime.fromtimestamp(value)
20+
if value:
21+
return datetime.fromtimestamp(value)
2122
return wrapper
2223

2324
def to_timestamp_property(func_to_decorate):
@@ -26,7 +27,8 @@ def to_timestamp_property(func_to_decorate):
2627
@functools.wraps(func_to_decorate)
2728
def wrapper(instance, value):
2829
""" Closure that converts from datetime to timestamp. """
29-
value = time.mktime(value.timetuple())
30+
if value:
31+
value = time.mktime(value.timetuple())
3032
func_to_decorate(instance, value)
3133
return wrapper
3234

@@ -37,4 +39,5 @@ def wrapper(instance, value):
3739

3840
from .impression import Impression
3941
from .message_thread import MessageThread
42+
from .note import Note
4043
from .user import User

intercom/intercom.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,23 @@ def create_impression(cls, user_id=None, email=None, user_ip=None,
191191
params=params)
192192
return user_dict
193193

194+
@classmethod
195+
def create_note(cls, user_id=None, email=None, body=None):
196+
""" Create a note.
197+
198+
>>> result = Intercom.create_note(email="somebody@example.com",
199+
... body="This is the text of my note.")
200+
>>> result['html']
201+
u'<p>This is the text of my note.</p>'
202+
>>> result['user']['email']
203+
u'somebody@example.com'
204+
205+
"""
206+
params = { 'email': email, 'user_id': user_id, 'body': body }
207+
user_dict = Intercom._call('POST', Intercom.api_endpoint + 'users/notes',
208+
params=params)
209+
return user_dict
210+
194211
@classmethod
195212
def get_message_threads(cls, user_id=None, email=None, thread_id=None):
196213
""" If a thread_id is specified, this returns a specific MessageThread

intercom/note.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# coding=utf-8
2+
#
3+
# Copyright 2013 keyes.ie
4+
#
5+
# License: http://jkeyes.mit-license.org/
6+
#
7+
""" Note module.
8+
9+
>>> from intercom import Intercom
10+
>>> Intercom.app_id = 'dummy-app-id'
11+
>>> Intercom.api_key = 'dummy-api-key'
12+
>>> from intercom import Note
13+
14+
"""
15+
16+
from . import Intercom
17+
from . import from_timestamp_property
18+
from .user import User
19+
from .user import UserId
20+
21+
class Note(UserId):
22+
""" A note on a User. """
23+
24+
@classmethod
25+
def create(cls, user_id=None, email=None, body=None):
26+
""" Create a Note.
27+
28+
>>> note = Note.create(email="somebody@example.com",
29+
... body="This is the text of my note.")
30+
>>> note['created_at']
31+
>>> note.html
32+
u'<p>This is the text of my note.</p>'
33+
34+
"""
35+
resp = Intercom.create_note(user_id=user_id, email=email, body=body)
36+
return cls(resp)
37+
38+
def save(self):
39+
""" Create a Note from this objects properties:
40+
41+
>>> note = Note()
42+
>>> note.email = "somebody@example.com"
43+
>>> note.body = "This is the text of my note."
44+
>>> note.save()
45+
>>> note.html
46+
u'<p>This is the text of my note.</p>'
47+
48+
"""
49+
resp = Intercom.create_note(user_id=self.user_id, email=self.email, body=self.body)
50+
self.update(resp)
51+
52+
@property
53+
def body(self):
54+
""" The body of the note. """
55+
return dict.get(self, 'body', None)
56+
57+
@body.setter
58+
def body(self, body):
59+
""" Set the note body. """
60+
self['body'] = body
61+
62+
@property
63+
def html(self):
64+
""" Get the html of the note – set by an API response. """
65+
return dict.get(self, 'html', None)
66+
67+
@property
68+
@from_timestamp_property
69+
def created_at(self):
70+
""" Returns the datetime this note was created – set by an API response. """
71+
return dict.get(self, 'created_at', None)
72+
73+
@property
74+
def user(self):
75+
""" Get the noted user – set by an API response. """
76+
data = dict.get(self, 'user', None)
77+
if data:
78+
return User(**data)
79+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"html": "<p>This is the text of my note.</p>",
3+
"created_at": null,
4+
"user": {
5+
"email": "somebody@example.com",
6+
"user_id": "123",
7+
"name": "Somebody",
8+
"created_at": 1270000000,
9+
"last_impression_at": 1300000000,
10+
"custom_data": {
11+
"app_name": "Genesis",
12+
"monthly_spend": 155.5,
13+
"team_mates": 7
14+
},
15+
"social_profiles": [
16+
{
17+
"type": "twitter",
18+
"url": "http://twitter.com/abc",
19+
"username": "abc"
20+
},
21+
{
22+
"type": "facebook",
23+
"url": "http://facebook.com/vanity",
24+
"username": "vanity",
25+
"id": "13241141441141413"
26+
}
27+
],
28+
"location_data": {
29+
"city_name": "Santiago",
30+
"continent_code": "SA",
31+
"country_name": "Chile",
32+
"latitude": -33.44999999999999,
33+
"longitude": -70.6667,
34+
"postal_code": "",
35+
"region_name": "12",
36+
"timezone": "Chile/Continental",
37+
"country_code": "CHL"
38+
},
39+
"session_count": 0,
40+
"last_seen_ip": "127.0.0.1",
41+
"last_seen_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
42+
"relationship_score": 90,
43+
"unsubscribed_from_emails": false
44+
}
45+
}

tests/unit/test_note.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#
2+
# Copyright 2013 keyes.ie
3+
#
4+
# License: http://jkeyes.mit-license.org/
5+
#
6+
7+
from mock import patch
8+
from nose.tools import raises
9+
from unittest import TestCase
10+
11+
from . import create_response
12+
13+
from intercom.intercom import AuthenticationError
14+
from intercom import Note
15+
16+
class NoteTest(TestCase):
17+
18+
@raises(AuthenticationError)
19+
@patch('requests.request', create_response(401))
20+
def test_create_note_identifiers(self):
21+
Note.create()
22+
23+
@patch('requests.request', create_response(200, 'create_note_valid.json'))
24+
def test_create_note_valid(self):
25+
note = Note.create(email='xxx@example.com')
26+
self.assertEqual("<p>This is the text of my note.</p>", note.html)
27+
28+
# check params
29+
note = Note.create(email='xxx@example.com', body="home")
30+
self.assertEqual("<p>This is the text of my note.</p>", note.html)
31+
32+
def test_properties(self):
33+
note = Note()
34+
note.body = 'xxx'
35+
note.email = 'xxx@example.com'
36+
note.user_id = '123'
37+
38+
self.assertEqual('xxx', note.body)
39+
self.assertEqual('xxx@example.com', note.email)
40+
self.assertEqual('123', note.user_id)
41+
42+
@patch('requests.request', create_response(200, 'create_note_valid.json'))
43+
def test_save(self):
44+
note = Note(email='xxx@example.com')
45+
note.save()
46+
self.assertEqual("<p>This is the text of my note.</p>", note.html)

0 commit comments

Comments
 (0)