Class: MultiPart::Post
- Inherits:
-
Object
- Object
- MultiPart::Post
- Defined in:
- lib/multi_part/multi_part_post.rb
Instance Method Summary collapse
- #construct_post_params(post_params) ⇒ Object
-
#initialize(post_params, request_headers = {}) ⇒ Post
constructor
A new instance of Post.
- #multi_part_boundary ⇒ Object
- #submit(to_url, query_string = nil, content_type = 'multipart/form-data') ⇒ Object
Constructor Details
#initialize(post_params, request_headers = {}) ⇒ Post
Returns a new instance of Post.
7 8 9 10 11 |
# File 'lib/multi_part/multi_part_post.rb', line 7 def initialize(post_params, request_headers={}) @parts, @streams = [], [] construct_post_params(post_params) @request_headers = request_headers end |
Instance Method Details
#construct_post_params(post_params) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/multi_part/multi_part_post.rb', line 13 def construct_post_params(post_params) post_params.each_pair do |key, val| if(val.respond_to?(:content_type)) #construct file part @parts << Parts::StringParam.new( "--" + multi_part_boundary + "\r\n" + \ "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{val.file_name}\"\r\n" + \ "Content-Type: #{val.content_type}\r\n\r\n" ) @streams << val @parts << Parts::StreamParam.new(val, val.file_size) else #construct string part param @parts << Parts::StringParam.new("--#{multi_part_boundary}\r\n" + "Content-Disposition: form-data; name=\"#{key}\"\r\n" + "\r\n" + "#{val}\r\n") end end @parts << Parts::StringParam.new( "\r\n--" + multi_part_boundary + "--\r\n" ) end |
#multi_part_boundary ⇒ Object
29 30 31 |
# File 'lib/multi_part/multi_part_post.rb', line 29 def multi_part_boundary @boundary ||= 'END_OF_PART' end |
#submit(to_url, query_string = nil, content_type = 'multipart/form-data') ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/multi_part/multi_part_post.rb', line 33 def submit(to_url, query_string=nil, content_type='multipart/form-data') post_stream = Stream::MultiPart.new(@parts) url = URI.parse( to_url ) post_url_with_query_string = "#{url.path}" post_url_with_query_string = "#{post_url_with_query_string}?#{query_string}" unless(query_string.nil?) req = Net::HTTP::Post.new(post_url_with_query_string, @request_headers) req.content_length = post_stream.size req.content_type = "#{content_type}; boundary=#{multi_part_boundary}" req.body_stream = post_stream http_handle = Net::HTTP.new(url.host, url.port) http_handle.use_ssl = true if(url.scheme == "https") res = http_handle.start {|http| http.request(req)} #close all the open hooks to the file on file-system @streams.each do |local_stream| local_stream.close(); end res end |