Class: ActionDispatch::RequestEncoder

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

Overview

:nodoc:

Defined Under Namespace

Classes: IdentityEncoder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mime_name, param_encoder, response_parser) ⇒ RequestEncoder

Returns a new instance of RequestEncoder.



16
17
18
19
20
21
22
23
24
25
26
# File 'actionpack/lib/action_dispatch/testing/request_encoder.rb', line 16

def initialize(mime_name, param_encoder, response_parser)
  @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

  @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



14
15
16
# File 'actionpack/lib/action_dispatch/testing/request_encoder.rb', line 14

def response_parser
  @response_parser
end

Class Method Details

.encoder(name) ⇒ Object



45
46
47
# File 'actionpack/lib/action_dispatch/testing/request_encoder.rb', line 45

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

.parser(content_type) ⇒ Object



40
41
42
43
# File 'actionpack/lib/action_dispatch/testing/request_encoder.rb', line 40

def self.parser(content_type)
  type = Mime::Type.lookup(content_type).ref if content_type
  encoder(type).response_parser
end

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



49
50
51
# File 'actionpack/lib/action_dispatch/testing/request_encoder.rb', line 49

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

#accept_headerObject



32
33
34
# File 'actionpack/lib/action_dispatch/testing/request_encoder.rb', line 32

def accept_header
  @mime.to_s
end

#content_typeObject



28
29
30
# File 'actionpack/lib/action_dispatch/testing/request_encoder.rb', line 28

def content_type
  @mime.to_s
end

#encode_params(params) ⇒ Object



36
37
38
# File 'actionpack/lib/action_dispatch/testing/request_encoder.rb', line 36

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