Class: Springcm::Document

Inherits:
Resource show all
Includes:
Mixins::AccessLevel, Mixins::Attributes, Mixins::ParentFolder
Defined in:
lib/springcm-sdk/document.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Attributes

#apply_attribute_group, #attribute_group, #attribute_groups=

Methods included from Mixins::ParentFolder

#move, #parent_folder_href

Methods included from Mixins::AccessLevel

#access_level, #create?, #move?, #read?, #see?, #set_access?, #write?

Methods inherited from Resource

#get, #patch, #put, #reload, #reload!, #resource_name, #resource_params, #resource_uri, #uid

Methods inherited from Object

#initialize, #method_missing, #raw

Constructor Details

This class inherits a constructor from Springcm::Object

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Springcm::Object

Class Method Details

.resource_paramsObject



14
15
16
17
18
# File 'lib/springcm-sdk/document.rb', line 14

def self.resource_params
  {
    "expand" => "attributegroups,path"
  }
end

Instance Method Details

#copy(path: nil, uid: nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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

#deleteObject



124
125
126
127
128
129
130
# File 'lib/springcm-sdk/document.rb', line 124

def delete
  reload!
  if path.start_with?("/#{@client..name}/Trash")
    raise Springcm::DeleteRefusedError.new(path)
  end
  unsafe_delete
end

#delete!Object



117
118
119
# File 'lib/springcm-sdk/document.rb', line 117

def delete!
  unsafe_delete
end

#downloadObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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

#upload_version(file:, type: :binary) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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