Skip to content

Commit 9d6bc47

Browse files
authored
Merge pull request #140 from jkeyes/by_tag
Add by_tag support
2 parents 7282f53 + ea4871f commit 9d6bc47

File tree

7 files changed

+94
-10
lines changed

7 files changed

+94
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ venv
66
*.egg-info
77
*.pyc
88
.DS_Store
9+
htmlcov
910
docs/_build
1011

1112
intercom.sublime-project
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
"""Operation to return resources with a particular tag."""
3+
4+
from intercom import utils
5+
from intercom.collection_proxy import CollectionProxy
6+
7+
8+
class Tags(object):
9+
"""A mixin that provides `by_tag` functionality a resource."""
10+
11+
def by_tag(self, _id):
12+
"""Return a CollectionProxy to all the tagged resources."""
13+
collection = utils.resource_class_to_collection_name(
14+
self.collection_class)
15+
finder_url = "/%s?tag_id=%s" % (collection, _id)
16+
return CollectionProxy(
17+
self.client, self.collection_class, collection, finder_url)

intercom/service/company.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
from intercom.api_operations.save import Save
99
from intercom.api_operations.load import Load
1010
from intercom.extended_api_operations.users import Users
11+
from intercom.extended_api_operations.tags import Tags
1112
from intercom.service.base_service import BaseService
1213

1314

14-
class Company(BaseService, All, Delete, Find, FindAll, Save, Load, Users):
15+
class Company(BaseService, All, Delete, Find, FindAll, Save, Load, Users, Tags):
1516

1617
@property
1718
def collection_class(self):
1819
return company.Company
1920

20-
# require 'intercom/extended_api_operations/tags'
2121
# require 'intercom/extended_api_operations/segments'

intercom/service/user.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
from intercom.api_operations.delete import Delete
99
from intercom.api_operations.save import Save
1010
from intercom.api_operations.load import Load
11+
from intercom.extended_api_operations.tags import Tags
1112
from intercom.service.base_service import BaseService
1213

1314

14-
class User(BaseService, All, Find, FindAll, Delete, Save, Load, Submit):
15+
class User(BaseService, All, Find, FindAll, Delete, Save, Load, Submit, Tags):
1516

1617
@property
1718
def collection_class(self):

tests/unit/__init__.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,30 @@ def get_user(email="bob@example.com", name="Joe Schmoe"):
137137
}
138138

139139

140+
def get_company(name):
141+
return {
142+
"type": "company",
143+
"id": "531ee472cce572a6ec000006",
144+
"name": name,
145+
"plan": {
146+
"type": "plan",
147+
"id": "1",
148+
"name": "Paid"
149+
},
150+
"company_id": "6",
151+
"remote_created_at": 1394531169,
152+
"created_at": 1394533506,
153+
"updated_at": 1396874658,
154+
"monthly_spend": 49,
155+
"session_count": 26,
156+
"user_count": 10,
157+
"custom_attributes": {
158+
"paid_subscriber": True,
159+
"team_mates": 0
160+
}
161+
}
162+
163+
140164
def page_of_users(include_next_link=False):
141165
page = {
142166
"type": "user.list",
@@ -157,6 +181,29 @@ def page_of_users(include_next_link=False):
157181
page["pages"]["next"] = "https://api.intercom.io/users?per_page=50&page=2"
158182
return page
159183

184+
185+
def page_of_companies(include_next_link=False):
186+
page = {
187+
"type": "company.list",
188+
"pages": {
189+
"type": "pages",
190+
"page": 1,
191+
"next": None,
192+
"per_page": 50,
193+
"total_pages": 7
194+
},
195+
"companies": [
196+
get_company('ACME A'),
197+
get_company('ACME B'),
198+
get_company('ACME C')
199+
],
200+
"total_count": 3
201+
}
202+
if include_next_link:
203+
page["pages"]["next"] = "https://api.intercom.io/companies?per_page=50&page=2"
204+
return page
205+
206+
160207
test_tag = {
161208
"id": "4f73428b5e4dfc000b000112",
162209
"name": "Test Tag",

tests/unit/test_company.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# -*- coding: utf-8 -*-
1+
# -*- coding: utf-8 -*- # noqa
22

33
import intercom
44
import unittest
@@ -9,34 +9,44 @@
99
from mock import patch
1010
from nose.tools import assert_raises
1111
from nose.tools import eq_
12+
from nose.tools import ok_
1213
from nose.tools import istest
14+
from tests.unit import page_of_companies
1315

1416

15-
class CompanyTest(unittest.TestCase):
17+
class CompanyTest(unittest.TestCase): # noqa
1618

17-
def setUp(self):
19+
def setUp(self): # noqa
1820
self.client = Client()
1921

2022
@istest
21-
def it_raises_error_if_no_response_on_find(self):
23+
def it_raises_error_if_no_response_on_find(self): # noqa
2224
with patch.object(Client, 'get', return_value=None) as mock_method:
2325
with assert_raises(intercom.HttpError):
2426
self.client.companies.find(company_id='4')
2527
mock_method.assert_called_once_with('/companies', {'company_id': '4'})
2628

2729
@istest
28-
def it_raises_error_if_no_response_on_find_all(self):
30+
def it_raises_error_if_no_response_on_find_all(self): # noqa
2931
with patch.object(Client, 'get', return_value=None) as mock_method:
3032
with assert_raises(intercom.HttpError):
3133
[x for x in self.client.companies.all()]
3234
mock_method.assert_called_once_with('/companies', {})
3335

3436
@istest
35-
def it_raises_error_on_load(self):
37+
def it_raises_error_on_load(self): # noqa
3638
company = Company()
3739
company.id = '4'
3840
side_effect = [None]
39-
with patch.object(Client, 'get', side_effect=side_effect) as mock_method: # noqa
41+
with patch.object(Client, 'get', side_effect=side_effect) as mock_method:
4042
with assert_raises(intercom.HttpError):
4143
self.client.companies.load(company)
4244
eq_([call('/companies/4', {})], mock_method.mock_calls)
45+
46+
@istest
47+
def it_gets_companies_by_tag(self): # noqa
48+
with patch.object(Client, 'get', return_value=page_of_companies(False)) as mock_method:
49+
companies = self.client.companies.by_tag(124)
50+
for company in companies:
51+
ok_(hasattr(company, 'company_id'))
52+
eq_([call('/companies?tag_id=124', {})], mock_method.mock_calls)

tests/unit/test_user.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from nose.tools import istest
2121
from tests.unit import get_user
2222
from tests.unit import mock_response
23+
from tests.unit import page_of_users
2324

2425

2526
class UserTest(unittest.TestCase):
@@ -179,6 +180,13 @@ def it_fetches_a_user(self):
179180
mock_method.assert_called_once_with(
180181
'/users', {'email': 'somebody@example.com'}) # noqa
181182

183+
@istest
184+
def it_gets_users_by_tag(self):
185+
with patch.object(Client, 'get', return_value=page_of_users(False)) as mock_method:
186+
users = self.client.users.by_tag(124)
187+
for user in users:
188+
ok_(hasattr(user, 'avatar'))
189+
182190
@istest
183191
def it_saves_a_user_always_sends_custom_attributes(self):
184192

0 commit comments

Comments
 (0)