Skip to content

Commit df30465

Browse files
committed
Support for Tagging.
1 parent 93ec880 commit df30465

File tree

6 files changed

+346
-1
lines changed

6 files changed

+346
-1
lines changed

intercom/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ def wrapper(instance, value):
4444
from .message_thread import MessageThread
4545
from .note import Note
4646
from .user import User
47+
from .tag import Tag
4748

4849
__all__ = (
4950
AuthenticationError, Intercom, ResourceNotFound, ServerError,
50-
Impression, MessageThread, Note, User
51+
Impression, MessageThread, Note, User, Tag
5152
)

intercom/intercom.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,78 @@ def reply_message_thread(
325325
'PUT', Intercom.api_endpoint + 'users/message_threads',
326326
params=params)
327327
return user_dict
328+
329+
@classmethod
330+
def create_tag(
331+
cls, name, tag_or_untag, user_ids=None, emails=None,
332+
color=None):
333+
""" Create a tag (and maybe tag users).
334+
335+
>>> tag = Intercom.create_tag("Free Trial", "tag",
336+
... user_ids=["abc123", "def456"])
337+
>>> tag.get('id', None)
338+
>>> tag['name']
339+
u'Free Trial'
340+
>>> tag.get('tagged_user_count', None)
341+
>>> tag['color']
342+
u'green'
343+
344+
"""
345+
346+
params = {
347+
'name': name,
348+
'tag_or_untag': tag_or_untag,
349+
'user_ids': user_ids,
350+
'emails': emails,
351+
'color': color
352+
}
353+
tag_dict = Intercom._call(
354+
'POST', Intercom.api_endpoint + 'tags', params=params)
355+
return tag_dict
356+
357+
@classmethod
358+
def update_tag(
359+
cls, name, tag_or_untag, user_ids=None, emails=None,
360+
color=None):
361+
""" Update a tag (and maybe tag users).
362+
363+
>>> tag = Intercom.update_tag("Free Trial", "tag",
364+
... user_ids=["abc123", "def456"])
365+
>>> tag.get('id', None)
366+
>>> tag['name']
367+
u'Free Trial'
368+
>>> tag.get('tagged_user_count', None)
369+
>>> tag['color']
370+
u'green'
371+
372+
"""
373+
374+
params = {
375+
'name': name,
376+
'tag_or_untag': tag_or_untag,
377+
'user_ids': user_ids,
378+
'emails': emails,
379+
'color': color
380+
}
381+
tag_dict = Intercom._call(
382+
'PUT', Intercom.api_endpoint + 'tags', params=params)
383+
return tag_dict
384+
385+
@classmethod
386+
def get_tag(cls, name=None):
387+
""" Return a dict for the tag by the specified name.
388+
389+
>>> tag = Intercom.get_tag(name="Free Trial")
390+
>>> tag.get('id', None)
391+
>>> tag['name']
392+
u'Free Trial'
393+
>>> tag.get('tagged_user_count', None)
394+
>>> tag['color']
395+
u'green'
396+
397+
"""
398+
399+
params = {'name': name}
400+
tag_dict = Intercom._call(
401+
'GET', Intercom.api_endpoint + 'tags', params=params)
402+
return tag_dict

intercom/tag.py

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# coding=utf-8
2+
#
3+
# Copyright 2013 keyes.ie
4+
#
5+
# License: http://jkeyes.mit-license.org/
6+
#
7+
""" Tag module.
8+
9+
>>> from intercom import Intercom
10+
>>> Intercom.app_id = 'dummy-app-id'
11+
>>> Intercom.api_key = 'dummy-api-key'
12+
>>> from intercom.tag import Tag
13+
14+
"""
15+
16+
from . import Intercom
17+
18+
19+
class Tag(dict):
20+
""" Represents a tag. """
21+
22+
@classmethod
23+
def find(cls, **params):
24+
""" Find a tag using params.
25+
26+
>>> tag = Tag.find(name="Free Trial")
27+
>>> tag.id
28+
>>> tag.name
29+
u'Free Trial'
30+
>>> tag.tagged_user_count
31+
>>> tag.color
32+
u'green'
33+
34+
"""
35+
resp = Intercom.get_tag(**params)
36+
return cls(resp)
37+
38+
@classmethod
39+
def find_by_name(cls, name):
40+
""" Find a tag by name.
41+
42+
>>> tag = Tag.find_by_name("Free Trial")
43+
>>> tag.id
44+
>>> tag.name
45+
u'Free Trial'
46+
>>> tag.tagged_user_count
47+
>>> tag.color
48+
u'green'
49+
50+
"""
51+
return Tag.find(name=name)
52+
53+
@classmethod
54+
def create(
55+
cls, name, tag_or_untag, user_ids=None, emails=None,
56+
color=None):
57+
""" Create a new tag an optionally tag user.
58+
59+
>>> tag = Tag.create(user_ids=["abc123", "def456"],
60+
... name="Free Trial", tag_or_untag="tag")
61+
>>> tag.id
62+
>>> tag.name
63+
u'Free Trial'
64+
>>> tag.tagged_user_count
65+
>>> tag.color
66+
u'green'
67+
68+
"""
69+
resp = Intercom.create_tag(
70+
name, tag_or_untag, user_ids=user_ids, emails=emails,
71+
color=color)
72+
return cls(resp)
73+
74+
def save(self):
75+
""" Update a tag:
76+
77+
>>> tag = Tag()
78+
>>> tag.user_ids = ["abc123", "def456"]
79+
>>> tag.name = "Free Trial"
80+
>>> tag.tag_or_untag = "tag"
81+
>>> tag.save()
82+
>>> tag.tagged_user_count
83+
>>> tag.color
84+
u'green'
85+
86+
"""
87+
resp = Intercom.update_tag(
88+
self.name, self.get('tag_or_untag', None),
89+
user_ids=self.get('user_ids', None),
90+
emails=self.get('emails', None),
91+
color=self.color)
92+
self.update(resp)
93+
94+
@property
95+
def name(self):
96+
""" Get the name of the tag. """
97+
return dict.get(self, 'name', None)
98+
99+
@name.setter
100+
def name(self, name):
101+
""" Get the name of the tag. """
102+
self['name'] = name
103+
104+
@property
105+
def color(self):
106+
""" Get the color of the tag. """
107+
return dict.get(self, 'color', None)
108+
109+
@color.setter
110+
def color(self, color):
111+
""" Get the color of the tag. """
112+
self['color'] = color
113+
114+
@property
115+
def id(self):
116+
""" The id of the tag. """
117+
return dict.get(self, 'id', None)
118+
119+
@property
120+
def segment(self):
121+
""" Get the segment of the tag. """
122+
return dict.get(self, 'segment', None)
123+
124+
@property
125+
def tagged_user_count(self):
126+
""" Get the tagged_user_count of the tag. """
127+
return dict.get(self, 'tagged_user_count', None)
128+
129+
@property
130+
def tag_or_untag(self):
131+
""" The tag_or_untag of the tag. """
132+
raise AttributeError("tag_or_untag is a write-only property")
133+
134+
@tag_or_untag.setter
135+
def tag_or_untag(self, tag_or_untag):
136+
""" The tag_or_untag of the tag. """
137+
self['tag_or_untag'] = tag_or_untag
138+
139+
@property
140+
def user_ids(self):
141+
""" The user_ids of the tag. """
142+
raise AttributeError("user_ids is a write-only property")
143+
144+
@user_ids.setter
145+
def user_ids(self, user_ids):
146+
""" The user_ids of the tag. """
147+
self['user_ids'] = user_ids
148+
149+
@property
150+
def emails(self):
151+
""" The emails of the tag. """
152+
raise AttributeError("emails is a write-only property")
153+
154+
@emails.setter
155+
def emails(self, emails):
156+
""" The emails of the tag. """
157+
self['emails'] = emails
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"id": null,
3+
"name": "Poweruser",
4+
"color": "green",
5+
"segment": false,
6+
"tagged_user_count": null
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"id": null,
3+
"name": "Free Trial",
4+
"color": "green",
5+
"segment": false,
6+
"tagged_user_count": null
7+
}

tests/unit/test_tag.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# coding=utf-8
2+
#
3+
# Copyright 2012 keyes.ie
4+
#
5+
# License: http://jkeyes.mit-license.org/
6+
#
7+
8+
from intercom.tag import Tag
9+
from nose.tools import eq_
10+
from nose.tools import raises
11+
from mock import patch
12+
from unittest import TestCase
13+
14+
from . import create_response
15+
16+
17+
class TagTest(TestCase):
18+
19+
@raises(AttributeError)
20+
def test_writeonly_emails(self):
21+
tag = Tag()
22+
tag.emails
23+
24+
def test_write_emails(self):
25+
tag = Tag()
26+
tag.emails = ["joe@example.com"]
27+
eq_(["joe@example.com"], tag['emails'])
28+
29+
@raises(AttributeError)
30+
def test_writeonly_user_ids(self):
31+
tag = Tag()
32+
tag.user_ids
33+
34+
def test_write_user_ids(self):
35+
tag = Tag()
36+
tag.user_ids = ["abc123"]
37+
eq_(["abc123"], tag['user_ids'])
38+
39+
@raises(AttributeError)
40+
def test_writeonly_tag_or_untag(self):
41+
tag = Tag()
42+
tag.tag_or_untag
43+
44+
def test_write_tag_or_untag(self):
45+
tag = Tag()
46+
tag.tag_or_untag = "tag"
47+
eq_('tag', tag['tag_or_untag'])
48+
49+
@raises(AttributeError)
50+
def test_readonly_segment(self):
51+
tag = Tag()
52+
tag.segment = "xyz"
53+
54+
@raises(AttributeError)
55+
def test_readonly_user_count(self):
56+
tag = Tag()
57+
tag.tagged_user_count = 0
58+
59+
@raises(AttributeError)
60+
def test_readonly_id(self):
61+
tag = Tag()
62+
tag.id = 0
63+
64+
def test_accessor(self):
65+
tag = Tag()
66+
67+
tag.name = "xyz"
68+
eq_("xyz", tag.name)
69+
70+
tag.color = "abc"
71+
eq_("abc", tag.color)
72+
73+
@patch('requests.request', create_response(200, 'create_tag_valid.json'))
74+
def test_create(self):
75+
tag = Tag.create(name="Poweruser", tag_or_untag="tag")
76+
eq_(None, tag.id)
77+
eq_('green', tag.color)
78+
eq_(False, tag.segment)
79+
eq_(None, tag.tagged_user_count)
80+
81+
@patch('requests.request', create_response(200, 'create_tag_valid.json'))
82+
def test_update(self):
83+
tag = Tag()
84+
tag.save()
85+
eq_(None, tag.id)
86+
eq_('green', tag.color)
87+
88+
@patch('requests.request', create_response(200, 'get_tag_valid.json'))
89+
def test_find(self):
90+
tag = Tag.find(name="Poweruser")
91+
eq_(None, tag.id)
92+
eq_('green', tag.color)
93+
94+
@patch('requests.request', create_response(200, 'get_tag_valid.json'))
95+
def test_find_by_name(self):
96+
tag = Tag.find_by_name("Poweruser")
97+
eq_(None, tag.id)
98+
eq_('green', tag.color)

0 commit comments

Comments
 (0)