Sha256: d1502726ebfa51de1db8057d97f3831cfd68dbbebe0df82406b71651827e4282

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

begin
  require 'httpclient'
rescue LoadError
  puts "httpclient is not available. In order to run minglr, you must: sudo gem install cucumber"
  exit 1
end

module Resources
  
  class Attachment < Base
  
    def self.configure
      self.prefix += "cards/:card_number/"
    end
    
    def self.curl(command)
      `#{command}`
    end
    
    def self.fetch(card_number, username, password)
      if card_to_update = Card.find(card_number)
        attachments = find(:all, :params => { :card_number => card_number })
        attachments.each do |attachment|
          url = self.site + attachment.url
          url.userinfo = nil, nil
          puts "Downloading #{url.to_s}:"
          command = "curl --insecure --progress-bar --output #{attachment.file_name} --user #{username}:#{password} #{url}"
          curl(command)
        end
      end
    end
    
    def self.attach(card_number, file_name, username, password)
      if card_to_update = Card.find(card_number)
        url = (site.to_s.gsub(/#{site.path}$/, '')) + collection_path(:card_number => card_number)
        if File.exist?(file_name)
          File.open(file_name) do |file|
            body = { 'file' => file, "filename" => file_name }
            client = HTTPClient.new
            client.set_auth(nil, username, password)
            response = client.post(url, body)
            if response.status_code == 201
              puts "File '#{file_name}' attached to card #{card_number}"
            else
              puts "Error attaching file '#{file_name}' to card #{card_number} (Got back HTTP code #{response.status_code})"
            end
          end
        else
          warn "Unable to open file '#{file_name}'"
        end
      end
    end
  
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
schubert-minglr-1.3.1 lib/minglr/resources/attachment.rb