Class: String

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

Overview

Extend string to include some helpful stuff

Instance Method Summary collapse

Instance Method Details

#fqdn?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/string_ext.rb', line 101

def fqdn?
  begin
    resolve_fqdn
  rescue SocketError, Timeout::Error
    return false
  end

  if ip_addr?
    return false
  else
    return true
  end
end

#hexify(opts = {}) ⇒ Object



77
78
79
# File 'lib/string_ext.rb', line 77

def hexify
  self.each_byte.map { |b| b.to_s(16).rjust(2,'0') }.join
end

#ip_addr?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/string_ext.rb', line 15

def ip_addr?
  begin
    IPAddr.new(self)

  # Using ArgumentError instead of IPAddr::InvalidAddressError
  # for 1.9.3 backward compatibility
  rescue ArgumentError
    return false
  end

  return true
end

#resolve_fqdnObject



57
58
59
60
61
62
63
# File 'lib/string_ext.rb', line 57

def resolve_fqdn
  begin
    IPSocket.getaddress(self)
  rescue SocketError
    nil # Can return anything you want here
  end
end

#resolve_fqdn_as_ipv4(timeout = 3) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/string_ext.rb', line 42

def resolve_fqdn_as_ipv4(timeout = 3)
  begin
    Timeout::timeout(timeout) {
      Resolv::DNS.open do |dns|
        ress = dns.getresources self, Resolv::DNS::Resource::IN::A
        temp = ress.map { |r| r.address  }
        return temp[0]
      end
    }
  rescue Timeout::Error
    return ""
  end

end

#resolve_fqdn_as_ipv6(timeout = 3) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/string_ext.rb', line 28

def resolve_fqdn_as_ipv6(timeout = 3)
  begin
    Timeout::timeout(timeout) {
      Resolv::DNS.open do |dns|
        ress = dns.getresources self, Resolv::DNS::Resource::IN::AAAA
        temp = ress.map { |r| r.address  }
        return temp[0]
      end
    }
  rescue Timeout::Error
    return ""
  end
end

#resolve_ptr(timeout = 3) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/string_ext.rb', line 65

def resolve_ptr(timeout = 3)
  begin
    Timeout::timeout(timeout) {
      reversed_dns = Resolv.new.getname(self)
      return reversed_dns
    }
  rescue Timeout::Error,Resolv::ResolvError
    return ""
  end
end

#unhexifyObject



7
8
9
# File 'lib/string_ext.rb', line 7

def unhexify
  [self].pack("H*")
end