diff --git a/lib/discourse_data_explorer/report_generator.rb b/lib/discourse_data_explorer/report_generator.rb index cc85d48..b014d01 100644 --- a/lib/discourse_data_explorer/report_generator.rb +++ b/lib/discourse_data_explorer/report_generator.rb @@ -35,26 +35,10 @@ module ::DiscourseDataExplorer build_report_post(query, table, attach_csv: opts[:attach_csv], result:) end - private - def self.params_to_hash(query_params) params = JSON.parse(query_params) - params_hash = {} - if !params.blank? - param_key, param_value = [], [] - params.flatten.each.with_index do |data, i| - if i % 2 == 0 - param_key << data - else - param_value << data - end - end - - params_hash = Hash[param_key.zip(param_value)] - end - - params_hash + params.map { |p| p.is_a?(Hash) ? [p["key"], p["value"]] : p }.to_h end def self.build_report_pms(query, table = "", targets = [], attach_csv: false, result: nil) diff --git a/spec/report_generator_spec.rb b/spec/report_generator_spec.rb index 86198da..6c28bda 100644 --- a/spec/report_generator_spec.rb +++ b/spec/report_generator_spec.rb @@ -253,4 +253,24 @@ describe DiscourseDataExplorer::ReportGenerator do ) end end + + describe ".params_to_hash" do + context "when passing nothing" do + let(:query_params) { "[]" } + + it { expect(described_class.params_to_hash(query_params)).to eq({}) } + end + + context "when passing an array of arrays" do + let(:query_params) { '[["foo", 1], ["bar", 2]]' } + + it { expect(described_class.params_to_hash(query_params)).to eq({ "foo" => 1, "bar" => 2 }) } + end + + context "when passing an array of hashes" do + let(:query_params) { '[{ "key": "foo", "value": 1 }, { "key": "bar", "value": 2 }]' } + + it { expect(described_class.params_to_hash(query_params)).to eq({ "foo" => 1, "bar" => 2 }) } + end + end end