File: multipartable.rb

package info (click to toggle)
ruby-multipart-post 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 136 kB
  • sloc: ruby: 423; makefile: 4
file content (29 lines) | stat: -rw-r--r-- 1,062 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#--
# Copyright (c) 2007-2013 Nick Sieger.
# See the file README.txt included with the distribution for
# software license details.
#++

require 'parts'
  module Multipartable
    DEFAULT_BOUNDARY = "-----------RubyMultipartPost"
    def initialize(path, params, headers={}, boundary = DEFAULT_BOUNDARY)
      headers = headers.clone # don't want to modify the original variable
      parts_headers = headers.delete(:parts) || {}
      super(path, headers)
      parts = params.map do |k,v|
        case v
        when Array
          v.map {|item| Parts::Part.new(boundary, k, item, parts_headers[k]) }
        else
          Parts::Part.new(boundary, k, v, parts_headers[k])
        end
      end.flatten
      parts << Parts::EpiloguePart.new(boundary)
      ios = parts.map {|p| p.to_io }
      self.set_content_type(headers["Content-Type"] || "multipart/form-data",
                            { "boundary" => boundary })
      self.content_length = parts.inject(0) {|sum,i| sum + i.length }
      self.body_stream = CompositeReadIO.new(*ios)
    end
  end