Skip to content

Commit

Permalink
fixed custom type
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermesilveira committed Dec 19, 2009
1 parent 2c6273f commit bfea79d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 27 deletions.
1 change: 1 addition & 0 deletions lib/restfulie.rb
Expand Up @@ -12,6 +12,7 @@
require 'restfulie/server/marshalling'
require 'restfulie/server/transition'
require 'restfulie/server/restfulie_controller'
require 'restfulie/server/atom_media_type'

module Restfulie

Expand Down
42 changes: 25 additions & 17 deletions lib/restfulie/media_type.rb
Expand Up @@ -4,7 +4,21 @@ module Restfulie

# TODO rename it and move it
def self.default_types
[Type.new('application/xml', self), Type.new('application/json', self), Type.new('xml', self), Type.new('json', self), CustomExecutionType.new('html', self, lambda {})]
[Type.new('html', self, lambda {}),
rendering_type('application/xml', self),
rendering_type('application/json', self),
rendering_type('xml', self),
rendering_type('json', self)]
end

# TODO remove this nasty method
def self.rendering_type(name, type)
Type.new(name,type)
end

# TODO remove this method
def self.custom_type(name, type, l)
CustomType.new(name, type, l)
end

class Type
Expand All @@ -13,38 +27,31 @@ def initialize(name, type)
@name = name
@type = type
end

def short_name
name.gsub(/\//,'_').gsub(/\+/,'_')
end

def format_name
name[/(.*[\+\/])?(.*)/,2]
end

# server side only (move it)
def execute_for(controller, resource, options, render_options)
formatted_resource = ["xml", "json"].include?(format_name) ? resource.send(:"to_#{format_name}", options) : resource
render_options[:text] = formatted_resource
render_options[:content_type] = name
controller.render render_options
end

end

# TODO should be refactored: Type should be a type that receives a block, serializing type should contain serialization
class CustomExecutionType < Type
attr_reader :name, :type
def initialize(name, type, lambda)
@name = name
@type = type
@lambda = lambda
class CustomType < Type
def initialize(name, type, l)
super(name, type)
@lambda = l
end
def execute_for(controller, resource, options, render_options)
@lambda.call
end
end

module DefaultMediaTypes

# from rails source code
Expand Down Expand Up @@ -77,10 +84,11 @@ def self.from_json(json)

end

Restfulie::MediaType.register(Type.new('application/xml', DefaultMediaTypes))
Restfulie::MediaType.register(Type.new('application/json', DefaultMediaTypes))
Restfulie::MediaType.register(Type.new('xml', DefaultMediaTypes))
Restfulie::MediaType.register(Type.new('json', DefaultMediaTypes))
Restfulie::MediaType.register(rendering_type('text/html', DefaultMediaTypes))
Restfulie::MediaType.register(rendering_type('application/xml', DefaultMediaTypes))
Restfulie::MediaType.register(rendering_type('application/json', DefaultMediaTypes))
Restfulie::MediaType.register(rendering_type('xml', DefaultMediaTypes))
Restfulie::MediaType.register(rendering_type('json', DefaultMediaTypes))


end
Expand Down
2 changes: 1 addition & 1 deletion lib/restfulie/media_type_control.rb
Expand Up @@ -4,7 +4,7 @@ module MediaTypeControl

def media_type(*args)
args.each do |name|
type = Restfulie::Type.new(name, self)
type = Restfulie.rendering_type(name, self)
Restfulie::MediaType.register(type)
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/restfulie/server/atom_media_type.rb
@@ -0,0 +1,3 @@
module Restfulie

end
18 changes: 9 additions & 9 deletions spec/media_type_spec.rb
Expand Up @@ -57,12 +57,12 @@ class City
extend Restfulie::MediaTypeControl
media_type 'vnd/caelum_city+xml', 'vnd/caelum_city+json'
end
types = ['application/xml','application/json','xml','json','vnd/caelum_city+xml', 'vnd/caelum_city+json']
types = ['html','application/xml','application/json','xml','json','vnd/caelum_city+xml', 'vnd/caelum_city+json']
verify(City.media_types, types)
end

def verify(what, expected)
types = expected.map do |key| Restfulie::Type.new(key, City) end
types = expected.map do |key| Restfulie.rendering_type(key, City) end
types.each_with_index do |t, i|
what[i].name.should eql(t.name)
what[i].type.should eql(t.type)
Expand All @@ -73,7 +73,7 @@ def verify(what, expected)
it "should invoke lambda if its a custom type" do
l = mock Object
l.should_receive :call
type= Restfulie::CustomExecutionType.new('name',Object, l)
type= Restfulie.custom_type('name',Object, l)
render_options = {}
resource = Object.new
options = Object.new
Expand All @@ -87,7 +87,7 @@ def verify(what, expected)
resource = Object.new
options = Object.new
controller = Object.new
type = Restfulie::Type.new('content-type', String)
type = Restfulie.rendering_type('content-type', String)
resource.should_receive(:to_xml).with(options).and_return('content')
type.should_receive(:format_name).at_least(1).and_return('xml')
controller.should_receive(:render).with(render_options)
Expand All @@ -102,7 +102,7 @@ def verify(what, expected)
resource = Object.new
options = Object.new
controller = Object.new
type = Restfulie::Type.new('content-type', String)
type = Restfulie.rendering_type('content-type', String)
type.should_receive(:format_name).at_least(1).and_return('else')
controller.should_receive(:render).with(render_options)
type.execute_for(controller, resource, options, render_options)
Expand All @@ -115,19 +115,19 @@ def verify(what, expected)
context Restfulie::Type do

it "should translate / and + to _ when generating the short name" do
Restfulie::Type.new('vnd/city+xml',String).short_name.should eql('vnd_city_xml')
Restfulie.rendering_type('vnd/city+xml',String).short_name.should eql('vnd_city_xml')
end

it "should retrieve the format from the last part of the media type" do
Restfulie::Type.new('vnd/city+xml',String).format_name.should eql('xml')
Restfulie.rendering_type('vnd/city+xml',String).format_name.should eql('xml')
end

it "should retrieve the format if there is no +" do
Restfulie::Type.new('xml',String).format_name.should eql('xml')
Restfulie.rendering_type('xml',String).format_name.should eql('xml')
end

it "should retrieve the format if there is a /" do
Restfulie::Type.new('application/xml',String).format_name.should eql('xml')
Restfulie.rendering_type('application/xml',String).format_name.should eql('xml')
end

end
Expand Down
Empty file.

0 comments on commit bfea79d

Please sign in to comment.