Class: Mack::Notifier::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/mack-notifier/attachment.rb

Overview

Creates an attachment for a Mack::Notifier object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body = nil) ⇒ Attachment

Returns a new instance of Attachment.



11
12
13
14
15
16
17
# File 'lib/mack-notifier/attachment.rb', line 11

def initialize(body = nil)
  unless body.nil?
    self.add_file(body) if body.is_a?(String)
    self.add_io(body) if body.is_a?(IO)
    self.add_uploaded_file(body) if body.is_a?(Mack::Request::UploadedFile)
  end
end

Instance Attribute Details

#bodyObject

Returns a String representing the body of the attachment. This String is NOT encoded in anyway!



7
8
9
# File 'lib/mack-notifier/attachment.rb', line 7

def body
  @body
end

#file_nameObject

Returns the name of the attached file.



9
10
11
# File 'lib/mack-notifier/attachment.rb', line 9

def file_name
  @file_name
end

Instance Method Details

#add_file(file) ⇒ Object

Takes a path to a file, reads it in, and sets the file_name based on the path.



25
26
27
28
# File 'lib/mack-notifier/attachment.rb', line 25

def add_file(file)
  self.file_name = File.basename(file)
  self.body = File.read(file)
end

#add_io(io) ⇒ Object

Takes an IO object and sets the body. You’ll need to explicity set the file_name afterwards.



20
21
22
# File 'lib/mack-notifier/attachment.rb', line 20

def add_io(io)
  self.body = io.read
end

#add_uploaded_file(file) ⇒ Object

Takes a Mack::Request::UploadedFile file object, reads it in, and sets the file name.



31
32
33
34
# File 'lib/mack-notifier/attachment.rb', line 31

def add_uploaded_file(file)
  self.body = File.read(file.temp_file.path)
  self.file_name = file.file_name
end