Class: RestClient::Payload::Multipart
- Inherits:
-
Base
- Object
- Base
- RestClient::Payload::Multipart
show all
- Defined in:
- lib/restclient/payload.rb
Constant Summary
collapse
- EOL =
"\r\n"
Instance Method Summary
collapse
Methods inherited from Base
#escape, #initialize, #inspect, #read, #size
Instance Method Details
#boundary ⇒ Object
134
135
136
|
# File 'lib/restclient/payload.rb', line 134
def boundary
@boundary ||= rand(1_000_000).to_s
end
|
#build_stream(params) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/restclient/payload.rb', line 87
def build_stream(params)
b = "--#{boundary}"
@stream = Tempfile.new("RESTClient.Stream.#{rand(1000)}")
@stream.write(b + EOL)
x = params.to_a
last_index = x.length - 1
x.each_with_index do |a, index|
k, v = *a
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)
@stream.write(EOL) unless last_index == index
end
@stream.write('--')
@stream.write(EOL)
@stream.seek(0)
end
|
#close ⇒ Object
142
143
144
|
# File 'lib/restclient/payload.rb', line 142
def close
@stream.close
end
|
#create_file_field(s, k, v) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/restclient/payload.rb', line 116
def create_file_field(s, k, v)
begin
s.write("Content-Disposition: multipart/form-data; name=\"#{k}\"; filename=\"#{File.basename(v.path)}\"#{EOL}")
s.write("Content-Type: #{v.respond_to?(:content_type) ? v.content_type : mime_for(v.path)}#{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
109
110
111
112
113
114
|
# File 'lib/restclient/payload.rb', line 109
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
|
138
139
140
|
# File 'lib/restclient/payload.rb', line 138
def
super.merge({'Content-Type' => %Q{multipart/form-data; boundary="#{boundary}"}})
end
|
#mime_for(path) ⇒ Object
129
130
131
132
|
# File 'lib/restclient/payload.rb', line 129
def mime_for(path)
ext = File.extname(path)[1..-1]
MIME_TYPES[ext] || 'text/plain'
end
|