Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration to allow overriding the encoding method. #23

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/http/form_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,20 @@ class << self
# @return [Multipart] if any of values is a {FormData::File}
# @return [Urlencoded] otherwise
def create(data)
data = ensure_hash data
klass = multipart?(data) ? Multipart : Urlencoded
data = ensure_hash data
if multipart?(data)
Multipart.new data
else
Urlencoded.new data, encoder
end
end

def encoder
@encoder ||= ::URI.method(:encode_www_form)
end

klass.new data
def encoder=(encoder)
@encoder = encoder
end

# Coerce `obj` to Hash.
Expand Down
6 changes: 3 additions & 3 deletions lib/http/form_data/urlencoded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class Urlencoded
include Readable

# @param [#to_h, Hash] data form data key-value Hash
def initialize(data)
uri_encoded_data = ::URI.encode_www_form FormData.ensure_hash(data)
@io = StringIO.new(uri_encoded_data)
def initialize(data, encoder = ::URI.method(:encode_www_form))
encoded_data = encoder.call(FormData.ensure_hash(data))
@io = StringIO.new(encoded_data)
end

# Returns MIME type to be used for HTTP request `Content-Type` header.
Expand Down