module Onering module Util module String def nil_empty return nil if (self.strip.chomp.empty? rescue true) self.strip.chomp end def to_bytes case self when /^\s*([\d\.]+)\s*([KMGTPEZY]?)([Bb])\s*$/ power = case $2 when 'K' then 1 when 'M' then 2 when 'G' then 3 when 'T' then 4 when 'P' then 5 when 'E' then 6 when 'Z' then 7 when 'Y' then 8 else 0 end num = (Integer($1) rescue Float($1)) div = ($3 == 'b' ? 8 : 1) return ((num * (1024 ** power)) / div) else return nil end end end extend self HTTP_STATUS_CODES = { 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', 418 => 'I\'m a Teapot', 420 => 'Enhance Your Calm', 422 => 'Unprocessable Entity', 423 => 'Locked', 424 => 'Failed Dependency', 426 => 'Upgrade Required', 428 => 'Precondition Required', 429 => 'Too Many Requests', 431 => 'Request Header Fields Too Large', 444 => 'No Response', 451 => 'Unavailable For Legal Reasons', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', 508 => 'Loop Detected', 509 => 'Bandwidth Limit Exceeded', 510 => 'Not Extended', 511 => 'Network Authentication Required' } def gem_path(name) if Gem::Specification.respond_to?(:find_by_name) return Gem::Specification.find_by_name(name).gem_dir else return Gem::SourceIndex.from_installed_gems.find_name(name).sort{|a,b| a.version.to_s <=> b.version.to_s }.last.full_gem_path end end def fact(name, default=nil) reporter = Onering::Reporter.new() name = name.to_s if defined?(Facter) if name.downcase == 'all' return Facter.to_hash() else fact_value = Facter.value(name) # short circuit nil responses return default if fact_value.nil? # if we are asking for a nested object... if name.include?('.') # ...and the response IS an object... if fact_value.is_a?(Hash) # remove the first part and return the rest name = name.sub(/^[^\.]+\./,'') return fact_value.get(name, default) else # not an object, return default return reporter.report().get(name, default) end else # this is a simple request, return the fact return fact_value end end end return reporter.report().get(name, default) end def make_filter(filter) filter = filter.collect{|k,v| "#{k}/#{v}" } if filter.is_a?(Hash) filter = filter.collect{|i| i.sub(':','/') }.join("/") if filter.is_a?(Array) return filter end def http_status(code) return (HTTP_STATUS_CODES[code.to_i] || nil) end end end class String include Onering::Util::String end class Module def submodules constants.collect {|const_name| const_get(const_name)}.select {|const| const.class == Module} end end class NilClass def <=>(*args) return 1 end end