lib/onering/util.rb in onering-client-0.0.46 vs lib/onering/util.rb in onering-client-0.0.50

- old
+ new

@@ -5,21 +5,111 @@ return nil if (self.strip.chomp.empty? rescue true) self.strip.chomp end end + extend self - def self.gem_path(name) + + 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) + name = name.to_s + + if defined?(Facter) + fact = Facter.value(name) + + # short circuit nil responses + return default if fact.nil? + + # if we are asking for a nested object... + if name.include?('.') + # ...and the response IS an object... + if fact.is_a?(Hash) + # remove the first part and return the rest + name = name.sub(/^[^\.]+\./,'') + return fact.get(name, default) + else + # not an object, return default + return default + end + else + # this is a simple request, return the fact + return fact + end + end + + return 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 \ No newline at end of file