Class: Workarea::Geolocation

Inherits:
Object
  • Object
show all
Defined in:
lib/workarea/geolocation.rb

Defined Under Namespace

Classes: Address

Instance Method Summary collapse

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

#cityObject



24
25
26
27
28
# File 'lib/workarea/geolocation.rb', line 24

def city
  @env['HTTP_GEOIP2_DATA_CITY_NAME'].presence ||
    @env['HTTP_GEOIP_CITY'].presence ||
    request.try(:city)
end

#coordinatesArray<Number>

can be found

Returns:

  • (Array<Number>)

    or nil if neither latitude nor longitude



48
49
50
51
# File 'lib/workarea/geolocation.rb', line 48

def coordinates
  return unless latitude.present? && longitude.present?
  [latitude, longitude]
end

#countryObject



10
11
12
13
14
15
16
# File 'lib/workarea/geolocation.rb', line 10

def country
  Country[
    @env['HTTP_GEOIP2_DATA_COUNTRY_CODE'].presence ||
      @env['HTTP_GEOIP_CITY_COUNTRY_CODE'].presence ||
        request.try(:country_code)
  ]
end

#latitudeObject



34
35
36
37
38
# File 'lib/workarea/geolocation.rb', line 34

def latitude
  @env['HTTP_GEOIP2_DATA_LATITUDE'].presence ||
    @env['HTTP_GEOIP_LATITUDE'].presence ||
    request.try(:latitude)
end

#longitudeObject



40
41
42
43
44
# File 'lib/workarea/geolocation.rb', line 40

def longitude
  @env['HTTP_GEOIP2_DATA_LONGITUDE'].presence ||
    @env['HTTP_GEOIP_LONGITUDE'].presence ||
    request.try(:longitude)
end

#postal_codeObject



30
31
32
# File 'lib/workarea/geolocation.rb', line 30

def postal_code
  @env['HTTP_GEOIP_POSTAL_CODE'].presence || request.try(:postal_code)
end

#regionObject



18
19
20
21
22
# File 'lib/workarea/geolocation.rb', line 18

def region
  region_from_geoip2.presence ||
    @env['HTTP_GEOIP_REGION'].presence ||
    request_data['region_code']
end