diff --git a/lib/wrest/components/translators.rb b/lib/wrest/components/translators.rb index 663c5b3b..51b62269 100644 --- a/lib/wrest/components/translators.rb +++ b/lib/wrest/components/translators.rb @@ -23,6 +23,7 @@ def self.lookup(content_type) end end +require "wrest/components/translators/txt" require "wrest/components/translators/xml" require "wrest/components/translators/json" require "wrest/components/translators/content_types" diff --git a/lib/wrest/components/translators/content_types.rb b/lib/wrest/components/translators/content_types.rb index 41a4f2fb..e1dc9bbf 100644 --- a/lib/wrest/components/translators/content_types.rb +++ b/lib/wrest/components/translators/content_types.rb @@ -14,7 +14,8 @@ module Components::Translators 'application/xml' => Wrest::Components::Translators::Xml, 'text/xml' => Wrest::Components::Translators::Xml, 'application/json' => Wrest::Components::Translators::Json, - 'text/javascript' => Wrest::Components::Translators::Json + 'text/javascript' => Wrest::Components::Translators::Json, + 'text/plain' => Wrest::Components::Translators::Txt } end end \ No newline at end of file diff --git a/lib/wrest/components/translators/txt.rb b/lib/wrest/components/translators/txt.rb new file mode 100644 index 00000000..29a53cf0 --- /dev/null +++ b/lib/wrest/components/translators/txt.rb @@ -0,0 +1,31 @@ +# Copyright 2009 Sidu Ponnappa + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software distributed under the License +# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and limitations under the License. +module Wrest + module Components::Translators + module Txt + extend self + + def deserialise(response,options={}) + response.body + end + + def deserialize(response, options = {}) + deserialise(response, options) + end + + def serialise(hash, options = {}) + hash.inspect + end + + def serialize(hash, options = {}) + serialise(hash, options) + end + end + end +end diff --git a/spec/wrest/components/translators/txt_spec.rb b/spec/wrest/components/translators/txt_spec.rb new file mode 100644 index 00000000..fdd01043 --- /dev/null +++ b/spec/wrest/components/translators/txt_spec.rb @@ -0,0 +1,26 @@ +require "spec_helper" + +module Wrest::Components::Translators + describe Txt do + let(:http_response) { mock('Http Reponse') } + it "should return response body when deserialise" do + http_response.should_receive(:body).and_return("Homebrew is the easiest.") + + Txt.deserialise(http_response).should == "Homebrew is the easiest." + end + + it "should return string version of any object when serialise" do + Txt.serialise({"ooga"=>{"age" => "12"}}).should == "{\"ooga\"=>{\"age\"=>\"12\"}}" + end + + it "has #deserialize delegate to #deserialise" do + Txt.should_receive(:deserialise).with(http_response, :option => :something) + Txt.deserialize(http_response, :option => :something) + end + + it "has #serialize delegate to #serialise" do + Txt.should_receive(:serialise).with({ :hash => :foo }, :option => :something) + Txt.serialize({:hash => :foo}, :option => :something) + end + end +end