Sha256: f5012d3156795b071af82d532754819d6b1c3b6f9c3818fd03416fc8a942ca90

Contents?: true

Size: 1.27 KB

Versions: 22

Compression:

Stored size: 1.27 KB

Contents

class MultipartResponse
	class NoBoundaryFoundInContentTypeError < ArgumentError
		def initialize(content_type_header, exception)
			super("Content-Type header of '#{content_type_header}' has no boundary defined: #{exception.class.name}: #{exception}")
		end
	end

	class Part
		def initialize(data)
			if data.include?("\r\n\r\n")
				headers, *body = *data.split("\r\n\r\n")
				@headers = Hash[headers.split("\r\n").map{|h| h.split(/: ?/)}]
				@body = body.join("\r\n\r\n")
			else
				@headers = {'Content-Type' => 'text/plain'}
				@body = data
			end
		end

		attr_reader :headers, :body
		alias :header :headers
	end

	def initialize(content_type_header, body)
		@boundary = begin
			content_type_header.split(';').map{|e| e.strip}.select{|e| e =~ /^boundary=/}.first.match(/^boundary="(.*)"/)[1]
		rescue => e
			raise NoBoundaryFoundInContentTypeError.new(content_type_header, e)
		end

		body, epilogue = *body.split("--#{@boundary}--")
		preamble, *parts = *body.split("--#{@boundary}")
		
		@preamble = preamble.sub(/\r\n$/m, '')
		@preamble = nil if @preamble.empty?

		@epilogue = epilogue.sub(/^\r\n/m, '') if epilogue

		@parts = parts.map{|p| p.sub(/^\r\n/m, '').sub(/\r\n$/m, '')}.map{|p| Part.new(p)}
	end

	attr_reader :boundary, :preamble, :parts, :epilogue
	alias :part :parts
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
httpthumbnailer-client-0.1.1 lib/httpthumbnailer-client/multipart_response.rb
httpthumbnailer-0.3.1 lib/httpthumbnailer/multipart_response.rb
httpthumbnailer-0.3.0 lib/httpthumbnailer/multipart_response.rb
httpthumbnailer-0.2.0 lib/httpthumbnailer/multipart_response.rb
httpthumbnailer-client-0.1.0 lib/httpthumbnailer-client/multipart_response.rb
httpthumbnailer-0.1.0 lib/httpthumbnailer/multipart_response.rb
httpthumbnailer-0.0.14 lib/httpthumbnailer/multipart_response.rb
httpthumbnailer-0.0.13 lib/httpthumbnailer/multipart_response.rb
httpthumbnailer-0.0.12 lib/httpthumbnailer/multipart_response.rb
httpthumbnailer-0.0.11 lib/httpthumbnailer/multipart_response.rb
httpthumbnailer-0.0.10 lib/httpthumbnailer/multipart_response.rb
httpthumbnailer-0.0.9 lib/httpthumbnailer/multipart_response.rb
httpthumbnailer-0.0.8 lib/httpthumbnailer/multipart_response.rb
httpthumbnailer-client-0.0.3 lib/httpthumbnailer-client/multipart_response.rb
httpthumbnailer-0.0.7 lib/httpthumbnailer/multipart_response.rb
httpthumbnailer-0.0.6 lib/httpthumbnailer/multipart_response.rb
httpthumbnailer-client-0.0.2 lib/httpthumbnailer-client/multipart_response.rb
httpthumbnailer-0.0.5 lib/httpthumbnailer/multipart_response.rb
httpthumbnailer-0.0.4 lib/httpthumbnailer/multipart_response.rb
httpthumbnailer-0.0.3 lib/httpthumbnailer/multipart_response.rb