Module: Notion::Multipart
- Defined in:
- lib/notion/multipart.rb
Class Method Summary collapse
- .append_field(body, boundary, name, value) ⇒ Object
- .encode(data, filename:, content_type:, part_number: nil) ⇒ Object
- .header_value(value, name) ⇒ Object
- .quoted_header_value(value, name) ⇒ Object
Class Method Details
.append_field(body, boundary, name, value) ⇒ Object
33 34 35 36 37 |
# File 'lib/notion/multipart.rb', line 33 def append_field(body, boundary, name, value) body << "--#{boundary}\r\n" body << %(Content-Disposition: form-data; name="#{name}"\r\n\r\n) body << "#{value}\r\n" end |
.encode(data, filename:, content_type:, part_number: nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/notion/multipart.rb', line 9 def encode(data, filename:, content_type:, part_number: nil) filename = quoted_header_value(filename, "filename") content_type = header_value(content_type, "content_type") boundary = "notion-#{SecureRandom.hex(12)}" body = +"".b append_field(body, boundary, "part_number", part_number.to_s) if part_number body << "--#{boundary}\r\n" body << %(Content-Disposition: form-data; name="file"; filename="#{filename}"\r\n) body << "Content-Type: #{content_type}\r\n\r\n" body << data.b << "\r\n--#{boundary}--\r\n" [body, "multipart/form-data; boundary=#{boundary}"] end |
.header_value(value, name) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/notion/multipart.rb', line 22 def header_value(value, name) value = value.to_s raise ArgumentError, "#{name} cannot contain newlines" if value.match?(/[\r\n]/) value end |
.quoted_header_value(value, name) ⇒ Object
29 30 31 |
# File 'lib/notion/multipart.rb', line 29 def quoted_header_value(value, name) header_value(value, name).gsub(/[\\"]/) { |character| "\\#{character}" } end |