Module: Resto::Format

Included in:
Default, Json, Plain, Xml
Defined in:
lib/resto/format.rb,
lib/resto/format/xml.rb,
lib/resto/format/json.rb,
lib/resto/format/plain.rb,
lib/resto/format/default.rb

Overview

Note:

This module is only used internally by these classes/modules: Resto::Request::Base (see method Request::Header#format). Resto::Response::Base (see method Response::Base#format).

Add a Format

If a user of Resto wants to handle a format different from whats available, she can create a class in the Format module. Then include the Format module into the meta class and override the methods that she wants to change. See example below.

Example:

module Resto
  module Format
    class Foo; end

    class << Foo
      include Format

      def extension;         'foo'           end
      def accept;            'foo';          end
    end
  end
end

Defined Under Namespace

Classes: Default, Json, Plain, Xml

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get(symbol = :default) ⇒ Format, ...

Returns the class that corresponds to the symbol argument.

Examples:

Resto::Format.get(:json)
 # => Resto::Format::Json

Resto::Format.get(:xml)
 # => Resto::Format:Xml

Parameters:

  • symbol (Symbol) (defaults to: :default)

Returns:

Raises:

  • (NameError)

    if the class doesn’t exist.



51
52
53
# File 'lib/resto/format.rb', line 51

def self.get(symbol=:default)
  Resto::Format.const_get("#{symbol.to_s.capitalize}")
end

Instance Method Details

#acceptString

The accept header. This method is overriden by the classes that includes this module if they want a different accept header.

Returns:

  • (String)

    /



59
# File 'lib/resto/format.rb', line 59

def accept; '*/*'; end

#content_typenil

The content-type header. This method is overriden by the classes that includes this module if they want a different content-type header.

Returns:

  • (nil)


65
# File 'lib/resto/format.rb', line 65

def content_type; end

#decode(*args) ⇒ Array<Object>

Returns the arguments as an Array. This method is overriden by classes that includes this module if they want a different behavior.

Returns:

  • (Array<Object>)

    the arguments as an Array.



71
# File 'lib/resto/format.rb', line 71

def decode(*args); args end

#encode(*args) ⇒ Object

Returns the first argument. This method is overriden by classes that includes this module if they want a different behavior.

Returns:

  • (Object)

    the first argument.



77
# File 'lib/resto/format.rb', line 77

def encode(*args); args.first end

#extensionnil

The extension. This method is overriden by the classes that includes this module if they want to have an extension.

Returns:

  • (nil)


83
# File 'lib/resto/format.rb', line 83

def extension; end