socketlabs-injectionapi 0.0.1.pre.Dev
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +50 -0
- data/.idea/inspectionProfiles/Project_Default.xml +7 -0
- data/.idea/misc.xml +7 -0
- data/.idea/modules.xml +8 -0
- data/.idea/socketlabs-ruby.iml +12 -0
- data/.idea/vcs.xml +7 -0
- data/.idea/workspace.xml +452 -0
- data/LICENSE.MD +21 -0
- data/README.MD +212 -0
- data/gemfile +3 -0
- data/gemfile.lock +17 -0
- data/lib/socketlabs/injectionapi/address_result.rb +41 -0
- data/lib/socketlabs/injectionapi/core/http_request.rb +135 -0
- data/lib/socketlabs/injectionapi/core/http_response.rb +27 -0
- data/lib/socketlabs/injectionapi/core/injection_request_factory.rb +300 -0
- data/lib/socketlabs/injectionapi/core/injection_response_parser.rb +130 -0
- data/lib/socketlabs/injectionapi/core/send_validator.rb +387 -0
- data/lib/socketlabs/injectionapi/core/serialization/address_json.rb +48 -0
- data/lib/socketlabs/injectionapi/core/serialization/attachment_json.rb +60 -0
- data/lib/socketlabs/injectionapi/core/serialization/custom_header_json.rb +40 -0
- data/lib/socketlabs/injectionapi/core/serialization/injection_request.rb +56 -0
- data/lib/socketlabs/injectionapi/core/serialization/injection_response_dto.rb +77 -0
- data/lib/socketlabs/injectionapi/core/serialization/merge_data_json.rb +103 -0
- data/lib/socketlabs/injectionapi/core/serialization/merge_field_json.rb +40 -0
- data/lib/socketlabs/injectionapi/core/serialization/message_json.rb +250 -0
- data/lib/socketlabs/injectionapi/core/serialization/message_result_dto.rb +66 -0
- data/lib/socketlabs/injectionapi/core/string_extension.rb +74 -0
- data/lib/socketlabs/injectionapi/message/attachment.rb +146 -0
- data/lib/socketlabs/injectionapi/message/basic_message.rb +136 -0
- data/lib/socketlabs/injectionapi/message/bulk_message.rb +103 -0
- data/lib/socketlabs/injectionapi/message/bulk_recipient.rb +87 -0
- data/lib/socketlabs/injectionapi/message/custom_header.rb +53 -0
- data/lib/socketlabs/injectionapi/message/email_address.rb +52 -0
- data/lib/socketlabs/injectionapi/message/merge_data.rb +50 -0
- data/lib/socketlabs/injectionapi/message/message_base.rb +167 -0
- data/lib/socketlabs/injectionapi/proxy.rb +29 -0
- data/lib/socketlabs/injectionapi/send_response.rb +63 -0
- data/lib/socketlabs/injectionapi/send_result.rb +318 -0
- data/lib/socketlabs/injectionapi/socketlabsclient.rb +123 -0
- data/lib/socketlabs/version.rb +5 -0
- data/lib/socketlabs-injectionapi.rb +27 -0
- data/socketlabs-injectionapi.gemspec +25 -0
- metadata +85 -0
@@ -0,0 +1,103 @@
|
|
1
|
+
module SocketLabs
|
2
|
+
module InjectionApi
|
3
|
+
module Core
|
4
|
+
module Serialization
|
5
|
+
|
6
|
+
class MergeDataJson
|
7
|
+
|
8
|
+
# @param [Array] per_message
|
9
|
+
# @param [Array] global_merge_data
|
10
|
+
def initialize(
|
11
|
+
per_message = nil,
|
12
|
+
global_merge_data = nil
|
13
|
+
)
|
14
|
+
self.per_message_merge_data = per_message
|
15
|
+
self.global_merge_data = global_merge_data
|
16
|
+
end
|
17
|
+
|
18
|
+
# Get merge field data for each message.
|
19
|
+
# @return [Array]
|
20
|
+
def per_message_merge_data
|
21
|
+
@per_message_merge_data
|
22
|
+
end
|
23
|
+
|
24
|
+
# Set merge field data for each message.
|
25
|
+
# @param [Array] value
|
26
|
+
def per_message_merge_data=(value)
|
27
|
+
@per_message_merge_data = Array.new
|
28
|
+
|
29
|
+
unless value.nil?
|
30
|
+
value.each do |v1|
|
31
|
+
l1 = Array.new
|
32
|
+
v1.each do |v2|
|
33
|
+
if v2.instance_of? MergeFieldJson
|
34
|
+
l1.push(v2)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
@per_message_merge_data.push(l1)
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
# Get merge field data for all messages in the request
|
43
|
+
# @return [Array]
|
44
|
+
def global_merge_data
|
45
|
+
@global_merge_data
|
46
|
+
end
|
47
|
+
|
48
|
+
# Set merge field data for all messages in the request
|
49
|
+
# @param [Array] value
|
50
|
+
def global_merge_data=(value)
|
51
|
+
@global_merge_data = Array.new
|
52
|
+
|
53
|
+
unless value.nil?
|
54
|
+
value.each do |v1|
|
55
|
+
if v1.instance_of? MergeFieldJson
|
56
|
+
@global_merge_data.push(v1)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
# build json hash for MergeDataJson
|
63
|
+
# @return [hash]
|
64
|
+
def to_hash
|
65
|
+
|
66
|
+
json = {}
|
67
|
+
|
68
|
+
if @global_merge_data.length > 0
|
69
|
+
e = Array.new
|
70
|
+
@global_merge_data.each do |value|
|
71
|
+
e.push(value.to_hash)
|
72
|
+
end
|
73
|
+
json[:global] = e
|
74
|
+
end
|
75
|
+
|
76
|
+
if @per_message_merge_data.length > 0
|
77
|
+
e = Array.new
|
78
|
+
@per_message_merge_data.each do |message|
|
79
|
+
m = Array.new
|
80
|
+
message.each do |value|
|
81
|
+
m.push(value.to_hash)
|
82
|
+
end
|
83
|
+
e.push(m)
|
84
|
+
end
|
85
|
+
json[:perMessage] = e
|
86
|
+
end
|
87
|
+
|
88
|
+
json
|
89
|
+
end
|
90
|
+
|
91
|
+
def empty
|
92
|
+
@global_merge_data.length <= 0 && @per_message_merge_data.length <= 0
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module SocketLabs
|
2
|
+
module InjectionApi
|
3
|
+
module Core
|
4
|
+
module Serialization
|
5
|
+
|
6
|
+
# Represents a merge field as a field and value pair.
|
7
|
+
# To be serialized into JSON string before sending to the Injection Api.
|
8
|
+
class MergeFieldJson
|
9
|
+
|
10
|
+
# The field of your merge field.
|
11
|
+
attr_accessor :field
|
12
|
+
# The merge field value.
|
13
|
+
attr_accessor :value
|
14
|
+
|
15
|
+
# Initializes a new instance of the MergeFieldJson class
|
16
|
+
# @param [String] field
|
17
|
+
# @param [String] value
|
18
|
+
def initialize(
|
19
|
+
field = nil,
|
20
|
+
value = nil
|
21
|
+
)
|
22
|
+
@field = field
|
23
|
+
@value = value
|
24
|
+
end
|
25
|
+
|
26
|
+
# build json hash for MergeFieldJson
|
27
|
+
# @return [hash]
|
28
|
+
def to_hash
|
29
|
+
{
|
30
|
+
:field => @field,
|
31
|
+
:value => @value
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,250 @@
|
|
1
|
+
module SocketLabs
|
2
|
+
module InjectionApi
|
3
|
+
module Core
|
4
|
+
module Serialization
|
5
|
+
|
6
|
+
class MessageJson
|
7
|
+
|
8
|
+
# the message subject.
|
9
|
+
attr_accessor :subject
|
10
|
+
# the plain text portion of the message body.
|
11
|
+
attr_accessor :plain_text_body
|
12
|
+
# the HTML portion of the message body.
|
13
|
+
attr_accessor :html_body
|
14
|
+
# the api template.
|
15
|
+
attr_accessor :api_template
|
16
|
+
# the custom mailing id.
|
17
|
+
attr_accessor :mailing_id
|
18
|
+
# the custom message id.
|
19
|
+
attr_accessor :message_id
|
20
|
+
# the optional character set. Default is UTF-8
|
21
|
+
attr_accessor :charset
|
22
|
+
# the from email address.
|
23
|
+
attr_accessor :from_email_address
|
24
|
+
# the optional reply to email address.
|
25
|
+
attr_accessor :reply_to
|
26
|
+
# the the MergeDataJson for the message
|
27
|
+
attr_accessor :merge_data
|
28
|
+
|
29
|
+
|
30
|
+
def initialize
|
31
|
+
|
32
|
+
@subject = nil
|
33
|
+
@plain_text_body = nil
|
34
|
+
@html_body = nil
|
35
|
+
@api_template = nil
|
36
|
+
@mailing_id = nil
|
37
|
+
@message_id = nil
|
38
|
+
@charset = nil
|
39
|
+
@from_email_address = nil
|
40
|
+
@reply_to = nil
|
41
|
+
@merge_data = MergeDataJson.new
|
42
|
+
@attachments = Array.new
|
43
|
+
@custom_headers = Array.new
|
44
|
+
@to_email_address = Array.new
|
45
|
+
@cc_email_address = Array.new
|
46
|
+
@bcc_email_address = Array.new
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
# Get the list of attachments.
|
51
|
+
# @return [Array]
|
52
|
+
def attachments
|
53
|
+
@attachments
|
54
|
+
end
|
55
|
+
# Set the list of AttachmentJson.
|
56
|
+
# @param [Array] value
|
57
|
+
def attachments=(value)
|
58
|
+
@attachments = Array.new
|
59
|
+
unless value.nil? || value.empty?
|
60
|
+
value.each do |v1|
|
61
|
+
if v1.instance_of? AttachmentJson
|
62
|
+
@attachments.push(v1)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# Add an AttachmentJson to the attachments list.
|
69
|
+
# @param [AttachmentJson] value
|
70
|
+
def add_attachments(value)
|
71
|
+
if value.instance_of? AttachmentJson
|
72
|
+
@attachments.push(value)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
#custom_headers
|
77
|
+
# Get the list of CustomHeaderJson.
|
78
|
+
# @return [Array]
|
79
|
+
def custom_headers
|
80
|
+
@custom_headers
|
81
|
+
end
|
82
|
+
# Set the list of CustomHeaderJson.
|
83
|
+
# @param [Array] value
|
84
|
+
def custom_headers=(value)
|
85
|
+
@custom_headers = Array.new
|
86
|
+
unless value.nil? || value.empty?
|
87
|
+
value.each do |v1|
|
88
|
+
if v1.instance_of? CustomHeaderJson
|
89
|
+
@custom_headers.push(v1)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# Add a CustomHeaderJson to the custom header list
|
96
|
+
# @param [CustomHeaderJson] value
|
97
|
+
def add_custom_header(value)
|
98
|
+
if value.instance_of? CustomHeaderJson
|
99
|
+
@custom_headers.push(value)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
# Get the To email address list
|
104
|
+
# @return [Array]
|
105
|
+
def to_email_address
|
106
|
+
@to_email_address
|
107
|
+
end
|
108
|
+
# Set the To email address list
|
109
|
+
# @param [Array] value
|
110
|
+
def to_email_address=(value)
|
111
|
+
@to_email_address = Array.new
|
112
|
+
unless value.nil? || value.empty?
|
113
|
+
value.each do |v1|
|
114
|
+
if v1.instance_of? AddressJson
|
115
|
+
@to_email_address.push(v1)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
# Get the CC email address list
|
122
|
+
# @return [Array]
|
123
|
+
def cc_email_address
|
124
|
+
@cc_email_address
|
125
|
+
end
|
126
|
+
# Set the CC email address list
|
127
|
+
# @param [Array] value
|
128
|
+
def cc_email_address=(value)
|
129
|
+
@cc_email_address = Array.new
|
130
|
+
unless value.nil? || value.empty?
|
131
|
+
value.each do |v1|
|
132
|
+
if v1.instance_of? AddressJson
|
133
|
+
@cc_email_address.push(v1)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
# Get the BCC email address list
|
140
|
+
# @return [Array]
|
141
|
+
def bcc_email_address
|
142
|
+
@bcc_email_address
|
143
|
+
end
|
144
|
+
# Set the BCC email address list
|
145
|
+
# @param [Array] value
|
146
|
+
def bcc_email_address=(value)
|
147
|
+
@bcc_email_address = Array.new
|
148
|
+
unless value.nil? || value.empty?
|
149
|
+
value.each do |v1|
|
150
|
+
if v1.instance_of? AddressJson
|
151
|
+
@bcc_email_address.push(v1)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
# build json hash for MessageJson
|
158
|
+
# @return [hash]
|
159
|
+
def to_hash
|
160
|
+
|
161
|
+
json = {
|
162
|
+
:from => @from_email_address.to_hash
|
163
|
+
}
|
164
|
+
|
165
|
+
unless @subject.nil? || @subject.empty?
|
166
|
+
json[:subject] = @subject
|
167
|
+
end
|
168
|
+
|
169
|
+
unless @html_body.nil? || @html_body.empty?
|
170
|
+
json[:htmlBody] = @html_body
|
171
|
+
end
|
172
|
+
|
173
|
+
unless @plain_text_body.nil? || @plain_text_body.empty?
|
174
|
+
json[:textBody] = @plain_text_body
|
175
|
+
end
|
176
|
+
|
177
|
+
unless @api_template.nil?
|
178
|
+
json[:apiTemplate] = @api_template
|
179
|
+
end
|
180
|
+
|
181
|
+
unless @mailing_id.nil? || @mailing_id.empty?
|
182
|
+
json[:mailingId] = @mailing_id
|
183
|
+
end
|
184
|
+
|
185
|
+
unless @message_id.nil? || @message_id.empty?
|
186
|
+
json[:messageId] = @message_id
|
187
|
+
end
|
188
|
+
|
189
|
+
unless @reply_to.nil?
|
190
|
+
json[:replyTo] = @reply_to.to_hash
|
191
|
+
end
|
192
|
+
|
193
|
+
unless @charset.nil? || @charset.empty?
|
194
|
+
json[:charSet] = @charset
|
195
|
+
end
|
196
|
+
|
197
|
+
unless @to_email_address.nil? || @to_email_address.length == 0
|
198
|
+
e = Array.new
|
199
|
+
@to_email_address.each do |value|
|
200
|
+
e.push(value.to_hash)
|
201
|
+
end
|
202
|
+
json[:to] = e
|
203
|
+
end
|
204
|
+
|
205
|
+
unless @cc_email_address.nil? || @cc_email_address.length == 0
|
206
|
+
e = Array.new
|
207
|
+
@cc_email_address.each do |value|
|
208
|
+
e.push(value.to_hash)
|
209
|
+
end
|
210
|
+
json[:cc] = e
|
211
|
+
end
|
212
|
+
|
213
|
+
unless @bcc_email_address.nil? || @bcc_email_address.length == 0
|
214
|
+
e = Array.new
|
215
|
+
@bcc_email_address.each do |value|
|
216
|
+
e.push(value.to_hash)
|
217
|
+
end
|
218
|
+
json[:bcc] = e
|
219
|
+
end
|
220
|
+
|
221
|
+
unless @custom_headers.nil? || @custom_headers.length == 0
|
222
|
+
e = Array.new
|
223
|
+
@custom_headers.each do |value|
|
224
|
+
e.push(value.to_hash)
|
225
|
+
end
|
226
|
+
json[:customHeaders] = e
|
227
|
+
end
|
228
|
+
|
229
|
+
unless @attachments.nil? || @attachments.length == 0
|
230
|
+
e = Array.new
|
231
|
+
@attachments.each do |value|
|
232
|
+
e.push(value.to_hash)
|
233
|
+
end
|
234
|
+
json[:attachments] = e
|
235
|
+
end
|
236
|
+
|
237
|
+
unless @merge_data.nil? || @merge_data.empty
|
238
|
+
json[:mergeData] = @merge_data.to_hash
|
239
|
+
end
|
240
|
+
|
241
|
+
json
|
242
|
+
|
243
|
+
end
|
244
|
+
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require_relative '../../address_result.rb'
|
2
|
+
|
3
|
+
module SocketLabs
|
4
|
+
module InjectionApi
|
5
|
+
module Core
|
6
|
+
module Serialization
|
7
|
+
|
8
|
+
class MessageResultDto
|
9
|
+
include SocketLabs::InjectionApi
|
10
|
+
|
11
|
+
attr_accessor :index
|
12
|
+
attr_accessor :error_code
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@index = nil
|
16
|
+
@error_code = nil
|
17
|
+
@address_results = Array.new
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
# Get the List of AddressResult objects
|
22
|
+
# @return [Array]
|
23
|
+
def address_results
|
24
|
+
@address_results
|
25
|
+
end
|
26
|
+
|
27
|
+
# Set the List of AddressResult objects
|
28
|
+
# @param [Array] value
|
29
|
+
def address_results=(value)
|
30
|
+
@address_results = Array.new
|
31
|
+
|
32
|
+
unless value.nil?
|
33
|
+
|
34
|
+
if value.instance_of? Array
|
35
|
+
value.each do |v1|
|
36
|
+
if v1.instance_of? AddressResult
|
37
|
+
@address_results.push(v1)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
def to_hash
|
47
|
+
json = {
|
48
|
+
:ErrorCode => @error_code,
|
49
|
+
:Index => @index
|
50
|
+
}
|
51
|
+
if @address_results.length > 0
|
52
|
+
e = Array.new
|
53
|
+
@address_results.each do |value|
|
54
|
+
e.push(value.to_hash)
|
55
|
+
end
|
56
|
+
|
57
|
+
json[:AddressResult] = e
|
58
|
+
end
|
59
|
+
json
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module SocketLabs
|
2
|
+
module InjectionApi
|
3
|
+
module Core
|
4
|
+
|
5
|
+
class StringExtension
|
6
|
+
|
7
|
+
private
|
8
|
+
def strip_or_self!(str)
|
9
|
+
str.strip! || str if str
|
10
|
+
end
|
11
|
+
|
12
|
+
def value_empty(value)
|
13
|
+
value.nil? || value.empty?
|
14
|
+
end
|
15
|
+
|
16
|
+
def has_invalid_parts(value)
|
17
|
+
value.count('@') != 1
|
18
|
+
end
|
19
|
+
|
20
|
+
def is_part_empty(value)
|
21
|
+
|
22
|
+
unless value_empty(value)
|
23
|
+
true
|
24
|
+
end
|
25
|
+
|
26
|
+
part = strip_or_self!(value)
|
27
|
+
|
28
|
+
unless value_empty(part)
|
29
|
+
true
|
30
|
+
end
|
31
|
+
|
32
|
+
part.length <= 0
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
def has_invalid_characters(value)
|
37
|
+
[',', ' ', ';', 191.chr].each do |x|
|
38
|
+
if value.include? x
|
39
|
+
true
|
40
|
+
end
|
41
|
+
end
|
42
|
+
false
|
43
|
+
end
|
44
|
+
|
45
|
+
public
|
46
|
+
def is_valid_email_address(email_address)
|
47
|
+
|
48
|
+
if value_empty(email_address)
|
49
|
+
false
|
50
|
+
else
|
51
|
+
|
52
|
+
if has_invalid_parts(email_address) || has_invalid_characters(email_address)
|
53
|
+
false
|
54
|
+
else
|
55
|
+
parts = email_address.split('@')
|
56
|
+
if parts.count != 2
|
57
|
+
false
|
58
|
+
else
|
59
|
+
!(is_part_empty(parts[0]) || is_part_empty(parts[1]))
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require_relative '../core/string_extension.rb'
|
3
|
+
|
4
|
+
module SocketLabs
|
5
|
+
module InjectionApi
|
6
|
+
module Message
|
7
|
+
|
8
|
+
# Represents a custom header as a name-value pair.
|
9
|
+
# Example:
|
10
|
+
#
|
11
|
+
# attachment1 = Attachment(file_path="./bus.png")
|
12
|
+
#
|
13
|
+
# attachment2 = Attachment("bus", "image/png", "../bus.png")
|
14
|
+
#
|
15
|
+
# attachment3 = Attachment("bus", "image/png", content=bytes())
|
16
|
+
# attachment3.add_custom_header("name1", "value1")
|
17
|
+
# attachment3.add_custom_header("name2", "value2")
|
18
|
+
class Attachment
|
19
|
+
|
20
|
+
# Name of attachment (displayed in email clients).
|
21
|
+
attr_accessor :name
|
22
|
+
# BASE64 encoded string containing the contents of an attachment.
|
23
|
+
attr_accessor :content
|
24
|
+
# The MIME type of the attachment.
|
25
|
+
attr_accessor :mime_type
|
26
|
+
# A list of custom headers for the attachment.
|
27
|
+
attr_accessor :custom_headers
|
28
|
+
# The local file path for your attachment, used to stream the file in.
|
29
|
+
attr_accessor :file_path
|
30
|
+
# The contentId for your attachment, used if you need to reference the attachment in your email content.
|
31
|
+
attr_accessor :content_id
|
32
|
+
|
33
|
+
# Initializes a new instance of the CustomHeader class
|
34
|
+
# @param [merge] arguments - accepted arguments file_path, name, mime_type, content_id, custom_headers, content
|
35
|
+
|
36
|
+
def initialize(arguments)
|
37
|
+
|
38
|
+
@custom_headers = Array.new
|
39
|
+
|
40
|
+
unless arguments[:file_path].nil? || arguments[:file_path].empty?
|
41
|
+
readfile(arguments[:file_path])
|
42
|
+
end
|
43
|
+
|
44
|
+
unless arguments[:name].nil? || arguments[:name].empty?
|
45
|
+
@name = arguments[:name]
|
46
|
+
end
|
47
|
+
|
48
|
+
unless arguments[:mime_type].nil? || arguments[:mime_type].empty?
|
49
|
+
@mime_type = arguments[:mime_type]
|
50
|
+
end
|
51
|
+
|
52
|
+
@content_id = arguments[:content_id]
|
53
|
+
|
54
|
+
@custom_headers = []
|
55
|
+
unless arguments[:custom_headers].nil? || arguments[:custom_headers].empty?
|
56
|
+
@custom_headers = arguments[:custom_headers]
|
57
|
+
end
|
58
|
+
|
59
|
+
unless arguments[:content].nil? || arguments[:content].empty?
|
60
|
+
@content = arguments[:content]
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
# Read the specified file and get a str containing the resulting binary data.
|
66
|
+
# @param [String] file_path
|
67
|
+
def readfile(file_path)
|
68
|
+
|
69
|
+
@file_path = file_path
|
70
|
+
@name = File.basename(file_path)
|
71
|
+
@mime_type = get_mime_type_from_ext(File.extname(file_path))
|
72
|
+
|
73
|
+
file = File.open(file_path, "rb")
|
74
|
+
encoded_string = Base64.encode64(file.read)
|
75
|
+
@content = encoded_string
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
# Takes a file extension, minus the '.', and returns the corresponding MimeType for the given extension.
|
80
|
+
# @param [String] extension
|
81
|
+
# @return [String] mime type
|
82
|
+
def get_mime_type_from_ext(extension)
|
83
|
+
extension[0]=''
|
84
|
+
types = [
|
85
|
+
{ :ext => "txt", :mime_type => "text/plain" },
|
86
|
+
{ :ext => "ini", :mime_type => "text/plain" },
|
87
|
+
{ :ext => "sln", :mime_type => "text/plain" },
|
88
|
+
{ :ext => "cs", :mime_type => "text/plain" },
|
89
|
+
{ :ext => "js", :mime_type => "text/plain" },
|
90
|
+
{ :ext => "config", :mime_type => "text/plain" },
|
91
|
+
{ :ext => "vb", :mime_type => "text/plain" },
|
92
|
+
{ :ext => :"jpg", :mime_type => "image/jpeg" },
|
93
|
+
{ :ext => "jpeg", :mime_type => "image/jpeg" },
|
94
|
+
{ :ext => "bmp", :mime_type => "image/bmp" },
|
95
|
+
{ :ext => "csv", :mime_type => "text/csv" },
|
96
|
+
{ :ext => "doc", :mime_type => "application/msword" },
|
97
|
+
{ :ext => "docx", :mime_type => "application/vnd.openxmlformats-officedocument.wordprocessingml.document" },
|
98
|
+
{ :ext => "gif", :mime_type => "image/gif" },
|
99
|
+
{ :ext => "html", :mime_type => "text/html" },
|
100
|
+
{ :ext => "pdf", :mime_type => "application/pdf" },
|
101
|
+
{ :ext => "png", :mime_type => "image/png" },
|
102
|
+
{ :ext => "ppt", :mime_type => "application/vnd.ms-powerpoint" },
|
103
|
+
{ :ext => "pptx", :mime_type => "application/vnd.openxmlformats-officedocument.presentationml.presentation" },
|
104
|
+
{ :ext => "xls", :mime_type => "application/vnd.ms-excel" },
|
105
|
+
{ :ext => "xlsx", :mime_type => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" },
|
106
|
+
{ :ext => "xml", :mime_type => "application/xml" },
|
107
|
+
{ :ext => "zip", :mime_type => "application/x-zip-compressed" },
|
108
|
+
{ :ext => "wav", :mime_type => "audio/wav" },
|
109
|
+
{ :ext => "eml", :mime_type => "message/rfc822" },
|
110
|
+
{ :ext => "mp3", :mime_type => "audio/mpeg" },
|
111
|
+
{ :ext => "mp4", :mime_type => "video/mp4" },
|
112
|
+
{ :ext => "mov", :mime_type => "video/quicktime" }
|
113
|
+
]
|
114
|
+
mime = types.find {|x| x[:ext] == extension}
|
115
|
+
|
116
|
+
unless mime.nil? || mime.empty?
|
117
|
+
"application/octet-stream"
|
118
|
+
end
|
119
|
+
mime[:mime_type]
|
120
|
+
end
|
121
|
+
|
122
|
+
# Add a CustomHeader to the attachment
|
123
|
+
# @param [String/CustomHeader] name
|
124
|
+
# @param [String] value
|
125
|
+
def add_custom_header(name, value)
|
126
|
+
|
127
|
+
if name.kind_of? CustomHeader
|
128
|
+
@custom_headers.push(name)
|
129
|
+
|
130
|
+
elsif name.kind_of? String
|
131
|
+
@custom_headers.push(CustomHeader.new(name, value))
|
132
|
+
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
# Represents the Attachment as a String
|
137
|
+
# @return [String]
|
138
|
+
def to_s
|
139
|
+
"#{@name}, #{@mime_type}"
|
140
|
+
end
|
141
|
+
|
142
|
+
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|