Class: Basecamp::Attachment

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



10
11
12
# File 'lib/basecamp/resources/attachment.rb', line 10

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

Instance Attribute Details

#category_idObject

Returns the value of attribute category_id.



2
3
4
# File 'lib/basecamp/resources/attachment.rb', line 2

def category_id
  @category_id
end

#contentObject

Returns the value of attribute content.



2
3
4
# File 'lib/basecamp/resources/attachment.rb', line 2

def content
  @content
end

#filenameObject

Returns the value of attribute filename.



2
3
4
# File 'lib/basecamp/resources/attachment.rb', line 2

def filename
  @filename
end

#idObject

Returns the value of attribute id.



2
3
4
# File 'lib/basecamp/resources/attachment.rb', line 2

def id
  @id
end

Class Method Details

.create(filename, content) ⇒ Object



4
5
6
7
8
# File 'lib/basecamp/resources/attachment.rb', line 4

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

Instance Method Details

#attributesObject



14
15
16
# File 'lib/basecamp/resources/attachment.rb', line 14

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

#inspectObject



22
23
24
# File 'lib/basecamp/resources/attachment.rb', line 22

def inspect
  to_s
end

#saveObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/basecamp/resources/attachment.rb', line 26

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



18
19
20
# File 'lib/basecamp/resources/attachment.rb', line 18

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