From f9d127c25226a30553e9798733584a08dc637f48 Mon Sep 17 00:00:00 2001 From: Fabien Chaynes Date: Tue, 29 May 2018 17:33:08 +0200 Subject: [PATCH 1/5] Add configuration to allow overriding the encoding method. --- lib/http/form_data/configuration.rb | 29 +++++++++++++++++++++++++++++ lib/http/form_data/urlencoded.rb | 10 ++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 lib/http/form_data/configuration.rb diff --git a/lib/http/form_data/configuration.rb b/lib/http/form_data/configuration.rb new file mode 100644 index 0000000..cb6823f --- /dev/null +++ b/lib/http/form_data/configuration.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module HTTP + module FormData + def self.configuration + @configuration ||= Configuration.new + end + + def self.configuration=(configuration) + @configuration = configuration + end + + def self.configure + yield configuration + end + + def self.reset! + @configuration = Configuration.new + end + + class Configuration + attr_accessor :encoding_method + + def initialize + @encoding_method = ::URI.method(:encode_www_form) + end + end + end +end diff --git a/lib/http/form_data/urlencoded.rb b/lib/http/form_data/urlencoded.rb index 4a685d0..59270a0 100644 --- a/lib/http/form_data/urlencoded.rb +++ b/lib/http/form_data/urlencoded.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "http/form_data/readable" +require "http/form_data/configuration" require "uri" require "stringio" @@ -13,8 +14,8 @@ class Urlencoded # @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) + encoded_data = encode(data) + @io = StringIO.new(encoded_data) end # Returns MIME type to be used for HTTP request `Content-Type` header. @@ -29,6 +30,11 @@ def content_type # # @return [Integer] alias content_length size + + # @param [#to_h, Hash] data form data key-value Hash + def encode(data) + HTTP::FormData.configuration.encoding_method.call(data) + end end end end From 77803296d28f49aea16b035703fb1e1518f1df2d Mon Sep 17 00:00:00 2001 From: Fabien Chaynes Date: Tue, 29 May 2018 18:29:31 +0200 Subject: [PATCH 2/5] Fix Rubocop offenses + basic documentation. --- lib/http/form_data.rb | 20 ++++++++++++++++++++ lib/http/form_data/configuration.rb | 22 ++++++---------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/lib/http/form_data.rb b/lib/http/form_data.rb index be542bc..60b61d5 100644 --- a/lib/http/form_data.rb +++ b/lib/http/form_data.rb @@ -62,6 +62,26 @@ def ensure_hash(obj) end end + # Gets configuration. + def configuration + @configuration ||= Configuration.new + end + + # Sets configuration. + def configuration=(configuration) + @configuration = configuration + end + + # Allows to set configuration through a block. + def configure + yield configuration + end + + # Resets configuration. + def reset! + @configuration = Configuration.new + end + private # Tells whenever data contains multipart data or not. diff --git a/lib/http/form_data/configuration.rb b/lib/http/form_data/configuration.rb index cb6823f..8dab8ce 100644 --- a/lib/http/form_data/configuration.rb +++ b/lib/http/form_data/configuration.rb @@ -2,23 +2,13 @@ module HTTP module FormData - def self.configuration - @configuration ||= Configuration.new - end - - def self.configuration=(configuration) - @configuration = configuration - end - - def self.configure - yield configuration - end - - def self.reset! - @configuration = Configuration.new - end - + # Configuration to adapt behavior. class Configuration + # Allows to override the encoding method. + # By default, ::URI.encode_www_form will be used. + # If overriden, this variable should be set to a `Proc` which will + # receive a single parameter responding to `#to_h` and will return + # a string corresponding to the encoded data. attr_accessor :encoding_method def initialize From 0b7edc5fedc8b60864b0de5ee56d94f5b373f654 Mon Sep 17 00:00:00 2001 From: Fabien Chaynes Date: Wed, 30 May 2018 10:20:24 +0200 Subject: [PATCH 3/5] Remove configuration. --- lib/http/form_data.rb | 20 -------------------- lib/http/form_data/configuration.rb | 19 ------------------- lib/http/form_data/urlencoded.rb | 3 +-- 3 files changed, 1 insertion(+), 41 deletions(-) delete mode 100644 lib/http/form_data/configuration.rb diff --git a/lib/http/form_data.rb b/lib/http/form_data.rb index 60b61d5..be542bc 100644 --- a/lib/http/form_data.rb +++ b/lib/http/form_data.rb @@ -62,26 +62,6 @@ def ensure_hash(obj) end end - # Gets configuration. - def configuration - @configuration ||= Configuration.new - end - - # Sets configuration. - def configuration=(configuration) - @configuration = configuration - end - - # Allows to set configuration through a block. - def configure - yield configuration - end - - # Resets configuration. - def reset! - @configuration = Configuration.new - end - private # Tells whenever data contains multipart data or not. diff --git a/lib/http/form_data/configuration.rb b/lib/http/form_data/configuration.rb deleted file mode 100644 index 8dab8ce..0000000 --- a/lib/http/form_data/configuration.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -module HTTP - module FormData - # Configuration to adapt behavior. - class Configuration - # Allows to override the encoding method. - # By default, ::URI.encode_www_form will be used. - # If overriden, this variable should be set to a `Proc` which will - # receive a single parameter responding to `#to_h` and will return - # a string corresponding to the encoded data. - attr_accessor :encoding_method - - def initialize - @encoding_method = ::URI.method(:encode_www_form) - end - end - end -end diff --git a/lib/http/form_data/urlencoded.rb b/lib/http/form_data/urlencoded.rb index 59270a0..176aa63 100644 --- a/lib/http/form_data/urlencoded.rb +++ b/lib/http/form_data/urlencoded.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require "http/form_data/readable" -require "http/form_data/configuration" require "uri" require "stringio" @@ -33,7 +32,7 @@ def content_type # @param [#to_h, Hash] data form data key-value Hash def encode(data) - HTTP::FormData.configuration.encoding_method.call(data) + ::URI.encode_www_form(FormData.ensure_hash(data)) end end end From 74ecde712954aa3c49b9292074e00e6da2b23d55 Mon Sep 17 00:00:00 2001 From: Fabien Chaynes Date: Wed, 30 May 2018 10:43:37 +0200 Subject: [PATCH 4/5] Allow Urlencoded to be instanciated with an encoder. --- lib/http/form_data.rb | 12 +++++++++--- lib/http/form_data/urlencoded.rb | 9 ++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/http/form_data.rb b/lib/http/form_data.rb index be542bc..3eabae9 100644 --- a/lib/http/form_data.rb +++ b/lib/http/form_data.rb @@ -42,10 +42,16 @@ 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 - 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 From d7a2c3f96a04491ba44adf7cd34a0b4f5bcb3672 Mon Sep 17 00:00:00 2001 From: Fabien Chaynes Date: Wed, 30 May 2018 13:49:36 +0200 Subject: [PATCH 5/5] Expose HTTP::FormData.encoder --- lib/http/form_data.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/http/form_data.rb b/lib/http/form_data.rb index 3eabae9..5104afb 100644 --- a/lib/http/form_data.rb +++ b/lib/http/form_data.rb @@ -51,7 +51,11 @@ def create(data) end def encoder - ::URI.method(:encode_www_form) + @encoder ||= ::URI.method(:encode_www_form) + end + + def encoder=(encoder) + @encoder = encoder end # Coerce `obj` to Hash.