1010import datetime
1111import time
1212import types
13+ from intercom .api_operations .save import Save
1314
1415
1516class 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-
125118class 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
422356class 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
0 commit comments