Class: IPLookup
- Inherits:
-
Object
- Object
- IPLookup
- Defined in:
- lib/ip-lookup.rb,
lib/ip_lookup/db.rb,
lib/ip_lookup/errors.rb,
lib/ip_lookup/version.rb,
lib/ip_lookup/defaults.rb
Defined Under Namespace
Classes: DB, DBFileNotFoundError
Constant Summary collapse
- VERSION =
"1.0.4"- DEFAULT_COUNTRY =
"us"- DEFAULT_TIMEZONE =
"America/Swift_Current"- DEFAULT_COORDINATES =
NYC
[40.0, 74.0].freeze
- DEFAULT_SUBDIVISION =
nil
Instance Attribute Summary collapse
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#silent_exceptions ⇒ Object
readonly
Returns the value of attribute silent_exceptions.
Instance Method Summary collapse
- #coordinates ⇒ Object
- #country ⇒ Object
-
#initialize(ip_address, silent_exceptions: false) ⇒ IPLookup
constructor
A new instance of IPLookup.
- #subdivision ⇒ Object
- #timezone ⇒ Object
Constructor Details
#initialize(ip_address, silent_exceptions: false) ⇒ IPLookup
Returns a new instance of IPLookup.
14 15 16 17 |
# File 'lib/ip-lookup.rb', line 14 def initialize(ip_address, silent_exceptions: false) @silent_exceptions = silent_exceptions @result = DB.new(silent_exceptions).lookup(ip_address) end |
Instance Attribute Details
#result ⇒ Object (readonly)
Returns the value of attribute result.
11 12 13 |
# File 'lib/ip-lookup.rb', line 11 def result @result end |
#silent_exceptions ⇒ Object (readonly)
Returns the value of attribute silent_exceptions.
12 13 14 |
# File 'lib/ip-lookup.rb', line 12 def silent_exceptions @silent_exceptions end |
Instance Method Details
#coordinates ⇒ Object
36 37 38 39 40 |
# File 'lib/ip-lookup.rb', line 36 def coordinates error_handler(DEFAULT_COORDINATES) do [result.location.latitude, result.location.longitude] end end |
#country ⇒ Object
19 20 21 22 23 24 |
# File 'lib/ip-lookup.rb', line 19 def country error_handler(DEFAULT_COUNTRY) do iso_code = result.country.iso_code iso_code ? iso_code.downcase : DEFAULT_COUNTRY end end |
#subdivision ⇒ Object
42 43 44 45 46 |
# File 'lib/ip-lookup.rb', line 42 def subdivision error_handler(DEFAULT_SUBDIVISION) do result.subdivisions.any? ? result.subdivisions.first.iso_code.downcase : DEFAULT_SUBDIVISION end end |
#timezone ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/ip-lookup.rb', line 26 def timezone error_handler(DEFAULT_TIMEZONE) do timezone = result.location.time_zone timezone ||= timezone_for result.country.iso_code if result.country.iso_code timezone || DEFAULT_TIMEZONE end end |