Sha256: 70e2f5d5045b41b4ab0453179854bd2b923c7264a2dde01a76dc9a21f5711fdb

Contents?: true

Size: 1.62 KB

Versions: 9

Compression:

Stored size: 1.62 KB

Contents

module Mingle4r
  module API
    class Attachment
      module InstanceMethods
        # downloads the attachment. It an additional file path is given it saves it at the 
        # given path. The given path should be writable
        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 # download

        # alias for file_name
        def name
          file_name()
        end

        # so that active resource tries to find by proper id
        def id
          name()
        end

        # This method had to be overriden. 
        # normal active resource destroy doesn't work as mingle site for deleting attachments doesn't end with .xml.
        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
        alias_method :delete, :destroy
      end #module InstanceMethods

      extend Mingle4r::CommonClassMethods

    end # class Attachment
  end # class API
end # module Mingle4r

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mingle4r-0.5.0 lib/mingle4r/api/card/attachment.rb
mingle4r-0.4.8 lib/mingle4r/api/card/attachment.rb
mingle4r-0.4.7 lib/mingle4r/api/card/attachment.rb
mingle4r-0.4.6 lib/mingle4r/api/card/attachment.rb
mingle4r-0.4.5 lib/mingle4r/api/card/attachment.rb
mingle4r-0.4.3 lib/mingle4r/api/card/attachment.rb
mingle4r-0.4.2 lib/mingle4r/api/card/attachment.rb
mingle4r-0.4.1 lib/mingle4r/api/card/attachment.rb
mingle4r-0.4.0 lib/mingle4r/api/card/attachment.rb