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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/onering/util.rb', line 90

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 default
        end
      else
    # this is a simple request, return the fact
        return fact_value
      end
    end
  end

  return default
end

#gem_path(name) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/onering/util.rb', line 80

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



130
131
132
# File 'lib/onering/util.rb', line 130

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

#make_filter(filter) ⇒ Object



124
125
126
127
128
# File 'lib/onering/util.rb', line 124

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