Class: Viewpoint::EWS::FileAttachment

Inherits:
Attachment
  • Object
show all
Defined in:
lib/model/file_attachment.rb

Overview

This class represents a file attachment item. You can save this object to a file withthe #save_to_file method.

Instance Attribute Summary collapse

Attributes inherited from Attachment

#id

Attributes included from Model

#ews_methods, #ews_methods_undef

Instance Method Summary collapse

Constructor Details

#initialize(attachment_id) ⇒ FileAttachment

Returns a new instance of FileAttachment.

Parameters:

  • The unique ID for the attachment.



29
30
31
32
33
34
35
36
37
# File 'lib/model/file_attachment.rb', line 29

def initialize(attachment_id)
  @id = attachment_id
  conn = Viewpoint::EWS::EWS.instance
  resp = conn.ews.get_attachment([attachment_id])
  @file_name = resp.items.first[:file_attachment][:name][:text]

  # content in Base64
  @content = resp.items.first[:file_attachment][:content][:text]
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



27
28
29
# File 'lib/model/file_attachment.rb', line 27

def content
  @content
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



27
28
29
# File 'lib/model/file_attachment.rb', line 27

def file_name
  @file_name
end

Instance Method Details

#save_to_file(base_dir = nil, file_name = @file_name) ⇒ Object

Save this FileAttachment object to a file. By default it saves it to the original file’s name in the current working directory.

Parameters:

  • (defaults to: nil)

    the directory to save the file to. Pass nil if you do not want to specify a directory or you are passing a full path name to file_name

  • (defaults to: @file_name)

    The name of the file to save the content to. Leave this blank to use the default attachment name.



45
46
47
48
49
50
51
# File 'lib/model/file_attachment.rb', line 45

def save_to_file(base_dir = nil, file_name = @file_name)
  base_dir << '/' unless(base_dir.nil? or base_dir.end_with?('/'))
  File.open("#{base_dir}#{file_name}", 'w+') do |f|
    f.write(Base64.decode64(@content))
  end
  true
end