From 02f3e75312f907cea79dfd3b79e8caea10ab53d7 Mon Sep 17 00:00:00 2001 From: Walter Smith Date: Thu, 29 Oct 2015 10:24:11 -0400 Subject: [PATCH 1/2] Fix handling of key/value responses Non-key/value responses that happened to contain a colon (e.g., in a timestamp) were interpreted as key/value responses. --- lib/librevox/response.rb | 2 +- spec/librevox/spec_response.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/librevox/response.rb b/lib/librevox/response.rb index 063a7c6..abac427 100644 --- a/lib/librevox/response.rb +++ b/lib/librevox/response.rb @@ -20,7 +20,7 @@ def headers= headers end def content= content - @content = if content.respond_to?(:match) && content.match(/:/) + @content = if content.respond_to?(:match) && content.match(/\A[\w\-]+:/) headers_2_hash(content).merge(:body => content.split("\n\n", 2)[1].to_s) else content diff --git a/spec/librevox/spec_response.rb b/spec/librevox/spec_response.rb index 21f86b7..d560852 100644 --- a/spec/librevox/spec_response.rb +++ b/spec/librevox/spec_response.rb @@ -22,6 +22,13 @@ response.content[:other_key].should.equal "other value" end + should "parse non-key-value content with colons to string" do + response = Response.new("", "this just has a : in it") + + response.content.class.should.equal String + response.content.should.equal "this just has a : in it" + end + should "not parse regular content" do response = Response.new("", "OK.") From 7fdd3a87bff2550666c57b6b6fef03bea05e143d Mon Sep 17 00:00:00 2001 From: Walter Smith Date: Thu, 29 Oct 2015 10:24:50 -0400 Subject: [PATCH 2/2] Handle CSV responses E.g., "show calls". --- lib/librevox/response.rb | 9 +++++++++ spec/librevox/spec_response.rb | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/librevox/response.rb b/lib/librevox/response.rb index abac427..9e18690 100644 --- a/lib/librevox/response.rb +++ b/lib/librevox/response.rb @@ -22,6 +22,8 @@ def headers= headers def content= content @content = if content.respond_to?(:match) && content.match(/\A[\w\-]+:/) headers_2_hash(content).merge(:body => content.split("\n\n", 2)[1].to_s) + elsif content.respond_to?(:match) && content.match(/\A\w+,\w/) + csv_2_hashes(content.split("\n\n", 2)[0].to_s) else content end @@ -48,5 +50,12 @@ def command_reply? def headers_2_hash *args EM::Protocols::HeaderAndContentProtocol.headers_2_hash *args end + + def csv_2_hashes content + lines = content.chomp.split("\n") + return [] if lines.length == 0 + colnames = lines[0].split(",").map(&:to_sym) + lines[1..-1].map { |l| Hash[* colnames.zip(l.split(",")).flatten] } + end end end diff --git a/spec/librevox/spec_response.rb b/spec/librevox/spec_response.rb index d560852..dae6b2a 100644 --- a/spec/librevox/spec_response.rb +++ b/spec/librevox/spec_response.rb @@ -29,6 +29,16 @@ response.content.should.equal "this just has a : in it" end + should "parse CSV content to array of hashes" do + response = Response.new("Header1: some value", "a,b\nw,x\ny,z\n\n2 total.\n") + + response.headers.should.include :header1 + response.headers[:header1].should.equal "some value" + + response.content.class.should.equal Array + response.content.should.equal [{a: "w", b: "x"}, {a: "y", b: "z"}] + end + should "not parse regular content" do response = Response.new("", "OK.")