Module: Mingle4r::Card::Attachment::InstanceMethods

Defined in:
lib/mingle4r/card/attachment.rb

Instance Method Summary collapse

Instance Method Details

#destroyObject Also known as: delete

This method had to be overriden. normal active resource destroy doesn’t work as mingle site for deleting attachments doesn’t end with .xml.



35
36
37
38
39
# File 'lib/mingle4r/card/attachment.rb', line 35

def destroy
  connection = self.send(:connection)
  # deletes the attachment by removing .xml at the end
  connection.delete(self.send(:element_path).gsub(/\.xml\z/, ''))
end

#download(file_name = nil) ⇒ Object

downloads the attachment. It an additional file path is given it saves it at the given path. The given path should be writable



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mingle4r/card/attachment.rb', line 7

def download(file_name = nil)
  collection_uri = self.class.site
  rel_down_url = self.url
  base_url = "#{collection_uri.scheme}://#{collection_uri.host}:#{collection_uri.port}/"
  down_uri = URI.join(base_url, rel_down_url)
  req = Net::HTTP::Get.new(down_uri.path)
  req.basic_auth self.class.user, self.class.password
  begin
    res = Net::HTTP.start(down_uri.host, down_uri.port) { |http| http.request(req) }
    file_name ||= self.file_name()
    File.open(file_name, 'w') { |f| f.print(res.body) }
  rescue Exception => e
    e.message
  end
end

#idObject

so that active resource tries to find by proper id



29
30
31
# File 'lib/mingle4r/card/attachment.rb', line 29

def id
  name()
end

#nameObject

alias for file_name



24
25
26
# File 'lib/mingle4r/card/attachment.rb', line 24

def name
  file_name()
end