Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

support media type query by type

  • Loading branch information...
commit ee6ce2ed52d27cf66b5c9b381ac65c378fdbd205 1 parent 037d144
@guilhermesilveira guilhermesilveira authored
Showing with 47 additions and 18 deletions.
  1. +31 −18 lib/restfulie/media_type.rb
  2. +16 −0 spec/media_type_spec.rb
View
49 lib/restfulie/media_type.rb
@@ -8,6 +8,11 @@ def media_type(*args)
end
end
+ # returns the list of media types available for this resource
+ def media_types
+ ['application/xml', 'application/json'] + MediaType.medias_for(self)
+ end
+
end
module DefaultMediaTypes
@@ -44,29 +49,37 @@ def self.from_json(json)
module MediaType
- def self.register(name, who)
- media_types[name] = who
- end
+ class << self
- # TODO rename to type for mt
- def self.media_type(name)
- name = normalize(name)
- raise UnsupportedContentType.new("unsupported content type '#{name}'") if media_types[name].nil?
- media_types[name]
- end
+ def register(name, who)
+ media_types[name] = who
+ end
- def self.supports?(name)
- name = normalize(name)
- !media_types[name].nil?
- end
+ # TODO rename to type for mt
+ def media_type(name)
+ name = normalize(name)
+ raise UnsupportedContentType.new("unsupported content type '#{name}'") if media_types[name].nil?
+ media_types[name]
+ end
- def self.normalize(name)
- name[/[^;]*/]
- end
+ def supports?(name)
+ name = normalize(name)
+ !media_types[name].nil?
+ end
+
+ def normalize(name)
+ name[/[^;]*/]
+ end
- def self.media_types
- @media_types ||= {}
+ def media_types
+ @media_types ||= {}
+ end
+
+ def medias_for(type)
+ media_types.dup.delete_if {|key, value| value!=type}.keys
+ end
end
+
end
Restfulie::MediaType.register('application/xml', DefaultMediaTypes)
View
16 spec/media_type_spec.rb
@@ -43,6 +43,22 @@ class City
Restfulie::MediaType.media_type('vnd/caelum_city+json').should eql(City)
end
+ it "should return the list of media types for a specific type" do
+ class City
+ extend Restfulie::MediaTypeControl
+ media_type 'vnd/caelum_city+xml', 'vnd/caelum_city+json'
+ end
+ Restfulie::MediaType.medias_for(City).should eql(['vnd/caelum_city+xml', 'vnd/caelum_city+json'])
+ end
+
+ it "should return the list of media types for a specific type with json and xml support" do
+ class City
+ extend Restfulie::MediaTypeControl
+ media_type 'vnd/caelum_city+xml', 'vnd/caelum_city+json'
+ end
+ City.media_types.should eql(['application/xml','application/json','vnd/caelum_city+xml', 'vnd/caelum_city+json'])
+ end
+
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.