From 92a25a7a10c550403b40c0a07e70693789cba242 Mon Sep 17 00:00:00 2001 From: Adam Wiggins Date: Sat, 24 Jan 2009 14:28:05 -0800 Subject: [PATCH] beautify headers (:content_type => 'text/html') --- lib/rest_client.rb | 9 ++++++++- spec/rest_client_spec.rb | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/rest_client.rb b/lib/rest_client.rb index c6c40c1..a03c1de 100644 --- a/lib/rest_client.rb +++ b/lib/rest_client.rb @@ -270,7 +270,14 @@ module RestClient end def headers - @headers ||= @net_http_res.to_hash + @headers ||= self.class.beautify_headers(@net_http_res.to_hash) + end + + def self.beautify_headers(headers) + headers.inject({}) do |out, (key, value)| + out[key.gsub(/-/, '_').to_sym] = value.first + out + end end end end diff --git a/spec/rest_client_spec.rb b/spec/rest_client_spec.rb index 192cb80..f80d02d 100644 --- a/spec/rest_client_spec.rb +++ b/spec/rest_client_spec.rb @@ -356,9 +356,19 @@ describe RestClient do @response.code.should == 200 end + it "beautifies the headers by turning the keys to symbols" do + h = RestClient::Response.beautify_headers('content-type' => [ 'x' ]) + h.keys.first.should == :content_type + end + + it "beautifies the headers by turning the values to strings instead of one-element arrays" do + h = RestClient::Response.beautify_headers('x' => [ 'text/html' ] ) + h.values.first.should == 'text/html' + end + it "fetches the headers" do - @net_http_res.should_receive(:to_hash).and_return('a' => ['b']) - @response.headers['a'].should == ['b'] + @net_http_res.should_receive(:to_hash).and_return('content-type' => [ 'text/html' ]) + @response.headers.should == { :content_type => 'text/html' } end it "can access the net http result directly" do