Skip to content

Commit

Permalink
adding content type plain text support and serialise action for txt
Browse files Browse the repository at this point in the history
  • Loading branch information
buchin authored and achamian committed Dec 9, 2011
1 parent 6d695e2 commit 419f484
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/wrest/components/translators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 2 additions & 1 deletion lib/wrest/components/translators/content_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 31 additions & 0 deletions lib/wrest/components/translators/txt.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions spec/wrest/components/translators/txt_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 419f484

Please sign in to comment.