Skip to content

Commit 3bf84cf

Browse files
committed
Added in basic support for Companies based on the dicussion in intercom#10
The basic usage to set a user's companies is... user = User(email="foo@bar.com") user.companies = [ {"id":6, "name": "Intercom", "created_at": datetime.now()}, {"id":7, "name": "GitHub", "created_at": datetime.now()}] # Inspired by the ruby client. user.company = {"id":6, "name": "Intercom", "created_at": datetime.now()} As the Intercom API does not support returning company info, an AttributeError is rasied if the getter of the company/companies properties is called. Added in test coverage for the new Company object and company setters.
1 parent 35c6396 commit 3bf84cf

File tree

3 files changed

+137
-6
lines changed

3 files changed

+137
-6
lines changed

intercom/intercom.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ class Intercom(object):
8484
def _call(cls, method, url, params=None):
8585
""" Construct an API request, send it to the API, and parse the
8686
response. """
87-
8887
req_params = {}
8988
headers = {
9089
'User-Agent': 'python-intercom/' + __version__,

intercom/user.py

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class User(UserId):
5050
attributes = (
5151
'user_id', 'email', 'name', 'created_at', 'custom_data',
5252
'last_seen_ip', 'last_seen_user_agent', 'last_impression_at',
53-
'last_request_at', 'unsubscribed_from_emails')
53+
'last_request_at', 'unsubscribed_from_emails', 'companies')
5454

5555
@classmethod
5656
def find(cls, user_id=None, email=None):
@@ -296,6 +296,59 @@ def unsubscribed_from_emails(self, unsubscribed_from_emails):
296296
""" Sets whether or not the user has unsubscribed from email """
297297
self['unsubscribed_from_emails'] = unsubscribed_from_emails
298298

299+
@property
300+
def company(self):
301+
""" Get the company of a user. Currently unsupported by the Intercom
302+
API so an AttributeError is raised.
303+
304+
>>> user = User(email="somebody@example.com")
305+
>>> user.companies
306+
Traceback (most recent call last):
307+
...
308+
AttributeError("company is a write-only property")
309+
"""
310+
raise AttributeError("company is a write-only property")
311+
312+
@company.setter
313+
def company(self, company):
314+
""" Sets the company for a user.
315+
316+
>>> user = User(email="somebody@example.com")
317+
>>> user.company = {'id':6, 'name': 'Intercom', 'created_at': 103201}
318+
"""
319+
if isinstance(company, dict):
320+
self['companies'] = [Company(**company)]
321+
elif isinstance(company, Company):
322+
self['companies'] = [company]
323+
else:
324+
raise ValueError("company must be set as a dict or Company object")
325+
326+
@property
327+
def companies(self):
328+
""" Get the companies of a user. Currently unsupported by the Intercom
329+
API so an AttributeError is raised.
330+
331+
>>> user = User(email="somebody@example.com")
332+
>>> user.companies
333+
Traceback (most recent call last):
334+
...
335+
AttributeError("company is a write-only property")
336+
"""
337+
raise AttributeError("companies is a write-only property")
338+
339+
@companies.setter
340+
def companies(self, companies):
341+
""" Sets the companies for the user
342+
343+
>>> user = User(email="somebody@example.com")
344+
>>> user.companies = [{'id': 6, 'name': 'Intercom', 'created_at': 103201}]
345+
"""
346+
#Ensure a companies is set as a list.
347+
if isinstance(companies, list):
348+
self['companies'] = [Company(**c) for c in companies]
349+
else:
350+
raise ValueError("companies must be set as a list")
351+
299352
@property
300353
def custom_data(self):
301354
""" Returns a CustomData object for this User.
@@ -406,6 +459,40 @@ def __setitem__(self, key, value):
406459
""" Do not allow items to be set. """
407460
raise NotImplementedError
408461

462+
class Company(dict):
463+
""" Object represents an Intercom Company """
464+
465+
@property
466+
def id(self):
467+
""" Returns the company's id. """
468+
return dict.get(self, 'id', None)
469+
470+
@id.setter
471+
def id(self, company_id):
472+
""" Sets thecompany's id. """
473+
self['id'] = company_id
474+
475+
@property
476+
def name(self):
477+
""" Returns the company name e.g. Intercom. """
478+
return dict.get(self, 'name', None)
479+
480+
@name.setter
481+
def name(self, name):
482+
""" Sets the company name. """
483+
self['name'] = name
484+
485+
@property
486+
@from_timestamp_property
487+
def created_at(self):
488+
""" Returns the datetime this Company was created. """
489+
return dict.get(self, 'created_at', None)
490+
491+
@created_at.setter
492+
@to_timestamp_property
493+
def created_at(self, value):
494+
""" Sets the timestamp when this Company was created. """
495+
self['created_at'] = value
409496

410497
class LocationData(dict):
411498
""" Object representing a user's location data

tests/unit/test_user.py

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
from intercom.user import LocationData
2121
from intercom.user import SocialProfile
2222
from intercom.user import User
23+
from intercom.user import Company
24+
2325

2426
class CustomDataTest(TestCase):
2527
""" Test the CustomData object. """
@@ -98,6 +100,20 @@ def test_setitem(self):
98100
social_profile = SocialProfile()
99101
social_profile['type'] = 'facebook'
100102

103+
class CompanyTest(TestCase):
104+
""" Test the Company object. """
105+
106+
def test_properties(self):
107+
c_dict = {
108+
'id': 1,
109+
'name': 'Intercom',
110+
'created_at': 1331764344
111+
}
112+
company = Company(c_dict)
113+
self.assertEqual(1, company.id)
114+
self.assertEqual('Intercom', company.name)
115+
self.assertEqual(datetime.fromtimestamp(1331764344), company.created_at)
116+
101117
class UsersTest(TestCase):
102118

103119
@patch('requests.request', create_response(200, 'create_user_valid.json'))
@@ -133,10 +149,10 @@ def test_find_by_email(self):
133149
self.assertEqual('Mozilla/5.0', user.last_seen_user_agent)
134150
self.assertEqual(50, user.relationship_score)
135151
self.assertTrue(isinstance(user.last_impression_at, datetime))
136-
self.assertEqual(1331834352,
152+
self.assertEqual(1331834352,
137153
time.mktime(user.last_impression_at.timetuple()))
138154
self.assertTrue(isinstance(user.created_at, datetime))
139-
self.assertEqual(1331764344,
155+
self.assertEqual(1331764344,
140156
time.mktime(user.created_at.timetuple()))
141157
self.assertTrue(1, len(user.social_profiles))
142158
profile = user.social_profiles[0]
@@ -149,6 +165,8 @@ def test_find_by_email(self):
149165
self.assertEqual('Santiago', user.location_data.city_name)
150166
self.assertTrue(isinstance(user.location_data, LocationData))
151167
self.assertEqual('johnny', user.custom_data['nick'])
168+
with self.assertRaises(AttributeError):
169+
user.companies
152170

153171
@patch('requests.request', create_response(200, 'get_user_id_valid.json'))
154172
def test_find_by_user_id(self):
@@ -181,7 +199,10 @@ def test_properties(self):
181199
user.last_seen_user_agent = 'Mozilla/5.0'
182200
user.created_at = datetime.fromtimestamp(1331764344)
183201
user.custom_data = { 'name': 'Ace' }
184-
202+
user.companies = [{
203+
'id': 1,
204+
'name':' Intercom',
205+
'created_at': datetime.fromtimestamp(1331764344)}]
185206
try:
186207
# cannot set the relationship score
187208
user.relationship_score = 50
@@ -194,9 +215,33 @@ def test_properties(self):
194215
self.assertEqual('192.168.1.100', user.last_seen_ip)
195216
self.assertEqual('Mozilla/5.0', user.last_seen_user_agent)
196217
self.assertEqual(None, user.relationship_score)
197-
self.assertEqual(1331764344,
218+
self.assertEqual(1331764344,
198219
time.mktime(user.created_at.timetuple()))
199220
self.assertEqual('Ace', user.custom_data['name'])
221+
with self.assertRaises(AttributeError):
222+
user.companies
223+
224+
def test_company(self):
225+
user = User()
226+
user.company = {
227+
'id': 1,
228+
'name':' Intercom',
229+
'created_at': datetime.fromtimestamp(1331764344)}
230+
with self.assertRaises(AttributeError):
231+
user.company
232+
with self.assertRaises(ValueError):
233+
user.company = ['foo']
234+
235+
def test_companies(self):
236+
user = User()
237+
user.companies = [{
238+
'id': 1,
239+
'name':' Intercom',
240+
'created_at': datetime.fromtimestamp(1331764344)}]
241+
with self.assertRaises(AttributeError):
242+
user.companise
243+
with self.assertRaises(ValueError):
244+
user.companies = {'foo':'bar'}
200245

201246
class LocationDataTest(TestCase):
202247

0 commit comments

Comments
 (0)