Module: OptinParsing::ControllerAdditions::ClassMethods

Defined in:
lib/optin_parsing/controller_additions.rb

Instance Method Summary collapse

Instance Method Details

#parses(mime_type_or_short_cut, options = {}, &block) ⇒ Object

Declares that you want to parse a specific body type, for this controller and its subclasses You can either pass the symbols :xml or :json or an instance of Mime::Type. If you pass a Mime::Type you must also supply a block. The block will be passed the request raw post data and should return a hash of parsed parameter data

You can also supply a hash of options containing the keys :except or :only to restrict which actions will be parsed

Parameters:

  • mime_type_or_short_cut (Symbol, Mime::Type)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :except (Array, Symbol)

    A list of actions for which parsing should not be enabled

  • :only (Array, Symbol)

    A list of actions for which parsing should be enabled



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/optin_parsing/controller_additions.rb', line 25

def parses mime_type_or_short_cut, options={}, &block

  case mime_type_or_short_cut
  when Mime::Type
    raise ArgumentError, "You must supply a block when specifying a mime type" unless block
    self.parse_strategies = parse_strategies.merge(mime_type_or_short_cut => [block, normalize_optin_options(options)])
  when :xml
    self.parse_strategies = parse_strategies.merge(Mime::XML => [:xml, normalize_optin_options(options)])
  when :json
    self.parse_strategies = parse_strategies.merge(Mime::JSON => [:json, normalize_optin_options(options)])
  end
end