Sha256: 14ba471ea703003b8f5a6f961a2f2e7af662cd9d26e77c38b7aed3c20eca5a8f

Contents?: true

Size: 1.07 KB

Versions: 4

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2024, 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

4 entries across 4 versions & 1 rubygems

Version Path
async-rest-0.18.0 lib/async/rest/wrapper/form.rb
async-rest-0.17.0 lib/async/rest/wrapper/form.rb
async-rest-0.16.0 lib/async/rest/wrapper/form.rb
async-rest-0.15.0 lib/async/rest/wrapper/form.rb