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



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

#coordinatesArray<Number>

can be found

Returns:

  • (Array<Number>)

    or nil if neither latitude nor longitude



54
55
56
57
# File 'lib/workarea/geolocation.rb', line 54

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 ||= Country[
    @env['HTTP_GEOIP2_DATA_COUNTRY_CODE'].presence ||
      @env['HTTP_GEOIP_CITY_COUNTRY_CODE'].presence ||
        request.try(:country_code)
  ]
end

#latitudeObject



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

#longitudeObject



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

#namesObject



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_codeObject



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

#regionObject



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

#subdivisionObject



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