Module: PuppetX::SIMP::Simplib
- Defined in:
- lib/puppetx/simp/simplib.rb
Class Method Summary collapse
-
.hostname?(obj) ⇒ Boolean
Determine whether or not the passed value is a valid hostname, optionally postpended with ‘:<number>’ or ‘/<number>’.
-
.hostname_only?(obj) ⇒ Boolean
Determine whether or not the passed value is a valid hostname.
-
.human_sort(obj) ⇒ Object
Sort a list of values based on usual human sorting semantics.
-
.split_port(host_string) ⇒ Object
Return a host/port pair.
Class Method Details
.hostname?(obj) ⇒ Boolean
Determine whether or not the passed value is a valid hostname, optionally postpended with ‘:<number>’ or ‘/<number>’.
Returns false if is not comprised of ASCII letters (upper or lower case), digits, hypens (except at the beginning and end), and dots (except at beginning and end), excluding an optional, trailing ‘:<number>’ or ‘/<number>’
NOTE: This returns true for an IPv4 address, as it conforms to RFC 1123.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/puppetx/simp/simplib.rb', line 52 def self.hostname?(obj) # This regex shamelessly lifted from # http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address # and then augmented for postpends hname_regex = Regexp.new(/^ ( ([a-zA-Z0-9]| [a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\. )* ( [A-Za-z0-9]| [A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9] )((:|\/)?(\d+))?$/x) !hname_regex.match(obj).nil? end |
.hostname_only?(obj) ⇒ Boolean
Determine whether or not the passed value is a valid hostname.
Returns false if is not comprised of ASCII letters (upper or lower case), digits, hypens (except at the beginning and end), and dots (except at beginning and end)
NOTE: This returns true for an IPv4 address, as it conforms to RFC 1123.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/puppetx/simp/simplib.rb', line 27 def self.hostname_only?(obj) # This regex shamelessly lifted from # http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address hname_regex = Regexp.new(/^ ( ([a-zA-Z0-9]| [a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\. )* ( [A-Za-z0-9]| [A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9] )$/x) !hname_regex.match(obj).nil? end |
.human_sort(obj) ⇒ Object
Sort a list of values based on usual human sorting semantics.
This regex taken from www.bofh.org.uk/2007/12/16/comprehensible-sorting-in-ruby
10 11 12 13 14 15 16 17 18 |
# File 'lib/puppetx/simp/simplib.rb', line 10 def self.human_sort(obj) obj.to_s.split(/((?:(?:^|\s)[-+])?(?:\.\d+|\d+(?:\.\d+?(?:[eE]\d+)?(?:$|(?![eE\.])))?))/ms).map { |v| begin Float(v) rescue v.downcase end } end |
.split_port(host_string) ⇒ Object
Return a host/port pair
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/puppetx/simp/simplib.rb', line 70 def self.split_port(host_string) return [nil,nil] if host_string.nil? or host_string.empty? # CIDR addresses do not have ports return [host_string, nil] if host_string.include?('/') # IPv6 Easy if host_string.include?(']') host_pair = host_string.split(/\]:?/) host_pair[0] = host_pair[0] + ']' host_pair[1] = nil if host_pair.size == 1 # IPv6 Fallback elsif host_string.count(':') > 1 # Normalize IPv6 addresses to have '[]' for clarity host_pair = [%([#{host_string}]),nil] # Everything Else elsif host_string.include?(':') host_pair = host_string.split(':') else host_pair = [host_string,nil] end host_pair end |