Skip to content

Commit 381a7f9

Browse files
committed
Moving save to it's own module.
1 parent b77e1ad commit 381a7f9

File tree

4 files changed

+71
-97
lines changed

4 files changed

+71
-97
lines changed

intercom/api_operations/__init__.py

Whitespace-only changes.

intercom/api_operations/save.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from intercom import Intercom
2+
from intercom import utils
3+
4+
class Save(object):
5+
6+
@classmethod
7+
def create(cls, **params):
8+
collection = utils.resource_class_to_collection_name(cls)
9+
response = Intercom.post("/%s/" % (collection), **params)
10+
return cls(**response)
11+
12+
def from_dict(self, pdict):
13+
for key, value in pdict.items():
14+
setattr(self, key, value)
15+
16+
@property
17+
def to_dict(self):
18+
a_dict = {}
19+
for name in self.attributes.keys():
20+
a_dict[name] = self.__dict__[name] # direct access
21+
return a_dict
22+
23+
24+
@classmethod
25+
def from_api(cls, response):
26+
obj = cls()
27+
obj.from_response(response)
28+
return obj
29+
30+
def from_response(self, response):
31+
self.from_dict(response)
32+
return self
33+
34+
def save(self):
35+
collection = utils.resource_class_to_collection_name(self.__class__)
36+
params = self.__dict__
37+
if self.id_present:
38+
# update
39+
response = Intercom.put('/%s/%s' % (collection, self.id), **params)
40+
else:
41+
# create
42+
response = Intercom.post('/%s' % (collection), **params)
43+
if response:
44+
return self.from_response(response)
45+
46+
47+
@property
48+
def id_present(self):
49+
return getattr(self, 'id', None) and self.id != ""
50+
51+
@property
52+
def posted_updates(self):
53+
return getattr(self, 'update_verb', None) != 'post'
54+
55+
@property
56+
def identity_hash(self):
57+
identity_vars = getattr(self, 'identity_vars', None)
58+
if identity_vars:
59+
return {}

intercom/user.py

Lines changed: 6 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import datetime
1111
import time
1212
import types
13+
from intercom.api_operations.save import Save
1314

1415

1516
class ArgumentError(ValueError):
@@ -114,14 +115,6 @@ def to_datetime_value(value):
114115
return datetime.datetime.fromtimestamp(int(value))
115116

116117

117-
def resource_class_to_collection_name(cls):
118-
return cls.__name__.lower() + "s"
119-
120-
121-
def resource_class_to_name(cls):
122-
return cls.__name__.lower()
123-
124-
125118
class Resource(object):
126119

127120
# class __metaclass__(type):
@@ -235,7 +228,7 @@ class Find(object):
235228

236229
@classmethod
237230
def find(cls, **params):
238-
collection = resource_class_to_collection_name(cls)
231+
collection = utils.resource_class_to_collection_name(cls)
239232
print "find %s in %s" % (params, collection)
240233
if 'id' in params:
241234
response = Intercom.get("/%s/%s" % (collection, params['id']))
@@ -325,7 +318,7 @@ class FindAll(object):
325318

326319
@classmethod
327320
def find_all(cls, **params):
328-
collection = resource_class_to_collection_name(cls)
321+
collection = utils.resource_class_to_collection_name(cls)
329322
print "find_all %s in %s" % (params, collection)
330323
if 'id' in params and 'type' not in params:
331324
finder_url = "/%s/%s" % (collection, params['id'])
@@ -346,7 +339,7 @@ class All(object):
346339

347340
@classmethod
348341
def all(cls):
349-
collection = resource_class_to_collection_name(cls)
342+
collection = utils.resource_class_to_collection_name(cls)
350343
print "list %s" % (collection)
351344
finder_url = "/%s" % (collection)
352345
return CollectionProxy(cls, collection, finder_url)
@@ -357,72 +350,13 @@ class Count(object):
357350
@classmethod
358351
def count(cls):
359352
response = Intercom.get("/counts/")
360-
return response[resource_class_to_name(cls)]['count']
361-
362-
363-
class Save(object):
364-
365-
@classmethod
366-
def create(cls, **params):
367-
collection = resource_class_to_collection_name(cls)
368-
response = Intercom.post("/%s/" % (collection), **params)
369-
return cls(**response)
370-
371-
def from_dict(self, pdict):
372-
for key, value in pdict.items():
373-
setattr(self, key, value)
374-
375-
@property
376-
def to_dict(self):
377-
a_dict = {}
378-
for name in self.attributes.keys():
379-
a_dict[name] = self.__dict__[name] # direct access
380-
return a_dict
381-
382-
383-
@classmethod
384-
def from_api(cls, response):
385-
obj = cls()
386-
obj.from_response(response)
387-
return obj
388-
389-
def from_response(self, response):
390-
self.from_dict(response)
391-
return self
392-
393-
def save(self):
394-
collection = resource_class_to_collection_name(self.__class__)
395-
params = self.__dict__
396-
if self.id_present:
397-
# update
398-
response = Intercom.put('/%s/%s' % (collection, self.id), **params)
399-
else:
400-
# create
401-
response = Intercom.post('/%s' % (collection), **params)
402-
if response:
403-
return self.from_response(response)
404-
405-
406-
@property
407-
def id_present(self):
408-
return getattr(self, 'id', None) and self.id != ""
409-
410-
@property
411-
def posted_updates(self):
412-
return getattr(self, 'update_verb', None) != 'post'
413-
414-
@property
415-
def identity_hash(self):
416-
identity_vars = getattr(self, 'identity_vars', None)
417-
if identity_vars:
418-
return {}
419-
353+
return response[utils.resource_class_to_name(cls)]['count']
420354

421355

422356
class Delete(object):
423357

424358
def delete(self):
425-
collection = resource_class_to_collection_name(self.__class__)
359+
collection = utils.resource_class_to_collection_name(self.__class__)
426360
Intercom.delete("/%s/%s/" % (collection, self.id))
427361
return self
428362

intercom/utils.py

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,9 @@ def constantize_singular_resource_name(resource_name):
2121
return create_class_instance(class_name)
2222

2323

24-
if __name__ == "__main__":
25-
print pluralize('company')
26-
print pluralize('user')
27-
28-
print entity_key_from_type('company.list')
29-
30-
print constantize_singular_resource_name('location_data')
31-
print constantize_singular_resource_name('companies')
32-
33-
from intercom.user import User
34-
user = User()
35-
user.from_dict({
36-
'username': 'john',
37-
'companies': {
38-
"type": "company.list",
39-
"companies": [
40-
{
41-
"type": "company",
42-
"id": "xyz"
43-
}
44-
]
45-
}
46-
})
47-
print user.username
48-
print user.companies[0].__class__.__name__
24+
def resource_class_to_collection_name(cls):
25+
return cls.__name__.lower() + "s"
26+
27+
28+
def resource_class_to_name(cls):
29+
return cls.__name__.lower()

0 commit comments

Comments
 (0)