Module: Cloudant::Attachment

Included in:
API
Defined in:
lib/cloudant/attachment.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.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

Instance Method Details

#create_attachment(args) ⇒ Object Also known as: update_attachment

The Attachment Module contains methods to interact with document attachments.

Add an attachment to an existing document. Accepts the document id, the MIME type (ie: image/jpg), and the name for the attachment a rev field is also required (and suggested), but if not, the document is searched and rev extracted



8
9
10
11
12
13
14
15
16
# File 'lib/cloudant/attachment.rb', line 8

def create_attachment(args)
  if args[:id]
    args[:rev] = get_current_rev(args[:id]) unless args[:rev]
    attachment = Cloudant::Attachment.make_attachment(args)
    query_str  = build_attachment_query(args)

    @conn.query({url_path: query_str, opts: attachment, method: :put})
  end
end

#delete_attachment(args) ⇒ Object

Delete a document’s attachment. Accepts a document id, the rev in question, and the name of an attachment associated with that doc.



29
30
31
32
33
# File 'lib/cloudant/attachment.rb', line 29

def delete_attachment(args)
  query_str = build_attachment_query(args)
  
  @conn.query({url_path: query_str, method: :delete})
end

#read_attachment(args) ⇒ Object

Read a document’s attachments. Accepts a document id and the name of an attachment associated with that doc.



21
22
23
24
25
# File 'lib/cloudant/attachment.rb', line 21

def read_attachment(args)
  query_str = build_attachment_query(args)
  
  @conn.query({url_path: query_str, method: :get})
end