Class: BraintreeHttp::Multipart
- Inherits:
-
Object
- Object
- BraintreeHttp::Multipart
- Defined in:
- lib/braintreehttp/encoder.rb
Instance Method Summary collapse
- #_add_file_part(key, file) ⇒ Object
- #_add_form_field(key, value) ⇒ Object
- #_mime_type_for_file_name(filename) ⇒ Object
- #content_type ⇒ Object
- #decode(body) ⇒ Object
- #encode(request) ⇒ Object
Instance Method Details
#_add_file_part(key, file) ⇒ Object
101 102 103 104 105 |
# File 'lib/braintreehttp/encoder.rb', line 101 def _add_file_part(key, file) mime_type = _mime_type_for_file_name(file.path) return "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{File.basename(file.path)}\"#{LINE_FEED}" + "Content-Type: #{mime_type}#{LINE_FEED}#{LINE_FEED}#{file.read}#{LINE_FEED}" end |
#_add_form_field(key, value) ⇒ Object
97 98 99 |
# File 'lib/braintreehttp/encoder.rb', line 97 def _add_form_field(key, value) return "Content-Disposition: form-data; name=\"#{key}\"#{LINE_FEED}#{LINE_FEED}#{value}#{LINE_FEED}" end |
#_mime_type_for_file_name(filename) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/braintreehttp/encoder.rb', line 107 def _mime_type_for_file_name(filename) file_extension = File.extname(filename).strip.downcase[1..-1] if file_extension == "jpeg" || file_extension == "jpg" return "image/jpeg" elsif file_extension == "png" return "image/png" elsif file_extension == "pdf" return "application/pdf" else return "application/octet-stream" end end |
#content_type ⇒ Object
93 94 95 |
# File 'lib/braintreehttp/encoder.rb', line 93 def content_type /^multipart\/.*/ end |
#decode(body) ⇒ Object
89 90 91 |
# File 'lib/braintreehttp/encoder.rb', line 89 def decode(body) raise UnsupportedEncodingError.new("Multipart does not support deserialization") end |
#encode(request) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/braintreehttp/encoder.rb', line 73 def encode(request) boundary = DateTime.now.strftime("%Q") request.headers["Content-Type"] = "multipart/form-data; boundary=#{boundary}" form_params = [] request.body.each do |k, v| if v.is_a? File form_params.push(_add_file_part(k, v)) else form_params.push(_add_form_field(k, v)) end end form_params.collect {|p| "--" + boundary + "#{LINE_FEED}" + p}.join("") + "--" + boundary + "--" end |