From f076b1cecb767e88f7bd73f80392c43291ddabbf Mon Sep 17 00:00:00 2001 From: Ari Summer Date: Mon, 2 Mar 2020 17:15:45 -0700 Subject: [PATCH 1/5] Add ability to pass encoder to HTTP::FormData::Urlencoded instance --- lib/http/form_data/urlencoded.rb | 5 +++-- spec/lib/http/form_data/urlencoded_spec.rb | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/http/form_data/urlencoded.rb b/lib/http/form_data/urlencoded.rb index dc47b94..f5e583a 100644 --- a/lib/http/form_data/urlencoded.rb +++ b/lib/http/form_data/urlencoded.rb @@ -63,8 +63,9 @@ def encoder end # @param [#to_h, Hash] data form data key-value Hash - def initialize(data) - @io = StringIO.new(self.class.encoder.call(FormData.ensure_hash(data))) + def initialize(data, encoder = nil) + encoder ||= self.class.encoder + @io = StringIO.new(encoder.call(FormData.ensure_hash(data))) end # Returns MIME type to be used for HTTP request `Content-Type` header. diff --git a/spec/lib/http/form_data/urlencoded_spec.rb b/spec/lib/http/form_data/urlencoded_spec.rb index a3de752..de68b06 100644 --- a/spec/lib/http/form_data/urlencoded_spec.rb +++ b/spec/lib/http/form_data/urlencoded_spec.rb @@ -64,4 +64,13 @@ expect(form_data.to_s).to eq('{"foo[bar]":"test"}') end end + + context "with custom instance level encoder" do + let(:encoder) { Proc.new { |data| ::JSON.dump(data) } } + subject(:form_data) { HTTP::FormData::Urlencoded.new(data, encoder) } + + it "uses encoder passed to initializer" do + expect(form_data.to_s).to eq('{"foo[bar]":"test"}') + end + end end From fb8e309a21c884efbc4a7afbbd2aec9687fb0f79 Mon Sep 17 00:00:00 2001 From: Ari Summer Date: Mon, 2 Mar 2020 17:28:33 -0700 Subject: [PATCH 2/5] Add ability to pass encoder to FormData.create --- lib/http/form_data.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/http/form_data.rb b/lib/http/form_data.rb index be542bc..4effcfc 100644 --- a/lib/http/form_data.rb +++ b/lib/http/form_data.rb @@ -41,11 +41,14 @@ class << self # @param [#to_h, Hash] data # @return [Multipart] if any of values is a {FormData::File} # @return [Urlencoded] otherwise - def create(data) + def create(data, encoder: nil) data = ensure_hash data - klass = multipart?(data) ? Multipart : Urlencoded - klass.new data + if multipart?(data) + Multipart.new(data) + else + Urlencoded.new(data, encoder) + end end # Coerce `obj` to Hash. From 0384419295c0374e36f2deec963943809b910ad4 Mon Sep 17 00:00:00 2001 From: Ari Summer Date: Mon, 2 Mar 2020 17:48:51 -0700 Subject: [PATCH 3/5] Fix syntax issues --- lib/http/form_data.rb | 2 +- spec/lib/http/form_data/urlencoded_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/http/form_data.rb b/lib/http/form_data.rb index 4effcfc..03da28d 100644 --- a/lib/http/form_data.rb +++ b/lib/http/form_data.rb @@ -42,7 +42,7 @@ class << self # @return [Multipart] if any of values is a {FormData::File} # @return [Urlencoded] otherwise def create(data, encoder: nil) - data = ensure_hash data + data = ensure_hash data if multipart?(data) Multipart.new(data) diff --git a/spec/lib/http/form_data/urlencoded_spec.rb b/spec/lib/http/form_data/urlencoded_spec.rb index de68b06..2584b25 100644 --- a/spec/lib/http/form_data/urlencoded_spec.rb +++ b/spec/lib/http/form_data/urlencoded_spec.rb @@ -66,7 +66,7 @@ end context "with custom instance level encoder" do - let(:encoder) { Proc.new { |data| ::JSON.dump(data) } } + let(:encoder) { proc { |data| ::JSON.dump(data) } } subject(:form_data) { HTTP::FormData::Urlencoded.new(data, encoder) } it "uses encoder passed to initializer" do From 3083334c759e83b8ae7fb1ef803c28807803695b Mon Sep 17 00:00:00 2001 From: Ari Summer Date: Tue, 3 Mar 2020 12:24:00 -0700 Subject: [PATCH 4/5] Use keyword argument for encoder in HTTP::FormData::Urlencoded#initialize for consistency --- lib/http/form_data.rb | 2 +- lib/http/form_data/urlencoded.rb | 2 +- spec/lib/http/form_data/urlencoded_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/http/form_data.rb b/lib/http/form_data.rb index 03da28d..0ac92a7 100644 --- a/lib/http/form_data.rb +++ b/lib/http/form_data.rb @@ -47,7 +47,7 @@ def create(data, encoder: nil) if multipart?(data) Multipart.new(data) else - Urlencoded.new(data, encoder) + Urlencoded.new(data, encoder: encoder) end end diff --git a/lib/http/form_data/urlencoded.rb b/lib/http/form_data/urlencoded.rb index f5e583a..9ebcaf0 100644 --- a/lib/http/form_data/urlencoded.rb +++ b/lib/http/form_data/urlencoded.rb @@ -63,7 +63,7 @@ def encoder end # @param [#to_h, Hash] data form data key-value Hash - def initialize(data, encoder = nil) + def initialize(data, encoder: nil) encoder ||= self.class.encoder @io = StringIO.new(encoder.call(FormData.ensure_hash(data))) end diff --git a/spec/lib/http/form_data/urlencoded_spec.rb b/spec/lib/http/form_data/urlencoded_spec.rb index 2584b25..8f646c0 100644 --- a/spec/lib/http/form_data/urlencoded_spec.rb +++ b/spec/lib/http/form_data/urlencoded_spec.rb @@ -67,7 +67,7 @@ context "with custom instance level encoder" do let(:encoder) { proc { |data| ::JSON.dump(data) } } - subject(:form_data) { HTTP::FormData::Urlencoded.new(data, encoder) } + subject(:form_data) { HTTP::FormData::Urlencoded.new(data, encoder: encoder) } it "uses encoder passed to initializer" do expect(form_data.to_s).to eq('{"foo[bar]":"test"}') From 518d8209cb1a040fdb5822f7128b2d3aedee5a12 Mon Sep 17 00:00:00 2001 From: Ari Summer Date: Tue, 3 Mar 2020 12:46:17 -0700 Subject: [PATCH 5/5] Fix syntax issues - Use ruby 1.9 hash syntax - Reduce line length in spec --- lib/http/form_data.rb | 2 +- spec/lib/http/form_data/urlencoded_spec.rb | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/http/form_data.rb b/lib/http/form_data.rb index 0ac92a7..e993161 100644 --- a/lib/http/form_data.rb +++ b/lib/http/form_data.rb @@ -47,7 +47,7 @@ def create(data, encoder: nil) if multipart?(data) Multipart.new(data) else - Urlencoded.new(data, encoder: encoder) + Urlencoded.new(data, :encoder => encoder) end end diff --git a/spec/lib/http/form_data/urlencoded_spec.rb b/spec/lib/http/form_data/urlencoded_spec.rb index 8f646c0..649e3b6 100644 --- a/spec/lib/http/form_data/urlencoded_spec.rb +++ b/spec/lib/http/form_data/urlencoded_spec.rb @@ -67,7 +67,9 @@ context "with custom instance level encoder" do let(:encoder) { proc { |data| ::JSON.dump(data) } } - subject(:form_data) { HTTP::FormData::Urlencoded.new(data, encoder: encoder) } + subject(:form_data) do + HTTP::FormData::Urlencoded.new(data, :encoder => encoder) + end it "uses encoder passed to initializer" do expect(form_data.to_s).to eq('{"foo[bar]":"test"}')