class Ganddyn::IpResolver

Public Class Methods

new(urls = {}) click to toggle source

urls: hash or ipv4 and ipv6 url services

{ :ipv4 => 'http://ipv4_url', :ipv6 => 'http://ipv6_url'}
# File lib/ganddyn/ipresolver.rb, line 9
def initialize urls = {}
  raise ArgumentError, 'urls is not a Hash' unless urls.is_a? Hash

  @ipv4_url = urls.has_key?(:ipv4) ? urls[:ipv4] : "http://v4.ipv6-test.com/api/myip.php"
  @ipv6_url = urls.has_key?(:ipv6) ? urls[:ipv6] : "http://v6.ipv6-test.com/api/myip.php"
end

Public Instance Methods

get_ipv4() click to toggle source
# File lib/ganddyn/ipresolver.rb, line 16
def get_ipv4
  get_url @ipv4_url
end
get_ipv6() click to toggle source
# File lib/ganddyn/ipresolver.rb, line 20
def get_ipv6
  get_url @ipv6_url
end

Private Instance Methods

get_url( iUrl ) click to toggle source
# File lib/ganddyn/ipresolver.rb, line 25
def get_url( iUrl )
  retval = nil
  uri = URI(iUrl)
  begin
    res = Net::HTTP.get_response(uri)
    if res.code == '200'
      retval = res.body
    end
  rescue SocketError => e
    # normally it happens if the server name is invalid
    # or if no network is available
    # let's return nil
  rescue Exception => e
    # normally it happen if the machine has no ipv6 support
    # let's return nil
  end
  retval
end