Class: FaradayMiddleware::ResponseMiddleware

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/faraday_middleware/response_middleware.rb

Overview

Internal: The base class for middleware that parses responses.

Constant Summary collapse

CONTENT_TYPE =
'Content-Type'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, options = {}) ⇒ ResponseMiddleware

Returns a new instance of ResponseMiddleware.



28
29
30
31
32
33
# File 'lib/faraday_middleware/response_middleware.rb', line 28

def initialize(app = nil, options = {})
  super(app)
  @options = options
  @parser_options = options[:parser_options]
  @content_types = Array(options[:content_type])
end

Class Attribute Details

.parserObject

Returns the value of attribute parser.



12
13
14
# File 'lib/faraday_middleware/response_middleware.rb', line 12

def parser
  @parser
end

Class Method Details

.define_parser(parser = nil, &block) ⇒ Object

Store a Proc that receives the body and returns the parsed result.



16
17
18
19
20
# File 'lib/faraday_middleware/response_middleware.rb', line 16

def self.define_parser(parser = nil, &block)
  @parser = parser ||
            block  ||
            raise(ArgumentError, 'Define parser with a block')
end

.inherited(subclass) ⇒ Object



22
23
24
25
26
# File 'lib/faraday_middleware/response_middleware.rb', line 22

def self.inherited(subclass)
  super
  subclass.load_error = load_error if subclass.respond_to? :load_error=
  subclass.parser = parser
end

Instance Method Details

#call(environment) ⇒ Object



35
36
37
38
39
# File 'lib/faraday_middleware/response_middleware.rb', line 35

def call(environment)
  @app.call(environment).on_complete do |env|
    process_response(env) if process_response_type?(response_type(env)) && parse_response?(env)
  end
end

#parse(body) ⇒ Object

Parse the response body.

Instead of overriding this method, consider using ‘define_parser`.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/faraday_middleware/response_middleware.rb', line 51

def parse(body)
  if self.class.parser
    begin
      self.class.parser.call(body, @parser_options)
    rescue StandardError, SyntaxError => e
      raise e if e.is_a?(SyntaxError) &&
                 e.class.name != 'Psych::SyntaxError'

      raise Faraday::ParsingError, e
    end
  else
    body
  end
end

#parse_response?(env) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/faraday_middleware/response_middleware.rb', line 78

def parse_response?(env)
  env[:body].respond_to? :to_str
end

#preserve_raw?(env) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/faraday_middleware/response_middleware.rb', line 82

def preserve_raw?(env)
  env[:request].fetch(:preserve_raw, @options[:preserve_raw])
end

#process_response(env) ⇒ Object



41
42
43
44
45
46
# File 'lib/faraday_middleware/response_middleware.rb', line 41

def process_response(env)
  env[:raw_body] = env[:body] if preserve_raw?(env)
  env[:body] = parse(env[:body])
rescue Faraday::ParsingError => e
  raise Faraday::ParsingError.new(e.wrapped_exception, env[:response])
end

#process_response_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/faraday_middleware/response_middleware.rb', line 72

def process_response_type?(type)
  @content_types.empty? || @content_types.any? do |pattern|
    pattern.is_a?(Regexp) ? type =~ pattern : type == pattern
  end
end

#response_type(env) ⇒ Object



66
67
68
69
70
# File 'lib/faraday_middleware/response_middleware.rb', line 66

def response_type(env)
  type = env[:response_headers][CONTENT_TYPE].to_s
  type = type.split(';', 2).first if type.index(';')
  type
end