class SocketLabs::InjectionApi::Message::Attachment

Represents a custom header as a name-value pair. Example:

attachment1 = Attachment(file_path="./bus.png")

attachment2 = Attachment("bus", "image/png", "../bus.png")

attachment3 = Attachment("bus", "image/png", content=bytes())
attachment3.add_custom_header("name1", "value1")
attachment3.add_custom_header("name2", "value2")

Attributes

content[RW]

BASE64 encoded string containing the contents of an attachment.

content_id[RW]

The contentId for your attachment, used if you need to reference the attachment in your email content.

custom_headers[RW]

A list of custom headers for the attachment.

file_path[RW]

The local file path for your attachment, used to stream the file in.

mime_type[RW]

The MIME type of the attachment.

name[RW]

Name of attachment (displayed in email clients).

Public Class Methods

new(arguments) click to toggle source

Initializes a new instance of the CustomHeader class @param [merge] arguments - accepted arguments file_path, name, mime_type, content_id, custom_headers, content

# File lib/socketlabs/injectionapi/message/attachment.rb, line 36
def initialize(arguments)

  @custom_headers = Array.new

  unless arguments[:file_path].nil? || arguments[:file_path].empty?
    readfile(arguments[:file_path])
  end

  unless arguments[:name].nil? || arguments[:name].empty?
    @name = arguments[:name]
  end

  unless arguments[:mime_type].nil? || arguments[:mime_type].empty?
    @mime_type = arguments[:mime_type]
  end

  @content_id = arguments[:content_id]

  @custom_headers = []
  unless arguments[:custom_headers].nil? || arguments[:custom_headers].empty?
    @custom_headers = arguments[:custom_headers]
  end

  unless arguments[:content].nil? || arguments[:content].empty?
    @content = arguments[:content]
  end

end

Public Instance Methods

add_custom_header(name, value) click to toggle source

Add a CustomHeader to the attachment @param [String/CustomHeader] name @param [String] value

# File lib/socketlabs/injectionapi/message/attachment.rb, line 125
def add_custom_header(name, value)

  if name.kind_of? CustomHeader
    @custom_headers.push(name)

  elsif name.kind_of? String
    @custom_headers.push(CustomHeader.new(name, value))

  end
end
get_mime_type_from_ext(extension) click to toggle source

Takes a file extension, minus the '.', and returns the corresponding MimeType for the given extension. @param [String] extension @return [String] mime type

# File lib/socketlabs/injectionapi/message/attachment.rb, line 82
def get_mime_type_from_ext(extension)
  extension[0]=''
  types = [
    { :ext => "txt", :mime_type => "text/plain" },
    { :ext => "ini", :mime_type => "text/plain" },
    { :ext => "sln", :mime_type => "text/plain" },
    { :ext => "cs", :mime_type => "text/plain" },
    { :ext => "js", :mime_type => "text/plain" },
    { :ext => "config", :mime_type => "text/plain" },
    { :ext => "vb", :mime_type => "text/plain" },
    { :ext => :"jpg", :mime_type => "image/jpeg" },
    { :ext => "jpeg", :mime_type => "image/jpeg" },
    { :ext => "bmp", :mime_type => "image/bmp" },
    { :ext => "csv", :mime_type => "text/csv" },
    { :ext => "doc", :mime_type => "application/msword" },
    { :ext => "docx", :mime_type => "application/vnd.openxmlformats-officedocument.wordprocessingml.document" },
    { :ext => "gif", :mime_type => "image/gif" },
    { :ext => "html", :mime_type => "text/html" },
    { :ext => "pdf", :mime_type => "application/pdf" },
    { :ext => "png", :mime_type => "image/png" },
    { :ext => "ppt", :mime_type => "application/vnd.ms-powerpoint" },
    { :ext => "pptx", :mime_type => "application/vnd.openxmlformats-officedocument.presentationml.presentation" },
    { :ext => "xls", :mime_type => "application/vnd.ms-excel" },
    { :ext => "xlsx", :mime_type => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" },
    { :ext => "xml", :mime_type => "application/xml" },
    { :ext => "zip", :mime_type => "application/x-zip-compressed" },
    { :ext => "wav", :mime_type => "audio/wav" },
    { :ext => "eml", :mime_type => "message/rfc822" },
    { :ext => "mp3", :mime_type => "audio/mpeg" },
    { :ext => "mp4", :mime_type => "video/mp4" },
    { :ext => "mov", :mime_type => "video/quicktime" }
  ]
  mime = types.find {|x| x[:ext] == extension}

    unless mime.nil? || mime.empty?
    "application/octet-stream"
    end
    mime[:mime_type]
end
readfile(file_path) click to toggle source

Read the specified file and get a str containing the resulting binary data. @param [String] file_path

# File lib/socketlabs/injectionapi/message/attachment.rb, line 67
def readfile(file_path)

  @file_path = file_path
  @name = File.basename(file_path)
  @mime_type = get_mime_type_from_ext(File.extname(file_path))

  file = File.open(file_path, "rb")
  encoded_string = Base64.encode64(file.read)
  @content = encoded_string

end
to_s() click to toggle source

Represents the Attachment as a String @return [String]

# File lib/socketlabs/injectionapi/message/attachment.rb, line 138
def to_s
    "#{@name}, #{@mime_type}"
end