Class Net::HTTP::MultipartPost
In: lib/net/http_multipart_post.rb
Parent: Object

Methods

Constants

BOUNDARY = "MichaelNiessnerIsSuperDuperAwesome"

Attributes

multipart_post_files  [R] 
params  [R] 
url  [R] 

Public Class methods

[Source]

# File lib/net/http_multipart_post.rb, line 24
      def initialize(url, params)
        @url = url
        @multipart_post_files = extract_file_parameters_from(params)
        @params = extract_non_file_parameters_from(params)
      end

Public Instance methods

[Source]

# File lib/net/http_multipart_post.rb, line 30
      def post
        req = Post.new(url.path)
        req.body = body
        req.content_type = content_type
        req.basic_auth url.user, url.password if url.user
        Net::HTTP.new(url.host, url.port).start {|http|
          http.request(req)
        }    
      end

Protected Instance methods

[Source]

# File lib/net/http_multipart_post.rb, line 63
      def body
        encode_parameters + encode_multipart_post_files + final_boundary
      end

[Source]

# File lib/net/http_multipart_post.rb, line 59
      def content_type
        "multipart/form-data; boundary=#{BOUNDARY}"
      end

[Source]

# File lib/net/http_multipart_post.rb, line 110
      def disposition(attribute)
        "Content-Disposition: form-data; #{attribute}\r\n"
      end

[Source]

# File lib/net/http_multipart_post.rb, line 98
      def disposition_with_filename(name, filename)
        if name.nil?
          disposition("filename=\"#{filename}\"")
        else
          disposition("name=\"#{name}\"; filename=\"#{filename}\"")
        end
      end

[Source]

# File lib/net/http_multipart_post.rb, line 106
      def disposition_with_name(name)
        disposition("name=\"#{name}\"\r\n")
      end

[Source]

# File lib/net/http_multipart_post.rb, line 78
      def encode_multipart_post_file(name, multipart_post_file)
        parameter_boundary + 
        disposition_with_filename(name, multipart_post_file.filename) + 
        file_content_type(multipart_post_file.content_type) + 
        multipart_post_file.data + 
        "\r\n"
      end

[Source]

# File lib/net/http_multipart_post.rb, line 67
      def encode_multipart_post_files
        return "" if multipart_post_files.empty?
        if multipart_post_files.size == 1
          name = multipart_post_files.keys.first
          file = multipart_post_files.values.first
          encode_multipart_post_file(name, file)
        else
          raise "Currently more than 1 file upload is not supported."
        end
      end

[Source]

# File lib/net/http_multipart_post.rb, line 90
      def encode_parameter(key, value)
        parameter_boundary + disposition_with_name(key) + value.to_s + "\r\n"
      end

[Source]

# File lib/net/http_multipart_post.rb, line 86
      def encode_parameters
       params.sort_by{|key, value| key.to_s}.map{|key, value| encode_parameter(key, value)}.join
      end

[Source]

# File lib/net/http_multipart_post.rb, line 45
      def extract_file_parameters_from(hash)
        hash.reject{|key, value| !multipart_post_file?(value)}
      end

[Source]

# File lib/net/http_multipart_post.rb, line 49
      def extract_non_file_parameters_from(hash)
        hash.reject{|key, value| multipart_post_file?(value)}
      end

[Source]

# File lib/net/http_multipart_post.rb, line 94
      def file_content_type(string)
        "Content-Type: #{string}\r\n\r\n"
      end

[Source]

# File lib/net/http_multipart_post.rb, line 118
      def final_boundary
        "--#{BOUNDARY}--\r\n"
      end

[Source]

# File lib/net/http_multipart_post.rb, line 53
      def multipart_post_file?(object)
        object.respond_to?(:content_type) &&
        object.respond_to?(:data) &&
        object.respond_to?(:filename)
      end

[Source]

# File lib/net/http_multipart_post.rb, line 114
      def parameter_boundary
        "--#{BOUNDARY}\r\n"
      end

[Validate]