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



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

def initialize(raw_ip)
  @raw_ip = raw_ip
end

Class Method Details

.check!Object



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

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



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

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

#coordinatesObject



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

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

#country_nameObject



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

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

#human_readable_locationObject



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

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

#ipObject



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

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

#latitudeObject



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

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

#longitudeObject



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

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

#province_nameObject



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

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

#state_nameObject



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

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

#to_hashObject



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

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