Skip to content

Commit

Permalink
Allow Urlencoded to be instanciated with an encoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
FabienChaynes committed May 30, 2018
1 parent 0b7edc5 commit 93d42e1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
10 changes: 8 additions & 2 deletions lib/http/form_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 2 additions & 7 deletions lib/http/form_data/urlencoded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

0 comments on commit 93d42e1

Please sign in to comment.