Skip to content

Commit

Permalink
moving converter registration into a new converter module
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermesilveira committed Oct 24, 2010
1 parent aaa068e commit 4faee92
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
24 changes: 3 additions & 21 deletions lib/restfulie/client/http/request_marshaller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,9 @@ def initialize(requester)
def raw?
@raw
end

@@representations = {
'application/atom+xml' => ::Restfulie::Common::Converter::Atom,
'application/xml' => ::Restfulie::Common::Converter::Xml,
'text/xml' => ::Restfulie::Common::Converter::Xml,
'application/json' => ::Restfulie::Common::Converter::Json,
'application/opensearchdescriptor+xml' => ::Restfulie::Common::Converter::OpenSearch
}

def self.register_representation(media_type,representation)
@@representations[media_type] = representation
end

def self.content_type_for(media_type)
return nil unless media_type
content_type = media_type.split(';')[0] # [/(.*?);/, 1]
@@representations[content_type]
end

def accepts(media_types)
@default_representation = @@representations[media_types]
@default_representation = Restfulie::Common::Converter.content_type_for(media_types)
delegate(:accepts, media_types)
end

Expand All @@ -55,15 +37,15 @@ def request!(method, path, *args)
rel = self.respond_to?(:rel) ? self.rel : ""
type = headers['Content-Type']
raise Restfulie::Common::Error::RestfulieError, "Missing content type related to the data to be submitted" unless type
marshaller = RequestMarshaller.content_type_for(type)
marshaller = Restfulie::Common::Converter.content_type_for(type)
raise Restfulie::Common::Error::RestfulieError, "Missing content type for #{type} related to the data to be submitted" unless marshaller
payload = marshaller.marshal(payload, { :rel => rel, :recipe => recipe }) unless payload.nil? || (payload.kind_of?(String) && payload.empty?)
args = set_marshalled_payload(method, path, payload, *args)
args = add_representation_headers(method, path, marshaller, *args)
end

if @acceptable_mediatypes
unmarshaller = RequestMarshaller.content_type_for(@acceptable_mediatypes)
unmarshaller = Restfulie::Common::Converter.content_type_for(@acceptable_mediatypes)
args = add_representation_headers(method, path, unmarshaller, *args)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/restfulie/client/http/response_holder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module ResponseHolder

def resource
type = headers['content-type'] || response['Content-Type']
representation = Restfulie::Client::HTTP::RequestMarshaller.content_type_for(type[0]) || Restfulie::Common::Representation::Generic.new
representation = Restfulie::Common::Converter.content_type_for(type[0]) || Restfulie::Common::Representation::Generic.new
representation.unmarshal(response.body)
end

Expand Down
24 changes: 24 additions & 0 deletions lib/restfulie/common/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@ def self.root_element_for(obj)
obj.class.to_s.underscore
end
end

def self.register(media_type,representation)
representations[media_type] = representation
end

def self.content_type_for(media_type)
return nil unless media_type
content_type = media_type.split(';')[0] # [/(.*?);/, 1]
representations[content_type]
end

private

def self.representations
@representations ||= {}
end

register 'application/atom+xml' , ::Restfulie::Common::Converter::Atom
register 'application/xml' , ::Restfulie::Common::Converter::Xml
register 'text/xml' , ::Restfulie::Common::Converter::Xml
register 'application/json' , ::Restfulie::Common::Converter::Json
register 'application/opensearchdescription+xml' , ::Restfulie::Common::Converter::OpenSearch


end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Tokamak < ::ActionView::TemplateHandler
include ::ActionView::TemplateHandlers::Compilable

def compile(template)
"@restfulie_type_helpers = Restfulie::Client::HTTP::RequestMarshaller.content_type_for(self.response.content_type).helper; " +
"@restfulie_type_helpers = Restfulie::Common::Converter.content_type_for(self.response.content_type).helper; " +
"extend @restfulie_type_helpers; " +
"extend Restfulie::Server::ActionView::Helpers; " +
"code_block = lambda { #{template.source} };" +
Expand Down

0 comments on commit 4faee92

Please sign in to comment.