Skip to content

Add by_tag support #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ venv
*.egg-info
*.pyc
.DS_Store
htmlcov
docs/_build

intercom.sublime-project
Expand Down
17 changes: 17 additions & 0 deletions intercom/extended_api_operations/tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
"""Operation to return resources with a particular tag."""

from intercom import utils
from intercom.collection_proxy import CollectionProxy


class Tags(object):
"""A mixin that provides `by_tag` functionality a resource."""

def by_tag(self, _id):
"""Return a CollectionProxy to all the tagged resources."""
collection = utils.resource_class_to_collection_name(
self.collection_class)
finder_url = "/%s?tag_id=%s" % (collection, _id)
return CollectionProxy(
self.client, self.collection_class, collection, finder_url)
4 changes: 2 additions & 2 deletions intercom/service/company.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
from intercom.api_operations.save import Save
from intercom.api_operations.load import Load
from intercom.extended_api_operations.users import Users
from intercom.extended_api_operations.tags import Tags
from intercom.service.base_service import BaseService


class Company(BaseService, All, Delete, Find, FindAll, Save, Load, Users):
class Company(BaseService, All, Delete, Find, FindAll, Save, Load, Users, Tags):

@property
def collection_class(self):
return company.Company

# require 'intercom/extended_api_operations/tags'
# require 'intercom/extended_api_operations/segments'
3 changes: 2 additions & 1 deletion intercom/service/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
from intercom.api_operations.delete import Delete
from intercom.api_operations.save import Save
from intercom.api_operations.load import Load
from intercom.extended_api_operations.tags import Tags
from intercom.service.base_service import BaseService


class User(BaseService, All, Find, FindAll, Delete, Save, Load, Submit):
class User(BaseService, All, Find, FindAll, Delete, Save, Load, Submit, Tags):

@property
def collection_class(self):
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,30 @@ def get_user(email="bob@example.com", name="Joe Schmoe"):
}


def get_company(name):
return {
"type": "company",
"id": "531ee472cce572a6ec000006",
"name": name,
"plan": {
"type": "plan",
"id": "1",
"name": "Paid"
},
"company_id": "6",
"remote_created_at": 1394531169,
"created_at": 1394533506,
"updated_at": 1396874658,
"monthly_spend": 49,
"session_count": 26,
"user_count": 10,
"custom_attributes": {
"paid_subscriber": True,
"team_mates": 0
}
}


def page_of_users(include_next_link=False):
page = {
"type": "user.list",
Expand All @@ -157,6 +181,29 @@ def page_of_users(include_next_link=False):
page["pages"]["next"] = "https://api.intercom.io/users?per_page=50&page=2"
return page


def page_of_companies(include_next_link=False):
page = {
"type": "company.list",
"pages": {
"type": "pages",
"page": 1,
"next": None,
"per_page": 50,
"total_pages": 7
},
"companies": [
get_company('ACME A'),
get_company('ACME B'),
get_company('ACME C')
],
"total_count": 3
}
if include_next_link:
page["pages"]["next"] = "https://api.intercom.io/companies?per_page=50&page=2"
return page


test_tag = {
"id": "4f73428b5e4dfc000b000112",
"name": "Test Tag",
Expand Down
24 changes: 17 additions & 7 deletions tests/unit/test_company.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*- # noqa

import intercom
import unittest
Expand All @@ -9,34 +9,44 @@
from mock import patch
from nose.tools import assert_raises
from nose.tools import eq_
from nose.tools import ok_
from nose.tools import istest
from tests.unit import page_of_companies


class CompanyTest(unittest.TestCase):
class CompanyTest(unittest.TestCase): # noqa

def setUp(self):
def setUp(self): # noqa
self.client = Client()

@istest
def it_raises_error_if_no_response_on_find(self):
def it_raises_error_if_no_response_on_find(self): # noqa
with patch.object(Client, 'get', return_value=None) as mock_method:
with assert_raises(intercom.HttpError):
self.client.companies.find(company_id='4')
mock_method.assert_called_once_with('/companies', {'company_id': '4'})

@istest
def it_raises_error_if_no_response_on_find_all(self):
def it_raises_error_if_no_response_on_find_all(self): # noqa
with patch.object(Client, 'get', return_value=None) as mock_method:
with assert_raises(intercom.HttpError):
[x for x in self.client.companies.all()]
mock_method.assert_called_once_with('/companies', {})

@istest
def it_raises_error_on_load(self):
def it_raises_error_on_load(self): # noqa
company = Company()
company.id = '4'
side_effect = [None]
with patch.object(Client, 'get', side_effect=side_effect) as mock_method: # noqa
with patch.object(Client, 'get', side_effect=side_effect) as mock_method:
with assert_raises(intercom.HttpError):
self.client.companies.load(company)
eq_([call('/companies/4', {})], mock_method.mock_calls)

@istest
def it_gets_companies_by_tag(self): # noqa
with patch.object(Client, 'get', return_value=page_of_companies(False)) as mock_method:
companies = self.client.companies.by_tag(124)
for company in companies:
ok_(hasattr(company, 'company_id'))
eq_([call('/companies?tag_id=124', {})], mock_method.mock_calls)
8 changes: 8 additions & 0 deletions tests/unit/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from nose.tools import istest
from tests.unit import get_user
from tests.unit import mock_response
from tests.unit import page_of_users


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

@istest
def it_gets_users_by_tag(self):
with patch.object(Client, 'get', return_value=page_of_users(False)) as mock_method:
users = self.client.users.by_tag(124)
for user in users:
ok_(hasattr(user, 'avatar'))

@istest
def it_saves_a_user_always_sends_custom_attributes(self):

Expand Down