Class: RedHatSupportLib::Brokers::Attachment

Inherits:
Broker
  • Object
show all
Defined in:
lib/brokers/attachment.rb

Instance Attribute Summary

Attributes inherited from Broker

#connection

Instance Method Summary collapse

Constructor Details

#initialize(connection, comments_broker, attachments_config) ⇒ Attachment

Returns a new instance of Attachment.



8
9
10
11
12
# File 'lib/brokers/attachment.rb', line 8

def initialize(connection, comments_broker, attachments_config)
  super(connection)
  @comments_broker = comments_broker
  @attachments_config = attachments_config
end

Instance Method Details

#add(case_number, is_public, file_name, description) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/brokers/attachment.rb', line 43

def add(case_number, is_public, file_name, description)
  #puts "Sending attachment for case "+ case_number
  attachment_id = nil
  File.open(file_name) do |file|
    if file.size < @attachments_config[:max_http_size]
      headers = {:description => description}
      @connection.upload("/rs/cases/#{case_number}/attachments",
                         file, headers) do |code, headers|
        if code == 201
          location = headers[:location]
          attachment_id = get_id(location)
        else
          #What to return here?
          raise "Attachment failed " + code
        end
      end
    else
      ftp_connection = RedHatSupportLib::Network::FtpConnection.new(@attachments_config[:ftp_host])
      ftp_connection.upload(file_name,
                            @attachments_config[:ftp_remote_dir])
      comment = StringIO.new
      comment << "The file #{file_name} exceeds the byte limit to attach a file to a case;"
      comment << " Therefore, the file was uploaded to"
      comment << " #{@attachments_config[:ftp_host]} as #{File.basename(file_name)}"
      @comments_broker.add(comment.string, case_number, false, is_public)
      #TODO what to return here?
    end
  end
  attachment_id
end

#delete(case_number, attachment_uuid) ⇒ Object



37
38
39
40
41
# File 'lib/brokers/attachment.rb', line 37

def delete(case_number, attachment_uuid)

  uri = "/rs/cases/#{case_number}/attachments/#{attachment_uuid}";
  result = @connection.delete(uri)
end

#get(case_number, attachment_uuid, file_name, dest_dir) ⇒ Object



32
33
34
35
# File 'lib/brokers/attachment.rb', line 32

def get(case_number, attachment_uuid, file_name, dest_dir)
  #TODO

end

#get_id(uri) ⇒ Object



74
75
76
77
# File 'lib/brokers/attachment.rb', line 74

def get_id(uri)
  parts = uri.split("/")
  parts.pop
end

#list(case_number, start_date, end_date, filter = []) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/brokers/attachment.rb', line 14

def list(case_number, start_date, end_date, filter=[])

  uri = "/rs/cases/#{case_number}/attachments"
  param = ''
  if start_date
    param = "?startDate=#{start_date}"
  end
  if end_date
    if start_date
      param = param + "&endDate=#{end_date}"
    else
      param = param + "?endDate=#{end_date}"
    end
  end
  result = @connection.get(uri+param, {:accept => :json})
end