Parent

Class Index [+]

Quicksearch

Rack::Multipart::Generator

Public Class Methods

new(params, first = true) click to toggle source
    # File lib/rack/multipart/generator.rb, line 4
 4:       def initialize(params, first = true)
 5:         @params, @first = params, first
 6: 
 7:         if @first && !@params.is_a?(Hash)
 8:           raise ArgumentError, "value must be a Hash"
 9:         end
10:       end

Public Instance Methods

dump() click to toggle source
    # File lib/rack/multipart/generator.rb, line 12
12:       def dump
13:         return nil if @first && !multipart?
14:         return flattened_params if !@first
15: 
16:         flattened_params.map do |name, file|
17:           if file.respond_to?(:original_filename)
18:             ::File.open(file.path, "rb") do |f|
19:               f.set_encoding(Encoding::BINARY) if f.respond_to?(:set_encoding)
20:               content_for_tempfile(f, file, name)
21:             end
22:           else
23:             content_for_other(file, name)
24:           end
25:         end.join + "--#{MULTIPART_BOUNDARY}--\r"
26:       end

Private Instance Methods

content_for_other(file, name) click to toggle source
    # File lib/rack/multipart/generator.rb, line 83
83:       def content_for_other(file, name)
84: --#{MULTIPART_BOUNDARY}\rContent-Disposition: form-data; name="#{name}"\r\r#{file}\r
85:       end
content_for_tempfile(io, file, name) click to toggle source
    # File lib/rack/multipart/generator.rb, line 72
72:       def content_for_tempfile(io, file, name)
73: --#{MULTIPART_BOUNDARY}\rContent-Disposition: form-data; name="#{name}"; filename="#{Utils.escape(file.original_filename)}"\rContent-Type: #{file.content_type}\rContent-Length: #{::File.stat(file.path).size}\r\r#{io.read}\r
74:       end
flattened_params() click to toggle source
    # File lib/rack/multipart/generator.rb, line 47
47:       def flattened_params
48:         @flattened_params ||= begin
49:           h = Hash.new
50:           @params.each do |key, value|
51:             k = @first ? key.to_s : "[#{key}]"
52: 
53:             case value
54:             when Array
55:               value.map { |v|
56:                 Multipart.build_multipart(v, false).each { |subkey, subvalue|
57:                   h["#{k}[]#{subkey}"] = subvalue
58:                 }
59:               }
60:             when Hash
61:               Multipart.build_multipart(value, false).each { |subkey, subvalue|
62:                 h[k + subkey] = subvalue
63:               }
64:             else
65:               h[k] = value
66:             end
67:           end
68:           h
69:         end
70:       end
multipart?() click to toggle source
    # File lib/rack/multipart/generator.rb, line 29
29:       def multipart?
30:         multipart = false
31: 
32:         query = lambda { |value|
33:           case value
34:           when Array
35:             value.each(&query)
36:           when Hash
37:             value.values.each(&query)
38:           when Rack::Multipart::UploadedFile
39:             multipart = true
40:           end
41:         }
42:         @params.values.each(&query)
43: 
44:         multipart
45:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.