class StudioApi::File

Represents overlay files which can be loaded to appliance.

Supports finding files for appliance, updating metadata, deleting, uploading and downloading.

@example Find files for appliance StudioApi::File.find :all, :params => { :appliance_id => 1234 }

@example Upload file Xorg.conf

File.open ("/tmp/xorg.conf") do |file|
  StudioApi::File.upload file, 1234, :path => "/etc/X11",
                      :filename => "Xorg.conf", :permissions => "0755",
                      :owner => "root"
end

@example Update metadata file = StudioApi::File.find 1234 file.owner = “root” file.path = “/etc” file.filename = “pg.conf” file.save

Public Class Methods

upload( content, appliance_id, options = {}) click to toggle source

Uploads file to appliance @param (String,File) content as String or as opened File

( in this case its name is used as default for uploaded file name)

@param (to_i) appliance_id id of appliance where to upload @param (Hash<#to_s,#to_s>) options optional parameters, see API documentation @return [StudioApi::File] metadata of uploaded file

   # File lib/studio_api/file.rb
58 def self.upload ( content, appliance_id, options = {})
59   request_str = "files?appliance_id=#{appliance_id.to_i}"
60   options.each do |k,v|
61     request_str << "&#{CGI.escape k.to_s}=#{CGI.escape v.to_s}"
62   end
63   rq = GenericRequest.new studio_connection
64   response = rq.post request_str, :file => content
65   if defined? ActiveModel #rails 3 and ActiveResource persistency
66     File.new Hash.from_xml(response)["file"],true
67   else
68     File.new Hash.from_xml(response)["file"]
69   end
70 end

Public Instance Methods

content(&block) click to toggle source

Downloads file to output. Allow downloading to stream or to path. @return [String] content of file if no block @return [nil] if block given @yield [socket response segment] Read the Net::HTTPResponse segments @yieldparam[body segment] buffered chunk of body @yieldreturn [nil]

   # File lib/studio_api/file.rb
35 def content &block
36   rq = GenericRequest.new self.class.studio_connection
37   path = "/files/#{id.to_i}/data"
38   block_given? ? rq.get_file(path, &block) : rq.get(path)
39 end
overwrite( content ) click to toggle source

Overwritte file content and keep metadata ( of course without such things like size ) Immediatelly store new content @param (File,#to_s) input new content for file as String or open file @return [StudioApi::File] self with updated metadata

   # File lib/studio_api/file.rb
45 def overwrite ( content )
46   request_str = "/files/#{id.to_i}/data"
47   rq = GenericRequest.new self.class.studio_connection
48   response = rq.put request_str, :file => content
49   load Hash.from_xml(response)["file"]
50 end

Private Instance Methods

new?() click to toggle source

file uses for update parameter put @private

   # File lib/studio_api/file.rb
75 def new?
76   false
77 end