Sha256: aeee3888325879b348fc8e52830c98f21502d89f42a5ea2950230df29d5ac5aa

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2023, by Samuel Williams.

require_relative 'json'
require_relative 'url_encoded'

module Async
	module REST
		module Wrapper
			class Form < Generic
				DEFAULT_CONTENT_TYPES = {
					JSON::APPLICATION_JSON => JSON::Parser,
					URLEncoded::APPLICATION_FORM_URLENCODED => URLEncoded::Parser,
				}
				
				def initialize(content_types = DEFAULT_CONTENT_TYPES)
					@content_types = content_types
				end
				
				def prepare_request(request, payload)
					@content_types.each_key do |key|
						request.headers.add('accept', key)
					end
					
					if payload
						request.headers['content-type'] = URLEncoded::APPLICATION_FORM_URLENCODED
						
						request.body = ::Protocol::HTTP::Body::Buffered.new([
							::Protocol::HTTP::URL.encode(payload)
						])
					end
				end
				
				def parser_for(response)
					if content_type = response.headers['content-type']
						if parser = @content_types[content_type]
							return parser
						end
					end
					
					return super
				end
			end
		end
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
async-rest-0.14.0 lib/async/rest/wrapper/form.rb
async-rest-0.13.0 lib/async/rest/wrapper/form.rb