Sha256: f3be859f0b3d871d5298632043ebf186d342a16e794478aee65839cd19aa3af9

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

module Restfulie
  module Common
    module Converter
      Dir["#{File.dirname(__FILE__)}/converter/*.rb"].each {|f| autoload File.basename(f)[0..-4].camelize.to_sym, f }

      # Returns the default root element name for an item or collection
      def self.root_element_for(obj)
        if obj.kind_of?(Hash) && obj.size==1
          obj.keys.first.to_s
        elsif obj.kind_of?(Array) && !obj.empty?
          root_element_for(obj.first).to_s.underscore.pluralize
        else
          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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
restfulie-0.1.0.beta1 lib/restfulie/common/converter.rb