Class: BasecampAPI::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/basecamp/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.



375
376
377
# File 'lib/basecamp/basecamp.rb', line 375

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

Instance Attribute Details

#contentObject

Returns the value of attribute content.



367
368
369
# File 'lib/basecamp/basecamp.rb', line 367

def content
  @content
end

#filenameObject

Returns the value of attribute filename.



367
368
369
# File 'lib/basecamp/basecamp.rb', line 367

def filename
  @filename
end

#idObject

Returns the value of attribute id.



367
368
369
# File 'lib/basecamp/basecamp.rb', line 367

def id
  @id
end

Class Method Details

.create(filename, content) ⇒ Object



369
370
371
372
373
# File 'lib/basecamp/basecamp.rb', line 369

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

Instance Method Details

#attributesObject



379
380
381
# File 'lib/basecamp/basecamp.rb', line 379

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

#inspectObject



387
388
389
# File 'lib/basecamp/basecamp.rb', line 387

def inspect
  to_s
end

#saveObject



391
392
393
394
395
396
397
398
399
400
# File 'lib/basecamp/basecamp.rb', line 391

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



383
384
385
# File 'lib/basecamp/basecamp.rb', line 383

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