Skip to content

Commit c31fc1f

Browse files
committed
Adding tag_COLLECTION and untag_COLLECTION methods to Tag.
I investigated using generics like intercom-ruby uses but to no avail as I couldn't figure out how to pass the original method name as an additional arg to the generic method.
1 parent 573ea34 commit c31fc1f

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

intercom/tag.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,46 @@
1+
from intercom import Intercom
2+
from intercom import utils
13
from intercom.user import Resource
4+
from intercom.api_operations.all import All
25
from intercom.api_operations.count import Count
36
from intercom.api_operations.find import Find
7+
from intercom.api_operations.find_all import FindAll
48
from intercom.api_operations.save import Save
59

610

7-
class Tag(Resource, Find, Count, Save):
8-
pass
11+
class Tag(Resource, All, Count, Find, FindAll, Save):
12+
13+
@classmethod
14+
def _tag_collection(cls, name, collection_name, objects, untag=False):
15+
collection = utils.resource_class_to_collection_name(cls)
16+
17+
object_ids = []
18+
for obj in objects:
19+
if not hasattr(obj, 'keys'):
20+
obj = {'id': obj}
21+
if untag:
22+
obj['untag'] = True
23+
object_ids.append(obj)
24+
25+
params = {
26+
'name': name,
27+
collection_name: object_ids,
28+
}
29+
response = Intercom.post("/%s" % (collection), **params)
30+
return cls(**response)
31+
32+
@classmethod
33+
def tag_users(cls, name, users):
34+
return cls._tag_collection(name, 'users', users)
35+
36+
@classmethod
37+
def untag_users(cls, name, users):
38+
return cls._tag_collection(name, 'users', users, untag=True)
39+
40+
@classmethod
41+
def tag_companies(cls, name, companies):
42+
return cls._tag_collection(name, 'companies', companies)
43+
44+
@classmethod
45+
def untag_companies(cls, name, companies):
46+
return cls._tag_collection(name, 'companies', companies, untag=True)

0 commit comments

Comments
 (0)