Class: SocketLabs::InjectionApi::Message::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/socketlabs/injectionapi/message/attachment.rb

Overview

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")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ Attachment

Initializes a new instance of the CustomHeader class



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
63
# 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

Instance Attribute Details

#contentObject

BASE64 encoded string containing the contents of an attachment.



23
24
25
# File 'lib/socketlabs/injectionapi/message/attachment.rb', line 23

def content
  @content
end

#content_idObject

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



31
32
33
# File 'lib/socketlabs/injectionapi/message/attachment.rb', line 31

def content_id
  @content_id
end

#custom_headersObject

A list of custom headers for the attachment.



27
28
29
# File 'lib/socketlabs/injectionapi/message/attachment.rb', line 27

def custom_headers
  @custom_headers
end

#file_pathObject

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



29
30
31
# File 'lib/socketlabs/injectionapi/message/attachment.rb', line 29

def file_path
  @file_path
end

#mime_typeObject

The MIME type of the attachment.



25
26
27
# File 'lib/socketlabs/injectionapi/message/attachment.rb', line 25

def mime_type
  @mime_type
end

#nameObject

Name of attachment (displayed in email clients).



21
22
23
# File 'lib/socketlabs/injectionapi/message/attachment.rb', line 21

def name
  @name
end

Instance Method Details

#add_custom_header(name, value) ⇒ Object

Add a CustomHeader to the attachment



125
126
127
128
129
130
131
132
133
134
# 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) ⇒ String

Takes a file extension, minus the ‘.’, and returns the corresponding MimeType for the given extension.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# 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) ⇒ Object

Read the specified file and get a str containing the resulting binary data.



67
68
69
70
71
72
73
74
75
76
77
# 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_sString

Represents the Attachment as a String



138
139
140
# File 'lib/socketlabs/injectionapi/message/attachment.rb', line 138

def to_s
    "#{@name}, #{@mime_type}"
end