Class: Resources::Attachment

Inherits:
Base
  • Object
show all
Defined in:
lib/minglr/resources/attachment.rb

Class Method Summary collapse

Methods inherited from Base

print_collection, warn

Class Method Details

.attach(card_number, file_name, username, password) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/minglr/resources/attachment.rb', line 28

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

.configureObject



7
8
9
# File 'lib/minglr/resources/attachment.rb', line 7

def self.configure
  self.prefix += "cards/:card_number/"
end

.curl(command) ⇒ Object



11
12
13
# File 'lib/minglr/resources/attachment.rb', line 11

def self.curl(command)
  `#{command}`
end

.fetch(card_number, username, password) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/minglr/resources/attachment.rb', line 15

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