Sha256: a392d2b6752419f21ae74be8dc33a87274cf8de0872b9075058dc45f10837466

Contents?: true

Size: 1.29 KB

Versions: 34

Compression:

Stored size: 1.29 KB

Contents

require 'resourceful/abstract_form_data'

module Resourceful
  class MultipartFormData < AbstractFormData
    FileParamValue = Struct.new(:content, :file_name, :content_type)

    def add_file(name, file_name, content_type="application/octet-stream")
      add(name, FileParamValue.new(File.new(file_name, 'r'), File.basename(file_name), content_type))
    end

    def content_type
      "multipart/form-data; boundary=#{boundary}"
    end

    def read
      StringIO.new.tap do |out|
        first = true
        form_data.each do |key, val|
          out << "\r\n" unless first
          out << "--" << boundary
          out << "\r\nContent-Disposition: form-data; name=\"#{key}\""
          if val.kind_of?(FileParamValue)
            out << "; filename=\"#{val.file_name}\""
            out << "\r\nContent-Type: #{val.content_type}"
          end
          out << "\r\n\r\n"
          if val.kind_of?(FileParamValue)
            out << val.content.read
          else
            out << val.to_s
          end
          first = false
        end
        out << "\r\n--#{boundary}--"
      end.string
    end

    protected

    def boundary
      @boundary ||= (0..30).map{BOUNDARY_CHARS[rand(BOUNDARY_CHARS.length)]}.join
    end

    BOUNDARY_CHARS = [('a'..'z').to_a,('A'..'Z').to_a,(0..9).to_a].flatten
  end
end

Version data entries

34 entries across 34 versions & 5 rubygems

Version Path
paul-resourceful-0.6.0 lib/resourceful/multipart_form_data.rb
pezra-resourceful-0.6.0 lib/resourceful/multipart_form_data.rb
pezra-resourceful-0.7.0 lib/resourceful/multipart_form_data.rb
abiquo-etk-0.6.4 vendor/resourceful-1.0.1/lib/resourceful/multipart_form_data.rb
abiquo-etk-0.6.3 vendor/resourceful-1.0.1/lib/resourceful/multipart_form_data.rb
abiquo-etk-0.6.2 vendor/resourceful-1.0.1/lib/resourceful/multipart_form_data.rb
abiquo-etk-0.6.1 vendor/resourceful-1.0.1/lib/resourceful/multipart_form_data.rb
abiquo-etk-0.6.0 vendor/resourceful-1.0.1/lib/resourceful/multipart_form_data.rb
abiquo-etk-0.5.9 vendor/resourceful-1.0.1/lib/resourceful/multipart_form_data.rb
abiquo-etk-0.5.8 vendor/resourceful-1.0.1/lib/resourceful/multipart_form_data.rb
abiquo-etk-0.5.3 vendor/resourceful-1.0.1/lib/resourceful/multipart_form_data.rb
abiquo-etk-0.4.42 vendor/resourceful-1.0.1/lib/resourceful/multipart_form_data.rb
abiquo-etk-0.4.33 vendor/resourceful-1.0.1/lib/resourceful/multipart_form_data.rb
abiquo-etk-0.4.32 vendor/resourceful-1.0.1/lib/resourceful/multipart_form_data.rb
abiquo-etk-0.4.29 vendor/resourceful-1.0.1/lib/resourceful/multipart_form_data.rb
abiquo-etk-0.4.25 vendor/resourceful-1.0.1/lib/resourceful/multipart_form_data.rb
abiquo-etk-0.4.24 vendor/resourceful-1.0.1/lib/resourceful/multipart_form_data.rb
abiquo-etk-0.4.23 vendor/resourceful-1.0.1/lib/resourceful/multipart_form_data.rb
abiquo-etk-0.4.22 vendor/resourceful-1.0.1/lib/resourceful/multipart_form_data.rb
abiquo-etk-0.4.20 vendor/resourceful-1.0.1/lib/resourceful/multipart_form_data.rb