Class: StudioApi::File

Inherits:
ActiveResource::Base
  • Object
show all
Extended by:
StudioResource
Defined in:
lib/studio_api/file.rb

Overview

Represents overlay files which can be loaded to appliance.

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

StudioApi::File.find :all, :params => { :appliance_id => 1234 }

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

Examples:

Find files for appliance

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

Update metadata

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StudioResource

collection_path, element_path, extended, studio_connection, studio_connection=

Class Method Details

.upload(content, appliance_id, options = {}) ⇒ StudioApi::File

Uploads file to appliance

Parameters:

  • content (String, File)

    as String or as opened File ( in this case its name is used as default for uploaded file name)

  • appliance_id (#to_i)

    id of appliance where to upload

  • options (Hash<#to_s,#to_s>) (defaults to: {})

    optional parameters, see API documentation

Returns:



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/studio_api/file.rb', line 58

def self.upload ( content, appliance_id, options = {})
  request_str = "files?appliance_id=#{appliance_id.to_i}"
  options.each do |k,v|
    request_str << "&#{CGI.escape k.to_s}=#{CGI.escape v.to_s}"
  end
  rq = GenericRequest.new studio_connection
  response = rq.post request_str, :file => content
  if defined? ActiveModel #rails 3 and ActiveResource persistency
    File.new Hash.from_xml(response)["file"],true
  else
    File.new Hash.from_xml(response)["file"]
  end
end

Instance Method Details

#content {|socket response segment| ... } ⇒ String?

Downloads file to output. Allow downloading to stream or to path. @yieldparam[body segment] buffered chunk of body

Yields:

  • (socket response segment)

    Read the Net::HTTPResponse segments

Yield Returns:

  • (nil)

Returns:

  • (String)

    content of file if no block

  • (nil)

    if block given



35
36
37
38
39
# File 'lib/studio_api/file.rb', line 35

def content &block
  rq = GenericRequest.new self.class.studio_connection
  path = "/files/#{id.to_i}/data"
  block_given? ? rq.get_file(path, &block) : rq.get(path)
end

#overwrite(content) ⇒ StudioApi::File

Overwritte file content and keep metadata ( of course without such things like size ) Immediatelly store new content

Parameters:

  • input (File, #to_s)

    new content for file as String or open file

Returns:



45
46
47
48
49
50
# File 'lib/studio_api/file.rb', line 45

def overwrite ( content )
  request_str = "/files/#{id.to_i}/data"
  rq = GenericRequest.new self.class.studio_connection
  response = rq.put request_str, :file => content
  load Hash.from_xml(response)["file"]
end