Class: Basecamp::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/basecamp.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, content) ⇒ Attachment

Returns a new instance of Attachment.



223
224
225
# File 'lib/basecamp.rb', line 223

def initialize(filename, content)
  @filename, @content = filename, content
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



215
216
217
# File 'lib/basecamp.rb', line 215

def content
  @content
end

#filenameObject

Returns the value of attribute filename.



215
216
217
# File 'lib/basecamp.rb', line 215

def filename
  @filename
end

#idObject

Returns the value of attribute id.



215
216
217
# File 'lib/basecamp.rb', line 215

def id
  @id
end

Class Method Details

.create(filename, content) ⇒ Object



217
218
219
220
221
# File 'lib/basecamp.rb', line 217

def self.create(filename, content)
  returning new(filename, content) do |attachment|
    attachment.save
  end
end

Instance Method Details

#attributesObject



227
228
229
# File 'lib/basecamp.rb', line 227

def attributes
  { :file => id, :original_filename => filename }
end

#inspectObject



235
236
237
# File 'lib/basecamp.rb', line 235

def inspect
  to_s
end

#saveObject



239
240
241
242
243
244
245
246
247
248
# File 'lib/basecamp.rb', line 239

def save
  response = Basecamp.connection.post('/upload', content, 'Content-Type' => 'application/octet-stream')

  if response.code == '200'
    self.id = Hash.from_xml(response.body)['upload']['id']
    true
  else
    raise "Could not save attachment: #{response.message} (#{response.code})"
  end
end

#to_xml(options = {}) ⇒ Object



231
232
233
# File 'lib/basecamp.rb', line 231

def to_xml(options = {})
  { :file => attributes }.to_xml(options)
end