Class: RestClient::Payload::Multipart

Inherits:
Base
  • Object
show all
Defined in:
lib/vendor/rest-client/lib/rest_client/payload.rb

Constant Summary collapse

EOL =
"\r\n"

Instance Method Summary collapse

Methods inherited from Base

#escape, #initialize, #read, #size

Constructor Details

This class inherits a constructor from RestClient::Payload::Base

Instance Method Details

#boundaryObject



112
113
114
# File 'lib/vendor/rest-client/lib/rest_client/payload.rb', line 112

def boundary
	@boundary ||= rand(1_000_000).to_s
end

#build_stream(params) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/vendor/rest-client/lib/rest_client/payload.rb', line 68

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

#closeObject



120
121
122
# File 'lib/vendor/rest-client/lib/rest_client/payload.rb', line 120

def close
	@stream.close
end

#create_file_field(s, k, v) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/vendor/rest-client/lib/rest_client/payload.rb', line 94

def create_file_field(s, k, v)
	begin
		s.write("Content-Disposition: multipart/form-data; name=\"#{k}\"; filename=\"#{v.path}\"#{EOL}")
		s.write("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



87
88
89
90
91
92
# File 'lib/vendor/rest-client/lib/rest_client/payload.rb', line 87

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

#headersObject



116
117
118
# File 'lib/vendor/rest-client/lib/rest_client/payload.rb', line 116

def headers
	super.merge({'Content-Type' => %Q{multipart/form-data; boundary="#{boundary}"}})
end

#mime_for(path) ⇒ Object



107
108
109
110
# File 'lib/vendor/rest-client/lib/rest_client/payload.rb', line 107

def mime_for(path)
	ext = File.extname(path)[1..-1]
	MIME_TYPES[ext] || 'text/plain'
end