Skip to content

Commit 9b5de69

Browse files
committed
Make models more consistent with existing models
1 parent 00e7852 commit 9b5de69

File tree

2 files changed

+24
-31
lines changed

2 files changed

+24
-31
lines changed

geoip2/database.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ def connection_type(self, ip_address):
9999
:returns: :py:class:`geoip2.models.ConnectionType` object
100100
101101
"""
102-
record = self._get(ip_address)
103-
return geoip2.models.ConnectionType(ip_address=ip_address,
104-
connection_type=record.get(
105-
'connection_type'))
102+
return self._flat_model_for(geoip2.models.ConnectionType, ip_address)
106103

107104
def domain(self, ip_address):
108105
"""Get the Domain object for the IP address
@@ -112,9 +109,7 @@ def domain(self, ip_address):
112109
:returns: :py:class:`geoip2.models.Domain` object
113110
114111
"""
115-
record = self._get(ip_address)
116-
return geoip2.models.Domain(ip_address=ip_address,
117-
domain=record.get('domain'))
112+
return self._flat_model_for(geoip2.models.Domain, ip_address)
118113

119114
def isp_org(self, ip_address):
120115
"""Get the ISPOrg object for the IP address
@@ -124,14 +119,7 @@ def isp_org(self, ip_address):
124119
:returns: :py:class:`geoip2.models.ISPOrg` object
125120
126121
"""
127-
record = self._get(ip_address)
128-
return geoip2.models.ISPOrg(ip_address=ip_address,
129-
autonomous_system_number=record.get(
130-
'autonomous_system_number'),
131-
autonomous_system_organization=record.get(
132-
'autonomous_system_organization'),
133-
isp=record.get('isp'),
134-
organization=record.get('organization'))
122+
return self._flat_model_for(geoip2.models.ISPOrg, ip_address)
135123

136124
def _get(self, ip_address):
137125
record = self._db_reader.get(ip_address)
@@ -145,6 +133,11 @@ def _model_for(self, model_class, ip_address):
145133
record.setdefault('traits', {})['ip_address'] = ip_address
146134
return model_class(record, locales=self._locales)
147135

136+
def _flat_model_for(self, model_class, ip_address):
137+
record = self._get(ip_address)
138+
record['ip_address'] = ip_address
139+
return model_class(record)
140+
148141
def close(self):
149142
"""Closes the GeoIP2 database"""
150143

geoip2/models.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,10 @@ class ConnectionType(object):
326326
:type: unicode
327327
"""
328328

329-
def __init__(self, ip_address=None, connection_type=None):
330-
self.connection_type = connection_type
331-
self.ip_address = ip_address
329+
def __init__(self, raw):
330+
self.connection_type = raw.get('connection_type')
331+
self.ip_address = raw.get('ip_address')
332+
self.raw = raw
332333

333334

334335
class Domain(object):
@@ -351,9 +352,10 @@ class Domain(object):
351352
352353
"""
353354

354-
def __init__(self, ip_address=None, domain=None):
355-
self.domain = domain
356-
self.ip_address = ip_address
355+
def __init__(self, raw):
356+
self.domain = raw.get('domain')
357+
self.ip_address = raw.get('ip_address')
358+
self.raw = raw
357359

358360

359361
class ISPOrg(object):
@@ -395,13 +397,11 @@ class ISPOrg(object):
395397
"""
396398

397399
# pylint:disable=too-many-arguments
398-
def __init__(self, ip_address=None,
399-
autonomous_system_number=None,
400-
autonomous_system_organization=None,
401-
isp=None,
402-
organization=None):
403-
self.autonomous_system_number = autonomous_system_number
404-
self.autonomous_system_organization = autonomous_system_organization
405-
self.isp = isp
406-
self.organization = organization
407-
self.ip_address = ip_address
400+
def __init__(self, raw):
401+
self.autonomous_system_number = raw.get('autonomous_system_number')
402+
self.autonomous_system_organization = raw.get(
403+
'autonomous_system_organization')
404+
self.isp = raw.get('isp')
405+
self.organization = raw.get('organization')
406+
self.ip_address = raw.get('ip_address')
407+
self.raw = raw

0 commit comments

Comments
 (0)