Module: ApiResource::Formats

Defined in:
lib/api_resource/formats.rb,
lib/api_resource/formats/xml_format.rb,
lib/api_resource/formats/json_format.rb

Defined Under Namespace

Modules: JsonFormat, XmlFormat Classes: BadFormat

Class Method Summary collapse

Class Method Details

.[](mime_type_reference) ⇒ Object

Lookup the format class from a mime type reference symbol. Example:

ActiveResource::Formats[:xml]  # => ActiveResource::Formats::XmlFormat
ActiveResource::Formats[:json] # => ActiveResource::Formats::JsonFormat


10
11
12
13
14
15
16
17
# File 'lib/api_resource/formats.rb', line 10

def self.[](mime_type_reference)
  format_name = ActiveSupport::Inflector.camelize(mime_type_reference.to_s) + "Format"
  begin
    ApiResource::Formats.const_get(format_name)
  rescue NameError => e
    raise BadFormat.new("#{mime_type_reference} is not a valid format")
  end
end