Module: Onering::Util

Extended by:
Util
Included in:
API, Reporter, Util
Defined in:
lib/onering/util.rb

Defined Under Namespace

Modules: String

Constant Summary collapse

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'
}

Instance Method Summary collapse

Instance Method Details

#fact(name, default = nil) ⇒ Object



66
67
68
69
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/onering/util.rb', line 66

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

#gem_path(name) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/onering/util.rb', line 56

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

#http_status(code) ⇒ Object



101
102
103
# File 'lib/onering/util.rb', line 101

def http_status(code)
  return (HTTP_STATUS_CODES[code.to_i] || nil)
end

#make_filter(filter) ⇒ Object



95
96
97
98
99
# File 'lib/onering/util.rb', line 95

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