class RHC::Rest::Base

Constants

URI_ESCAPE_REGEX

Attributes

client[R]

Public Class Methods

new(attrs=nil, client=nil) click to toggle source
# File lib/rhc/rest/base.rb, line 13
def initialize(attrs=nil, client=nil)
  @attributes = (attrs || {}).stringify_keys!
  @attributes['messages'] ||= []
  @client = client
end

Public Instance Methods

add_message(msg) click to toggle source
# File lib/rhc/rest/base.rb, line 19
def add_message(msg)
  messages << msg
end
has_param?(sym, name) click to toggle source
# File lib/rhc/rest/base.rb, line 50
def has_param?(sym, name)
  if l = link(sym)
    (l['required_params'] || []).any?{ |p| p['name'] == name} or (l['optional_params'] || []).any?{ |p| p['name'] == name}
  end
end
rest_method(link_name, payload={}, options={}) click to toggle source
# File lib/rhc/rest/base.rb, line 23
def rest_method(link_name, payload={}, options={})
  link = link(link_name)
  raise "No link defined for #{link_name}" unless link
  url = link['href']
  url = url.gsub(/:\w+/) { |s| URI.escape(options[:params][s], URI_ESCAPE_REGEX) || s } if options[:params]
  method = options[:method] || link['method']

  result = client.request(options.merge({
    :url => url,
    :method => method,
    :payload => payload,
  }))
  if result.is_a?(Hash) && (result['messages'] || result['errors'])
    attributes['messages'] = Array(result['messages'])
    result = self
  end
  result
end
supports?(sym) click to toggle source
# File lib/rhc/rest/base.rb, line 46
def supports?(sym)
  !!link(sym)
end

Protected Instance Methods

debug(msg, obj=nil) click to toggle source
# File lib/rhc/rest/base.rb, line 72
def debug(msg, obj=nil)
  client.debug("#{msg}#{obj ? " #{obj}" : ''}") if client && client.debug?
end
debug?() click to toggle source
# File lib/rhc/rest/base.rb, line 76
def debug?
  client && client.debug?
end