forked from sendgrid/sendgrid-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attachment.rb
55 lines (47 loc) · 1.36 KB
/
attachment.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require 'base64'
module SendGrid
class Attachment
attr_accessor :type, :filename, :disposition, :content_id
def initialize
@content = nil
@type = nil
@filename = nil
@disposition = nil
@content_id = nil
end
def content=(content)
@encoded_content = nil
@content = content
end
def content
return @encoded_content if @encoded_content
@encoded_content = if @content.respond_to?(:read)
encode @content
else
@content
end
end
def to_json(*)
{
'content' => content,
'type' => type,
'filename' => filename,
'disposition' => disposition,
'content_id' => content_id
}.delete_if { |_, value| value.to_s.strip == '' }
end
private
def encode(io)
str = io.read
# Since the API expects UTF-8, we need to ensure that we're
# converting other formats to it so (byte-wise) Base64 encoding
# will come through properly on the other side.
#
# Not much to be done to try to handle encoding for files opened
# in binary mode, but at least we can faithfully convey the
# bytes.
str = str.encode('UTF-8') unless io.respond_to?(:binmode?) && io.binmode?
Base64.encode64 str
end
end
end