BaseObject model basically provide the basic operation around REST model, like defining a links between different objects, element with text values, or collection of these elements
For initializing new object you require to set id, url, client and name attribute.
# File lib/base_object.rb, line 34 def initialize(opts={}, &block) @id, @url, @client, @base_name = opts[:id], opts[:url], opts[:client], opts[:name] @objects = [] raise BaseObjectParamError if @id.nil? or @url.nil? or @client.nil? or @base_name.nil? yield self if block_given? end
This method define collection of text elements inside REST model XML syntax: <addresses>
<address>127.0.0.1</address> <address>127.0.0.2</address> </addresses>
# File lib/base_object.rb, line 112 def add_addresses!(collection_name, values=[]) @objects << { :type => :collection, :method_name => collection_name.sanitize, :values => values.collect { |v| { :address => v.text.strip, :type => v[:type] }} } end
# File lib/base_object.rb, line 79 def add_authentication!(auth_type, values=[]) value = { :key => (values/'login/keyname').text.strip } if auth_type == 'key' if auth_type == 'password' value = { :username => (values/'login/username').text.strip, :username => (values/'login/password').text.strip } end @objects << { :type => :collection, :method_name => 'authentication', :values => value } end
This method adds blobs to the blob_list property of a bucket
# File lib/base_object.rb, line 165 def add_blob!(blob_name) if @blob_list.nil? @blob_list = [blob_name] @objects << { :type => :list, :method_name => "blob_list", :value => @blob_list } else @blob_list << blob_name current = search_for_method('blob_list') current[:value] = @blob_list end end
This method define collection of text elements inside REST model XML syntax: <addresses>
<address>127.0.0.1</address> <address>127.0.0.2</address> </addresses>
# File lib/base_object.rb, line 125 def add_collection!(collection_name, values=[]) @objects << { :type => :collection, :method_name => collection_name.sanitize, :values => values } end
Method add property for hardware profile
# File lib/base_object.rb, line 57 def add_hwp_property!(name, property, type) hwp_property=case type when :float then DeltaCloud::HWP::FloatProperty.new(property, name) when :integer then DeltaCloud::HWP::Property.new(property, name) end @objects << { :type => :property, :method_name => name.sanitize, :property => hwp_property } end
This method add link to another object in REST model XML syntax: <link rel="destroy" href="localhost/api/resource" method="post"/>
# File lib/base_object.rb, line 43 def add_link!(object_name, id) @objects << { :type => :link, :method_name => object_name.sanitize, :id => id } @objects << { :type => :text, :method_name => "#{object_name.sanitize}_id", :value => id } end
# File lib/base_object.rb, line 94 def add_provider!(provider_id, entrypoints) @providers ||= [] @providers << { provider_id.intern => entrypoints.map { |e| { :kind => e[:kind], :url => e.text } } } @objects << { :type => :collection, :method_name => 'providers', :values => @providers } end
This method define text object in REST model XML syntax: <name>Instance 1</name>
# File lib/base_object.rb, line 71 def add_text!(object_name, value) @objects << { :type => :text, :method_name => object_name.sanitize, :value => value } end
Basic method hander. This define a way how value from property will be returned
# File lib/base_object.rb, line 135 def method_handler(m, args=[]) case m[:type] when :link then return @client.send(m[:method_name].singularize, m[:id]) when :text then return m[:value] when :property then return m[:property] when :collection then return m[:values] when :list then return m[:value].join(", ") else raise NoHandlerForMethod end end
# File lib/base_object.rb, line 146 def method_missing(method_name, *args) # First of all search throught array for method name m = search_for_method(method_name) if m.nil? if method_name == :"valid_provider?" return providers.any? { |p| p.keys.include? args.first.to_sym } end if method_name == :"valid_provider_url?" return providers.map { |p| !p.find { |k, v| v.find { |u| u[:url] == args.first } }.nil? } end super else # Call appropriate handler for method method_handler(m, args) end end