class Springcm::Document

Public Class Methods

resource_params() click to toggle source
# File lib/springcm-sdk/document.rb, line 14
def self.resource_params
  {
    "expand" => "attributegroups,path"
  }
end

Public Instance Methods

copy(path: nil, uid: nil) click to toggle source
# File lib/springcm-sdk/document.rb, line 97
def copy(path: nil, uid: nil)
  parent = @client.folder(path: path, uid: uid)
  body = {
    "DestinationFolder" => parent.raw,
    "DocumentsToCopy" => [ raw ]
  }
  conn = @client.authorized_connection(url: @client.object_api_url)
  res = conn.post do |req|
    req.headers["Content-Type"] = "application/json"
    req.url "copytasks"
    req.body = body.to_json
  end
  if res.success?
    data = JSON.parse(res.body)
    CopyTask.new(data, @client)
  else
    nil
  end
end
delete() click to toggle source
# File lib/springcm-sdk/document.rb, line 124
def delete
  reload!
  if path.start_with?("/#{@client.account.name}/Trash")
    raise Springcm::DeleteRefusedError.new(path)
  end
  unsafe_delete
end
Also aliased as: unsafe_delete
delete!() click to toggle source
# File lib/springcm-sdk/document.rb, line 117
def delete!
  unsafe_delete
end
download() click to toggle source
# File lib/springcm-sdk/document.rb, line 20
def download
  io = StringIO.new
  conn = @client.authorized_connection(url: @client.download_api_url)
  res = conn.get do |req|
    req.url resource_uri
    req.options.on_data = Proc.new do |chunk, total_bytes|
      io << chunk
    end
  end
  if res.success?
    io.rewind
    io
  else
    nil
  end
end
history(offset: 0, limit: 20) click to toggle source
# File lib/springcm-sdk/document.rb, line 37
def history(offset: 0, limit: 20)
  Helpers.validate_offset_limit!(offset, limit)
  conn = @client.authorized_connection(url: @client.object_api_url)
  res = conn.get do |req|
    req.url "#{resource_uri}/historyitems"
    req.params["offset"] = offset
    req.params["limit"] = limit
  end
  if res.success?
    data = JSON.parse(res.body)
    ResourceList.new(data, self, HistoryItem, @client)
  else
    nil
  end
end
unsafe_delete()
Alias for: delete
upload_version(file:, type: :binary) click to toggle source
# File lib/springcm-sdk/document.rb, line 69
def upload_version(file:, type: :binary)
  # TODO: DRY Folder#upload
  file_types = {
    binary: "application/octet-stream",
    pdf: "application/pdf",
    csv: "text/csv",
    txt: "text/plain"
  }
  if !type.nil? && !file_types.keys.include?(type)
    raise ArgumentError.new("File type must be one of: nil, #{file_types.map(&:inspect).join(", ")}")
  end
  conn = @client.authorized_connection(url: @client.upload_api_url)
  res = conn.post do |req|
    req.headers["Content-Type"] = file_types[type]
    req.headers["Content-Length"] = file.size.to_s
    req.headers["Content-Disposition"] = "attachment; filename=\"#{name}\""
    req.url "documents/#{uid}"
    req.body = file
  end
  if res.success?
    data = JSON.parse(res.body)
    Document.new(data, @client)
  else
    nil
    puts res.body
  end
end
versions(offset: 0, limit: 20) click to toggle source
# File lib/springcm-sdk/document.rb, line 53
def versions(offset: 0, limit: 20)
  Helpers.validate_offset_limit!(offset, limit)
  conn = @client.authorized_connection(url: @client.object_api_url)
  res = conn.get do |req|
    req.url "#{resource_uri}/versions"
    req.params["offset"] = offset
    req.params["limit"] = limit
  end
  if res.success?
    data = JSON.parse(res.body)
    ResourceList.new(data, self, Document, @client, method_override: :versions)
  else
    nil
  end
end