Class: Locality::IP

Inherits:
Object
  • Object
show all
Defined in:
lib/locality/ip.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_ip) ⇒ IP

Returns a new instance of IP.



13
14
15
# File 'lib/locality/ip.rb', line 13

def initialize(raw_ip)
  @raw_ip = raw_ip
end

Class Method Details

.check!Object



17
18
19
20
21
22
23
24
# File 'lib/locality/ip.rb', line 17

def self.check!
  if Locality.config.maxmind_geoip2_path.blank?
    fail download_message
  end

  # Just checking to see if it causes any trouble notiications, but not depending on it
  new('8.8.8.8').country_name
end

Instance Method Details

#city_nameObject



65
66
67
68
69
# File 'lib/locality/ip.rb', line 65

def city_name
  return unless lookup
  lookup.city.presence
rescue => exception
end

#coordinatesObject



47
48
49
50
51
# File 'lib/locality/ip.rb', line 47

def coordinates
  return unless lookup
  lookup.coordinates.presence
rescue => exception
end

#country_nameObject



83
84
85
86
87
# File 'lib/locality/ip.rb', line 83

def country_name
  return unless lookup
  lookup.country.presence
rescue => exception
end

#human_readable_locationObject



43
44
45
# File 'lib/locality/ip.rb', line 43

def human_readable_location
  city_name || province_name || state_name || country_name
end

#ipObject



37
38
39
40
41
# File 'lib/locality/ip.rb', line 37

def ip
  @ip ||= ::IPAddr.new(raw_ip.to_s)
rescue => exception
  nil
end

#latitudeObject



53
54
55
56
57
# File 'lib/locality/ip.rb', line 53

def latitude
  return unless lookup
  lookup.latitude.presence
rescue => exception
end

#longitudeObject



59
60
61
62
63
# File 'lib/locality/ip.rb', line 59

def longitude
  return unless lookup
  lookup.longitude.presence
rescue => exception
end

#province_nameObject



71
72
73
74
75
# File 'lib/locality/ip.rb', line 71

def province_name
  return unless lookup
  lookup.province.presence
rescue => exception
end

#state_nameObject



77
78
79
80
81
# File 'lib/locality/ip.rb', line 77

def state_name
  return unless lookup
  lookup.state.presence
rescue => exception
end

#to_hashObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/locality/ip.rb', line 26

def to_hash
  {
    ip: ip.to_s,
    city_name: city_name,
    province_name: province_name,
    state_name: state_name,
    country_name: country_name,
    human_readable_location: human_readable_location,
  }
end