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

Constant Summary collapse

HTMLResponseParser =

:nodoc:

defined?(::Nokogiri::HTML5) ? ::Nokogiri::HTML5 : ::Nokogiri::HTML

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.



21
22
23
24
25
26
27
28
29
30
31
# File 'actionpack/lib/action_dispatch/testing/request_encoder.rb', line 21

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



19
20
21
# File 'actionpack/lib/action_dispatch/testing/request_encoder.rb', line 19

def response_parser
  @response_parser
end

Class Method Details

.encoder(name) ⇒ Object



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

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

.parser(content_type) ⇒ Object



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

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



54
55
56
# File 'actionpack/lib/action_dispatch/testing/request_encoder.rb', line 54

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



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

def accept_header
  @mime.to_s
end

#content_typeObject



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

def content_type
  @mime.to_s
end

#encode_params(params) ⇒ Object



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

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