Sha256: bc8fefdfe71e68ef324ba3ebabfa04cc3cf9fd95a32945abb152e7c92f9b9dd0

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

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

require 'json'

require 'protocol/http/body/wrapper'
require 'protocol/http/body/buffered'

require_relative 'generic'

module Async
	module REST
		module Wrapper
			class URLEncoded < Generic
				APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded".freeze
				
				def initialize(content_type = APPLICATION_FORM_URLENCODED)
					@content_type = content_type
				end
				
				attr :content_type
				
				def split(*arguments)
					@content_type.split
				end
				
				def prepare_request(request, payload)
					request.headers['accept'] ||= @content_type
					
					if payload
						request.headers['content-type'] = @content_type
						
						request.body = ::Protocol::HTTP::Body::Buffered.new([
							::Protocol::HTTP::URL.encode(payload)
						])
					end
				end
				
				class Parser < ::Protocol::HTTP::Body::Wrapper
					def join
						::Protocol::HTTP::URL.decode(super, symbolize_keys: true)
					end
				end
				
				def parser_for(response)
					if content_type = response.headers['content-type']
						if content_type.start_with? @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/url_encoded.rb
async-rest-0.13.0 lib/async/rest/wrapper/url_encoded.rb