Module: ImmosquareConstants::Ip

Defined in:
lib/immosquare-constants/ip.rb

Class Method Summary collapse

Class Method Details

.get_real_ipObject

##

On récupère l’IP réelle

##


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/immosquare-constants/ip.rb', line 12

def get_real_ip
  begin
    begin
      uri = URI.parse("https://checkip.amazonaws.com/")
      response = Net::HTTP.get_response(uri)
      raise("No IP found") unless response.is_a?(Net::HTTPSuccess)

      response.body.strip
    rescue StandardError => e
      s  = Socket.ip_address_list.find(&:ipv4_private?)
      s  = Socket.ip_address_list.first if s.blank?
      raise("No IP found") if s.blank?

      s.ip_address
    end
  rescue StandardError => e
    "0.0.0.0"
  end
end