Class: Multipart
- Inherits:
-
Base
show all
- Defined in:
- lib/restclient/multipart.rb
Constant Summary
collapse
- EOL =
"\r\n"
Instance Method Summary
collapse
Methods inherited from Base
#escape, #initialize, #read, #rewind, #size
Constructor Details
This class inherits a constructor from Base
Instance Method Details
#boundary ⇒ Object
81
82
83
|
# File 'lib/restclient/multipart.rb', line 81
def boundary
@boundary ||= rand(1_000_000).to_s
end
|
#build_stream(params) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/restclient/multipart.rb', line 41
def build_stream(params)
b = "--#{boundary}"
@stream = Tempfile.new("RESTClient.Stream.#{rand(1000)}")
@stream.write(b)
params.each do |k,v|
@stream.write(EOL)
if v.respond_to?(:read) && v.respond_to?(:path)
create_file_field(@stream, k,v)
else
create_regular_field(@stream, k,v)
end
@stream.write(EOL + b)
end
@stream.write('--')
@stream.write(EOL)
@stream.seek(0)
end
|
#close ⇒ Object
89
90
91
|
# File 'lib/restclient/multipart.rb', line 89
def close
@stream.close
end
|
#create_file_field(s, k, v, content_type = nil) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/restclient/multipart.rb', line 67
def create_file_field(s, k, v, content_type = nil)
begin
content_type ||= `file -b --mime #{v.path}`.gsub(/\n/, '')
s.write("Content-Disposition: multipart/form-data; name=\"#{k}\"; filename=\"#{v.path}\"#{EOL}")
s.write("Content-Type: #{content_type}#{EOL}")
s.write(EOL)
while data = v.read(8124)
s.write(data)
end
ensure
v.close
end
end
|
#create_regular_field(s, k, v) ⇒ Object
60
61
62
63
64
65
|
# File 'lib/restclient/multipart.rb', line 60
def create_regular_field(s, k, v)
s.write("Content-Disposition: multipart/form-data; name=\"#{k}\"")
s.write(EOL)
s.write(EOL)
s.write(v)
end
|
85
86
87
|
# File 'lib/restclient/multipart.rb', line 85
def
super.merge({:content_type => %Q{multipart/form-data; boundary="#{boundary}"}})
end
|