Skip to content

Commit 6b6b2e5

Browse files
committed
Changed LocationData from a dict to a LocationData object for consistency.
Added missing __init__.py files (error in my .gitignore).
1 parent 154c33d commit 6b6b2e5

File tree

6 files changed

+111
-7
lines changed

6 files changed

+111
-7
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ venv
55
*.egg-info
66
*.pyc
77
.DS_Store
8-
_*
8+
docs/_build
99

1010
intercom.sublime-project
1111
intercom.sublime-workspace

docs/index.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
.. intercom documentation master file, created by
2-
sphinx-quickstart on Tue Mar 20 09:38:03 2012.
3-
You can adapt this file completely to your liking, but it should at least
4-
contain the root `toctree` directive.
5-
61
========
72
intercom
83
========

intercom/user.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ def social_profiles(self):
127127

128128
@property
129129
def location_data(self):
130-
return dict.get(self, 'location_data', None)
130+
data = dict.get(self, 'location_data', None)
131+
if not isinstance(data, LocationData):
132+
data = LocationData(data)
133+
dict.__setitem__(self, 'location_data', data)
134+
return data
131135

132136
@property
133137
def custom_data(self):
@@ -181,3 +185,46 @@ def username(self):
181185
def __setitem__(self, key, value):
182186
""" Do not allow items to be set. """
183187
raise NotImplementedError
188+
189+
class LocationData(dict):
190+
""" Object representing a user's location data """
191+
192+
@property
193+
def city_name(self):
194+
return self.get('city_name', None)
195+
196+
@property
197+
def continent_code(self):
198+
return self.get('continent_code', None)
199+
200+
@property
201+
def country_name(self):
202+
return self.get('country_name', None)
203+
204+
@property
205+
def latitude(self):
206+
return self.get('latitude', None)
207+
208+
@property
209+
def longitude(self):
210+
return self.get('longitude', None)
211+
212+
@property
213+
def postal_code(self):
214+
return self.get('postal_code', None)
215+
216+
@property
217+
def region_name(self):
218+
return self.get('region_name', None)
219+
220+
@property
221+
def timezone(self):
222+
return self.get('timezone', None)
223+
224+
@property
225+
def country_code(self):
226+
return self.get('country_code', None)
227+
228+
def __setitem__(self, key, value):
229+
""" Do not allow items to be set. """
230+
raise NotImplementedError

tests/integration/__init__.py

Whitespace-only changes.

tests/unit/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#
2+
# Copyright 2012 keyes.ie
3+
#
4+
# License: http://jkeyes.mit-license.org/
5+
#
6+
7+
import os
8+
9+
from os import environ as ENV
10+
from mock import Mock
11+
12+
DIRPATH = os.path.dirname(__file__)
13+
FIXTURES = os.path.join(DIRPATH, 'fixtures')
14+
15+
def create_response(status, fixture=None):
16+
def request(*args, **kwargs):
17+
response = Mock()
18+
response.status_code = status
19+
if fixture:
20+
fixture_path = os.path.join(FIXTURES, fixture)
21+
response.content = open(fixture_path).read()
22+
return response
23+
return request

tests/unit/test_user.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from intercom.intercom import AuthError
1818
from intercom.user import CustomData
19+
from intercom.user import LocationData
1920
from intercom.user import SocialProfile
2021
from intercom.user import User
2122

@@ -132,11 +133,14 @@ def test_find_by_email(self):
132133
time.mktime(user.created_at.timetuple()))
133134
self.assertTrue(1, len(user.social_profiles))
134135
profile = user.social_profiles[0]
136+
self.assertTrue(isinstance(profile, SocialProfile))
135137
self.assertEqual('twitter', profile.type)
136138
self.assertEqual('foo', profile.username)
137139
self.assertEqual('http://twitter.com/foo', profile.url)
138140
self.assertEqual('1234567', profile.id)
139141
self.assertEqual('Santiago', user.location_data['city_name'])
142+
self.assertEqual('Santiago', user.location_data.city_name)
143+
self.assertTrue(isinstance(user.location_data, LocationData))
140144
self.assertEqual('johnny', user.custom_data['nick'])
141145

142146
@patch('requests.request', create_response(200, 'get_user_id_valid.json'))
@@ -186,3 +190,38 @@ def test_properties(self):
186190
self.assertEqual(1331764344,
187191
time.mktime(user.created_at.timetuple()))
188192
self.assertEqual('Ace', user.custom_data['name'])
193+
194+
class LocationDataTest(TestCase):
195+
196+
@raises(AttributeError)
197+
def test_setattr(self):
198+
location_data = LocationData()
199+
location_data.city_name = "Dublin"
200+
201+
@raises(NotImplementedError)
202+
def test_setitem(self):
203+
location_data = LocationData()
204+
location_data['city_name'] = "Dublin"
205+
206+
def test_properties(self):
207+
location_data = LocationData({
208+
"city_name": "Santiago",
209+
"continent_code": "SA",
210+
"country_name": "Chile",
211+
"latitude": -33.44999999999999,
212+
"longitude": -70.6667,
213+
"postal_code": "",
214+
"region_name": "12",
215+
"timezone": "Chile/Continental",
216+
"country_code": "CHL"
217+
})
218+
219+
self.assertEqual('Santiago', location_data.city_name)
220+
self.assertEqual('SA', location_data.continent_code)
221+
self.assertEqual('Chile', location_data.country_name)
222+
self.assertEqual(-33.44999999999999, location_data.latitude)
223+
self.assertEqual(-70.6667, location_data.longitude)
224+
self.assertEqual('', location_data.postal_code)
225+
self.assertEqual('12', location_data.region_name)
226+
self.assertEqual('Chile/Continental', location_data.timezone)
227+
self.assertEqual('CHL', location_data.country_code)

0 commit comments

Comments
 (0)