Method: Cloudant::Attachment.make_attachment

Defined in:
lib/cloudant/attachment.rb

.make_attachment(args) ⇒ Object

Accepts a Hash including :doc => the name of the doc to which the attachment will be attached, file_name, the name to be given to the attachment, the doc’s content type, and the attachment’s file type. Returns attachment to be uploaded



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cloudant/attachment.rb', line 39

def self.make_attachment(args)
  doc_name  = args[:id]
  file_name = args[:name]
  file_type = args[:type]
  file_path = args[:path]

  attachment = {
    "_id" => doc_name,
    "_attachments" => {
      file_name => {
        "content_type" => file_type
      }
    }
  }

  if File.exists?(file_path)
    data = File.open(file_path,'rb').read
    attachment["_attachments"][file_name]["data"] = data
  else
    raise Errno::ENOENT.new('file does not exist')
  end

  attachment
end