Method: JSS::Uploadable#upload

Defined in:
lib/jss-api/api_object/uploadable.rb

#upload(type, local_file) ⇒ String

Upload a file to the JSS via the REST Resource of the object to which this module is mixed in.

Parameters:

  • type (Symbol)

    the type of upload happening. Must be one of the keys defined in the class’s UPLOAD_TYPES Hash.

  • local_file (String, Pathname)

    String or Pathname pointing to the locally-readable file to be uploaded.

Returns:

  • (String)

    The xml response from the server.

Raises:



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/jss-api/api_object/uploadable.rb', line 117

def upload(type, local_file)

  ### the thing's gotta be in the JSS, and have an @id
  raise JSS::NoSuchItemError, 'Create this #{self.class::RSRC_OBJECT_KEY} in the JSS before uploading files to it.' unless @id and @in_jss

  ### the type has to be defined in the class of self.
  raise JSS::InvalidDataError, "#{self.class::RSRC_LIST_KEY} only take uploads of type: :#{self.class::UPLOAD_TYPES.keys.join(', :')}." unless self.class::UPLOAD_TYPES.keys.include? type

  ### figure out the resource after the UPLOAD_RSRC_PREFIX
  upload_rsrc = "#{UPLOAD_RSRC_PREFIX}/#{self.class::UPLOAD_TYPES[type]}/id/#{@id}"

  ### make a File object to hand to REST.
  file = File.new local_file.to_s, 'rb'

  ### upload it!
  JSS::API.cnx[upload_rsrc].post :name => file

end