Sha256: 4fb9d6a01dc17c46f2120cdab69324bec3bde4888428a6cb82acfc8f0a604ef7

Contents?: true

Size: 1.41 KB

Versions: 20

Compression:

Stored size: 1.41 KB

Contents

# from http://devlicio.us/blogs/sergio_pereira/archive/2008/04/02/xhtml-validation-script-using-ruby.aspx

require 'rubygems'
require 'mime/types'
require 'net/http'
require 'CGI'
 
class FormField
  attr_accessor :name, :value
  def initialize( name, value )
    @name = name
    @value = value
  end
 
  def to_form_data
    field = CGI::escape(@name)
    "Content-Disposition: form-data; name=\"#{field}\"" + 
      "\r\n\r\n#{@value}\r\n"
  end
end
 
class FileField
  attr_accessor :name, :path, :content
  def initialize( name, path, content )
    @name = name
    @path = "#{path}.html"
    @content = content
  end
 
  def to_form_data
    "Content-Disposition: form-data; " + 
    "name=\"#{CGI::escape(@name)}\"; " + 
    "filename=\"#{@path}\"\r\n" +
    "Content-Transfer-Encoding: binary\r\n" +
    "Content-Type: #{MIME::Types.type_for(@path)}" + 
    "\r\n\r\n#{@content}\r\n"
  end
end

class MultipartPost
  SEPARATOR = 'willvalidate-aaaaaabbbb0000'
  REQ_HEADER = {
      "Content-type" => "multipart/form-data, boundary=#{SEPARATOR} "
   }
 
  def self.build_form_data ( form_fields )
    fields = []
    form_fields.each do |key, value|
      if value.instance_of?(File)
        fields << FileField.new(key.to_s, value.path, value.read)
      else
        fields << FormField.new(key.to_s, value)
      end
    end
    fields.collect {|f| "--#{SEPARATOR}\r\n#{f.to_form_data}" }.join("") + 
         "--#{SEPARATOR}--"
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
ymdp-0.1.4 lib/ymdp/processor/form_post.rb
ymdp-0.1.3.1 lib/ymdp/support/form_post.rb
ymdp-0.1.3 lib/ymdp/support/form_post.rb
ymdp-0.1.1 lib/ymdp/support/form_post.rb
ymdp-0.1.0 lib/ymdp/support/form_post.rb
ymdp-0.0.17 lib/application_view/support/form_post.rb
ymdp-0.0.16 lib/application_view/support/form_post.rb
ymdp-0.0.15 lib/application_view/support/form_post.rb
ymdp-0.0.14 lib/application_view/support/form_post.rb
ymdp-0.0.13 lib/application_view/support/form_post.rb
ymdp-0.0.12 lib/application_view/support/form_post.rb
ymdp-0.0.11 lib/application_view/support/form_post.rb
ymdp-0.0.10 lib/application_view/support/form_post.rb
ymdp-0.0.9 lib/application_view/support/form_post.rb
ymdp-0.0.8 lib/application_view/support/form_post.rb
ymdp-0.0.7 lib/application_view/support/form_post.rb
ymdp-0.0.6 lib/application_view/support/form_post.rb
ymdp-0.0.5 lib/application_view/support/form_post.rb
ymdp-0.0.4 lib/application_view/support/form_post.rb
ymdp-0.0.3 lib/application_view/support/form_post.rb