# File lib/rhc/rest/base.rb, line 11 def initialize(attrs=nil, client=nil) @attributes = (attrs || {}).stringify_keys! @attributes['messages'] ||= [] @client = client end
# File lib/rhc/rest/base.rb, line 17 def add_message(msg) messages << msg end
# File lib/rhc/rest/base.rb, line 48 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
# File lib/rhc/rest/base.rb, line 54 def link_href(sym, params=nil, &block) if (l = link(sym)) && (h = l['href']) h = h.gsub(%r:\w+/){ |s| params[s].nil? ? s : CGI.escape(params[s]) } if params return h end yield if block_given? end
# File lib/rhc/rest/base.rb, line 40 def links attributes['links'] || {} end
# File lib/rhc/rest/base.rb, line 21 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(%r:\w+/) { |s| CGI.escape(options[:params][s]) || 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
# File lib/rhc/rest/base.rb, line 44 def supports?(sym) !!link(sym) end
# File lib/rhc/rest/base.rb, line 69 def debug(msg, obj=nil) client.debug("#{msg}#{obj ? " #{obj}" : ''}") if client && client.debug? end
# File lib/rhc/rest/base.rb, line 73 def debug? client && client.debug? end
# File lib/rhc/rest/base.rb, line 65 def link(sym) (links[sym.to_s] || links[sym.to_s.upcase]) end