Class: Ipify

Inherits:
Object
  • Object
show all
Defined in:
lib/detectors/ipify.rb

Overview

Detects your current IP via ‘ifconfig`

Instance Method Summary collapse

Constructor Details

#initialize(network_interface, version) ⇒ Ipify

Returns a new instance of Ipify.



5
6
7
8
9
10
# File 'lib/detectors/ipify.rb', line 5

def initialize(network_interface, version)
  @network_interface = network_interface
  @version = version == 6 ? 6 : 4

  raise 'Ipify supports only IPv4 yet' if version == 6
end

Instance Method Details

#detectObject



12
13
14
15
16
17
18
# File 'lib/detectors/ipify.rb', line 12

def detect
  ip = `curl -#{@version} --interface #{@network_interface} 'https://api.ipify.org?format=text' -s`

  raise 'no ip detected' unless ip

  ip
end