Class: Podio::FileAttachment

Inherits:
ActivePodio::Base show all
Defined in:
lib/podio/models/file_attachment.rb

Overview

Instance Attribute Summary

Attributes inherited from ActivePodio::Base

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActivePodio::Base

#==, #[], #[]=, #as_json, collection, delegate_to_hash, has_many, has_one, #hash, #initialize, #initialize_attributes, klass_from_string, list, member, #new_record?, output_attribute_as_json, #parent_model, #persisted?, property, #to_param

Constructor Details

This class inherits a constructor from ActivePodio::Base

Class Method Details

.attach(id, ref_type, ref_id) ⇒ Object

Attach a file to an existing reference



85
86
87
88
89
90
# File 'lib/podio/models/file_attachment.rb', line 85

def attach(id, ref_type, ref_id)
  Podio.connection.post do |req|
    req.url "/file/#{id}/attach"
    req.body = {:ref_type => ref_type, :ref_id => ref_id}
  end
end

.copy(id) ⇒ Object



93
94
95
# File 'lib/podio/models/file_attachment.rb', line 93

def copy(id)
  Podio.connection.post("/file/#{id}/copy").body['file_id']
end

.create(name, content_type) ⇒ Object

Uploading a file is a two-step operation First, the file must be created to get a file id and the path to move it to



176
177
178
179
180
181
182
183
# File 'lib/podio/models/file_attachment.rb', line 176

def create(name, content_type)
  response = Podio.connection.post do |req|
    req.url "/file/"
    req.body = { :name => name, :mimetype => content_type }
  end

  response.body
end

.create_from_external_file_id(linked_account_id, external_file_id, preserve_permissions = false, options = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/podio/models/file_attachment.rb', line 71

def create_from_external_file_id(, external_file_id, preserve_permissions=false, options={})
  response = Podio.client.connection.post do |req|
    req.url("/file/linked_account/#{}/", options)
    req.body = {
        :external_file_id     => external_file_id,
        :preserve_permissions => preserve_permissions
    }
  end

  member response.body
end

.delete(id) ⇒ Object



98
99
100
# File 'lib/podio/models/file_attachment.rb', line 98

def delete(id)
  Podio.connection.delete("/file/#{id}")
end

.find(id) ⇒ Object



103
104
105
# File 'lib/podio/models/file_attachment.rb', line 103

def find(id)
  member Podio.connection.get("/file/#{id}").body
end

.find_all(options = {}) ⇒ Object



113
114
115
116
117
# File 'lib/podio/models/file_attachment.rb', line 113

def find_all(options={})
  list Podio.connection.get { |req|
    req.url("/file/", options)
  }.body
end

.find_for_app(app_id, options = {}) ⇒ Object



120
121
122
123
124
# File 'lib/podio/models/file_attachment.rb', line 120

def find_for_app(app_id, options={})
  list Podio.connection.get { |req|
    req.url("/file/app/#{app_id}/", options)
  }.body
end

.find_for_google(linked_account_id, options = {}) ⇒ Object



147
148
149
150
151
# File 'lib/podio/models/file_attachment.rb', line 147

def find_for_google(, options={})
  list Podio.connection.get { |req|
    req.url("/file/google/#{}/", options)
  }.body
end

.find_for_space(space_id, options = {}) ⇒ Object



127
128
129
130
131
# File 'lib/podio/models/file_attachment.rb', line 127

def find_for_space(space_id, options={})
  list Podio.connection.get { |req|
    req.url("/file/space/#{space_id}/", options)
  }.body
end

.find_latest_for_app(app_id, options = {}) ⇒ Object



134
135
136
137
138
# File 'lib/podio/models/file_attachment.rb', line 134

def find_latest_for_app(app_id, options={})
  list Podio.connection.get { |req|
    req.url("/file/app/#{app_id}/latest/", options)
  }.body
end

.find_latest_for_space(space_id, options = {}) ⇒ Object



141
142
143
144
145
# File 'lib/podio/models/file_attachment.rb', line 141

def find_latest_for_space(space_id, options={})
  list Podio.connection.get { |req|
    req.url("/file/space/#{space_id}/latest/", options)
  }.body
end

.find_raw(id) ⇒ Object



108
109
110
# File 'lib/podio/models/file_attachment.rb', line 108

def find_raw(id)
  Podio.client.connection.get("/file/#{id}/raw").body
end

.replace(old_file_id, new_file_id) ⇒ Object



154
155
156
157
158
159
# File 'lib/podio/models/file_attachment.rb', line 154

def replace(old_file_id, new_file_id)
  Podio.connection.post { |req|
    req.url "/file/#{new_file_id}/replace"
    req.body = { :old_file_id => old_file_id }
  }.body
end

.set_available(id) ⇒ Object

Then, when the file has been moved, it must be marked as available



186
187
188
# File 'lib/podio/models/file_attachment.rb', line 186

def set_available(id)
  Podio.connection.post "/file/#{id}/available"
end

.update(id, description) ⇒ Object



162
163
164
165
166
167
# File 'lib/podio/models/file_attachment.rb', line 162

def update(id, description)
  Podio.connection.put { |req|
    req.url "/file/#{file_id}"
    req.body = { :description => description }
  }.body
end

.upload(file_stream, file_name) ⇒ Object

Accepts an open file stream along with a file name and uploads the file to Podio



51
52
53
54
55
56
57
58
59
60
# File 'lib/podio/models/file_attachment.rb', line 51

def upload(file_stream, file_name)
  response = Podio.client.connection.post do |req|
    req.options[:timeout] = 1200
    req.url "/file/v2/"
    req.headers['Content-Type'] = 'multipart/form-data'
    req.body = {:source => Faraday::UploadIO.new(file_stream, nil, nil), :filename => file_name}
  end

  member response.body
end

.upload_from_url(url) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/podio/models/file_attachment.rb', line 62

def upload_from_url(url)
  response = Podio.client.connection.post do |req|
    req.url "/file/from_url/"
    req.body = {:url => url}
  end

  member response.body
end

Instance Method Details

#api_friendly_ref_typeObject



37
38
39
# File 'lib/podio/models/file_attachment.rb', line 37

def api_friendly_ref_type
  'file'
end

#has_thumbnail?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/podio/models/file_attachment.rb', line 29

def has_thumbnail?
  self.thumbnail_link.present?
end

#image?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/podio/models/file_attachment.rb', line 33

def image?
  ['image/png', 'image/jpeg', 'image/gif', 'image/bmp'].include?(self.mimetype)
end

#raw_data(size = nil) ⇒ Object

Returns the raw bytes of a file for images pass the size used by podio in its urls e.g. files.podio.com/:file/medium



43
44
45
46
# File 'lib/podio/models/file_attachment.rb', line 43

def raw_data(size=nil)
  link = size ? "#{self.link}/#{size}" : self.link
  Podio.connection.get(link).body
end