Skip to content
This repository was archived by the owner on Nov 10, 2022. It is now read-only.
Open
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
11 changes: 10 additions & 1 deletion lib/librevox/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ 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)
elsif content.respond_to?(:match) && content.match(/\A\w+,\w/)
csv_2_hashes(content.split("\n\n", 2)[0].to_s)
else
content
end
Expand All @@ -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
17 changes: 17 additions & 0 deletions spec/librevox/spec_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@
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 "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.")

Expand Down