Module: CouchrestAttachmentHelpers

Defined in:
lib/glue_envs/couchrest/couchrest_attachment_handler.rb

Overview

Performs manipulations ont file attachment structures and metadata

Class Method Summary collapse

Class Method Details

.escape_names_in_attachments(unesc_attachments) ⇒ Object

Escapes attachment names in a CouchDB compatible way



65
66
67
68
69
70
71
72
# File 'lib/glue_envs/couchrest/couchrest_attachment_handler.rb', line 65

def self.escape_names_in_attachments(unesc_attachments)
  escaped_attachments = {}
  unesc_attachments.each do |unesc_key, val|
    esc_key = TkEscape.escape(unesc_key)
    escaped_attachments[esc_key] = val
  end
  return escaped_attachments
end

.sort_attachment_data(attachments) ⇒ Object

Attachment data format: attachment_name => attachment info attachment info format: { ‘data’ => attachment data, ‘md’ => attachment metadata } attachment data is sorted into the data and metadata CouchDB attachments can handle natively and the additional metadata that CouchDB attachments do not handle (boo, hiss)

Usage Example:

CouchrestAttachmentHelpers.sort_attachment_data(attachments)
#=> { 'data_by_name' => { attachment_1 => raw_attachment_data1,
                        attachment_2 => raw_attachment_data2 }.
    'att_md_by_name' => { attachment_1 => CouchDB metadata fields1,
                          attachment_2 => CouchDB metadata fields2}
    'cust_md_by_name' => { attachment_1 => Custom metadata fields1,
                          attachment_2 => Custom metadata fields2}
   }


27
28
29
30
31
32
33
34
35
36
37
38
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/glue_envs/couchrest/couchrest_attachment_handler.rb', line 27

def self.sort_attachment_data(attachments)
  all_couch_attach_params = {}
  all_custom_attach_params = {}
  all_attach_data = {}
  attachments.each do |att_name, att_info|
    #att_info: 'data' => att data, 'md' => att metadata
    esc_att_name = TkEscape.escape(att_name)
    att_params = {}
    obj_params = {}
    attach_data = nil
    att_info.each do |info, info_value|
      if info == 'data'
        attach_data = info_value
      elsif info == 'md'
        #md holds all file metadata (both couch and custom)
         = self.(info_value)
        att_params = ['att_md']
        obj_params = ['cust_md']
      end
    end
    #Not completely sure converting content type to a symbol was the problem,
    # but it works now
    #Investigate further time permitting.
    #couchrest change forced this workaround
    att_params[:content_type] = att_params['content_type']
    att_params.delete('content_type')

    all_couch_attach_params[esc_att_name] = att_params
    all_custom_attach_params[esc_att_name] = obj_params 
    all_attach_data[esc_att_name] = attach_data
  end
  sorted =  {'data_by_name' => all_attach_data,
    'att_md_by_name' => all_couch_attach_params,
    'cust_md_by_name' => all_custom_attach_params}
  return sorted
end

.unescape_names_in_attachments(esc_attachments) ⇒ Object

Unescapes attachment names in a CouchDb compatible way



75
76
77
78
79
80
81
82
83
# File 'lib/glue_envs/couchrest/couchrest_attachment_handler.rb', line 75

def self.unescape_names_in_attachments(esc_attachments)
  unescaped_attachments = {}
  #esc_attachments = esc_attachments || []
  esc_attachments.each do |esc_key, val|
    unesc_key = CGI.unescape(esc_key)
    unescaped_attachments[unesc_key] = val
  end
  return unescaped_attachments
end