Class: ActionDispatch::RequestEncoder

Inherits:
Object
  • Object
show all
Defined in:
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.



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

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.



12
13
14
# File 'lib/action_dispatch/testing/request_encoder.rb', line 12

def response_parser
  @response_parser
end

Class Method Details

.encoder(name) ⇒ Object



43
44
45
# File 'lib/action_dispatch/testing/request_encoder.rb', line 43

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

.parser(content_type) ⇒ Object



38
39
40
41
# File 'lib/action_dispatch/testing/request_encoder.rb', line 38

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



47
48
49
# File 'lib/action_dispatch/testing/request_encoder.rb', line 47

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



30
31
32
# File 'lib/action_dispatch/testing/request_encoder.rb', line 30

def accept_header
  @mime.to_s
end

#content_typeObject



26
27
28
# File 'lib/action_dispatch/testing/request_encoder.rb', line 26

def content_type
  @mime.to_s
end

#encode_params(params) ⇒ Object



34
35
36
# File 'lib/action_dispatch/testing/request_encoder.rb', line 34

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