Class: ActionDispatch::Integration::Session::RequestEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/action_dispatch/testing/integration.rb

Overview

:nodoc:

Constant Summary collapse

WWWFormEncoder =
new(:url_encoded_form, -> params { params }, nil, true)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mime_name, param_encoder, response_parser, url_encoded_form = false) ⇒ RequestEncoder

Returns a new instance of RequestEncoder.



404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/action_dispatch/testing/integration.rb', line 404

def initialize(mime_name, param_encoder, response_parser, url_encoded_form = false)
  @mime = Mime[mime_name]

  unless @mime
    raise ArgumentError, "Can't register a request encoder for " \
      "unregistered MIME Type: #{mime_name}. See `Mime::Type.register`."
  end

  @url_encoded_form = url_encoded_form
  @path_format      = ".#{@mime.symbol}" unless @url_encoded_form
  @response_parser  = response_parser || -> body { body }
  @param_encoder    = param_encoder   || :"to_#{@mime.symbol}".to_proc
end

Instance Attribute Details

#response_parserObject (readonly)

Returns the value of attribute response_parser.



402
403
404
# File 'lib/action_dispatch/testing/integration.rb', line 402

def response_parser
  @response_parser
end

Class Method Details

.encoder(name) ⇒ Object



436
437
438
# File 'lib/action_dispatch/testing/integration.rb', line 436

def self.encoder(name)
  @encoders[name] || WWWFormEncoder
end

.parser(content_type) ⇒ Object



431
432
433
434
# File 'lib/action_dispatch/testing/integration.rb', line 431

def self.parser(content_type)
  mime = Mime::Type.lookup(content_type)
  encoder(mime ? mime.ref : nil).response_parser
end

.register_encoder(mime_name, param_encoder: nil, response_parser: nil) ⇒ Object



440
441
442
# File 'lib/action_dispatch/testing/integration.rb', line 440

def self.register_encoder(mime_name, param_encoder: nil, response_parser: nil)
  @encoders[mime_name] = new(mime_name, param_encoder, response_parser)
end

Instance Method Details

#append_format_to(path) ⇒ Object



418
419
420
421
# File 'lib/action_dispatch/testing/integration.rb', line 418

def append_format_to(path)
  path << @path_format unless @url_encoded_form
  path
end

#content_typeObject



423
424
425
# File 'lib/action_dispatch/testing/integration.rb', line 423

def content_type
  @mime.to_s
end

#encode_params(params) ⇒ Object



427
428
429
# File 'lib/action_dispatch/testing/integration.rb', line 427

def encode_params(params)
  @param_encoder.call(params)
end