Class: Workarea::Geolocation
- Inherits:
-
Object
- Object
- Workarea::Geolocation
- Defined in:
- lib/workarea/geolocation.rb
Defined Under Namespace
Classes: Address
Instance Method Summary collapse
- #city ⇒ Object
-
#coordinates ⇒ Array<Number>
can be found.
- #country ⇒ Object
-
#initialize(env = {}, ip = nil) ⇒ Geolocation
constructor
A new instance of Geolocation.
- #latitude ⇒ Object
- #longitude ⇒ Object
- #names ⇒ Object
- #postal_code ⇒ Object
- #region ⇒ Object
- #subdivision ⇒ Object
Constructor Details
#initialize(env = {}, ip = nil) ⇒ Geolocation
Returns a new instance of Geolocation.
5 6 7 8 |
# File 'lib/workarea/geolocation.rb', line 5 def initialize(env = {}, ip = nil) @env = env @ip = ip end |
Instance Method Details
#city ⇒ Object
29 30 31 32 33 |
# File 'lib/workarea/geolocation.rb', line 29 def city @city ||= @env['HTTP_GEOIP2_DATA_CITY_NAME'].presence || @env['HTTP_GEOIP_CITY'].presence || request.try(:city) end |
#coordinates ⇒ Array<Number>
can be found
54 55 56 57 |
# File 'lib/workarea/geolocation.rb', line 54 def coordinates return unless latitude.present? && longitude.present? [latitude, longitude] end |
#country ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/workarea/geolocation.rb', line 10 def country @country ||= Country[ @env['HTTP_GEOIP2_DATA_COUNTRY_CODE'].presence || @env['HTTP_GEOIP_CITY_COUNTRY_CODE'].presence || request.try(:country_code) ] end |
#latitude ⇒ Object
40 41 42 43 44 |
# File 'lib/workarea/geolocation.rb', line 40 def latitude @latitude ||= @env['HTTP_GEOIP2_DATA_LATITUDE'].presence || @env['HTTP_GEOIP_LATITUDE'].presence || request.try(:latitude) end |
#longitude ⇒ Object
46 47 48 49 50 |
# File 'lib/workarea/geolocation.rb', line 46 def longitude @longitude ||= @env['HTTP_GEOIP2_DATA_LONGITUDE'].presence || @env['HTTP_GEOIP_LONGITUDE'].presence || request.try(:longitude) end |
#names ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/workarea/geolocation.rb', line 59 def names @names ||= [ postal_code, city, region, subdivision&.name, country&.alpha2, country&.alpha3, country&.name ].reject(&:blank?) end |
#postal_code ⇒ Object
35 36 37 38 |
# File 'lib/workarea/geolocation.rb', line 35 def postal_code @postal_code ||= @env['HTTP_GEOIP_POSTAL_CODE'].presence || request.try(:postal_code) end |
#region ⇒ Object
18 19 20 21 22 |
# File 'lib/workarea/geolocation.rb', line 18 def region @region ||= region_from_geoip2.presence || @env['HTTP_GEOIP_REGION'].presence || request_data['region_code'] end |
#subdivision ⇒ Object
24 25 26 27 |
# File 'lib/workarea/geolocation.rb', line 24 def subdivision return @subdivision if defined?(@subdivision) @subdivision = country&.subdivisions&.fetch(region) rescue nil end |