From 93d42e176d43fb9e6a1e616f6e9300c7dc7380db Mon Sep 17 00:00:00 2001 From: Fabien Chaynes Date: Wed, 30 May 2018 10:43:37 +0200 Subject: [PATCH] Allow Urlencoded to be instanciated with an encoder. --- lib/http/form_data.rb | 10 ++++++++-- lib/http/form_data/urlencoded.rb | 9 ++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/http/form_data.rb b/lib/http/form_data.rb index be542bc..c043dd1 100644 --- a/lib/http/form_data.rb +++ b/lib/http/form_data.rb @@ -43,9 +43,15 @@ class << self # @return [Urlencoded] otherwise def create(data) data = ensure_hash data - klass = multipart?(data) ? Multipart : Urlencoded + if multipart?(data) + Multipart.new data + else + Urlencoded.new data, encoder + end + end - klass.new data + def encoder + ::URI.method(:encode_www_form) end # Coerce `obj` to Hash. diff --git a/lib/http/form_data/urlencoded.rb b/lib/http/form_data/urlencoded.rb index 176aa63..b0de248 100644 --- a/lib/http/form_data/urlencoded.rb +++ b/lib/http/form_data/urlencoded.rb @@ -12,8 +12,8 @@ class Urlencoded include Readable # @param [#to_h, Hash] data form data key-value Hash - def initialize(data) - encoded_data = encode(data) + def initialize(data, encoder = ::URI.method(:encode_www_form)) + encoded_data = encoder.call(FormData.ensure_hash(data)) @io = StringIO.new(encoded_data) end @@ -29,11 +29,6 @@ def content_type # # @return [Integer] alias content_length size - - # @param [#to_h, Hash] data form data key-value Hash - def encode(data) - ::URI.encode_www_form(FormData.ensure_hash(data)) - end end end end