module RightScale::Api::BaseExtend
Public Instance Methods
[](*args)
click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_base.rb, line 269 def [](*args) ret = [] args.each { |arg| temp = [] begin if arg.is_a?(Hash) if arg.keys.first.to_s == "cloud_id" temp << find_by_cloud_id(arg.values.first.to_i) else temp << find_with_filter(arg) end elsif arg.is_a?(Regexp) temp << find_by(:nickname) { |n| n =~ arg } else temp << find(arg) end rescue end temp.flatten! if temp.empty? all = find_all if arg.is_a?(Hash) temp << all.select { |v| v.__send__(arg.keys.first.to_sym) =~ /#{arg.values.first}/ } elsif arg.is_a?(Regexp) temp += all.select { |n| n.name =~ arg } temp += all.select { |n| n.nickname =~ arg } if temp.empty? else temp += all.select { |n| n.name =~ /#{arg}/ } temp += all.select { |n| n.nickname =~ /#{arg}/ } if temp.empty? end end ret += temp } return (args.empty? ? find_all : ret.flatten.uniq) end
create(opts)
click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_base.rb, line 237 def create(opts) location = connection.post(self.resource_plural_name, self.resource_singular_name.to_sym => opts) newrecord = self.new('href' => location) newrecord.reload newrecord end
deny_methods(*symbols)
click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_base.rb, line 305 def deny_methods(*symbols) symbols.map! { |sym| sym.to_sym } if symbols.delete(:index) symbols |= [:find_all, :find_by, :find_by_cloud_id, :find_by_nickname, :find_by_nickname_speed, :find_with_filter] end if symbols.delete(:show) symbols |= [:show, :reload, :find, :find_by_id] end if symbols.delete(:update) symbols |= [:save, :update] end symbols.each do |sym| sym = sym.to_sym eval_str = "undef #{sym.inspect}" if self.respond_to?(sym) instance_eval(eval_str) elsif self.new.respond_to?(sym) class_eval(eval_str) end end end
filters()
click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_base.rb, line 265 def filters() [] end
find(href, additional_params={}) { |s| ... }
click to toggle source
Retrieves one or more resources of the same type.
@param [Integer|Symbol|String] href should be one of the following: resource id, :all, :first, :last, resource href @param [Hash] additional_params if href is an integer, will be part of retrieve request @param [Block] block if href is a symbol, will be used inside select block to refine results
# File lib/rest_connection/rightscale/rightscale_api_base.rb, line 211 def find(href, additional_params={}, &block) if href.is_a?(Integer) return self.new(connection.get(self.resource_plural_name + "/#{href}", additional_params)) elsif href.is_a?(Symbol) results = self.find_all if block_given? results = results.select { |s| yield(s) } end if href == :all return results elsif href == :first return results.first elsif href == :last return results.last end elsif uri = URI.parse(href) return self.new(connection.get(uri.path)) end nil end
find_all()
click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_base.rb, line 185 def find_all a = Array.new connection.get(self.resource_plural_name).each do |object| a << self.new(object) end return a end
find_by(attrib) { |s| ... }
click to toggle source
matches using result of block match expression ex: Server.find_by(:nickname) { |n| n =~ /production/ }
# File lib/rest_connection/rightscale/rightscale_api_base.rb, line 175 def find_by(attrib, &block) attrib = attrib.to_sym if self.filters.include?(attrib) connection.logger("#{self} includes the filter '#{attrib}', you might be able to speed up this API call") end self.find_all.select do |s| yield(s[attrib.to_s]) end end
find_by_cloud_id(cloud_id)
click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_base.rb, line 193 def find_by_cloud_id(cloud_id) a = Array.new connection.get(self.resource_plural_name, "cloud_id" => cloud_id).each do |object| a << self.new(object) end return a end
find_by_id(id)
click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_base.rb, line 232 def find_by_id(id) connection.logger("DEPRECATION WARNING: use of find_by_id is deprecated, please use find(id) ") self.find(id) end
find_by_nickname(nickname)
click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_base.rb, line 201 def find_by_nickname(nickname) connection.logger("DEPRECATION WARNING: use of find_by_nickname is deprecated, please use find_by(:nickname) { |n| n == '#{nickname}' } ") self.find_by(:nickname) { |n| n == nickname } end
find_by_nickname_speed(nickname)
click to toggle source
filter is only implemented on some api endpoints
# File lib/rest_connection/rightscale/rightscale_api_base.rb, line 245 def find_by_nickname_speed(nickname) self.find_with_filter('nickname' => nickname) end
find_with_filter(filter = {})
click to toggle source
filter is only implemented on some api endpoints
# File lib/rest_connection/rightscale/rightscale_api_base.rb, line 250 def find_with_filter(filter = {}) filter_params = [] filter.each { |key,val| unless self.filters.include?(key.to_sym) raise ArgumentError.new("#{key} is not a valid filter for resource #{self.resource_singular_name}") end filter_params << "#{key}=#{val}" } a = Array.new connection.get(self.resource_plural_name, :filter => filter_params).each do |object| a << self.new(object) end return a end
resource_plural_name()
click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_base.rb, line 165 def resource_plural_name self.to_s.underscore.pluralize end
resource_singular_name()
click to toggle source
# File lib/rest_connection/rightscale/rightscale_api_base.rb, line 169 def resource_singular_name self.to_s.underscore end