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'.freeze

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.



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

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

Class Attribute Details

.parserObject

Returns the value of attribute parser.



9
10
11
# File 'lib/faraday_middleware/response_middleware.rb', line 9

def parser
  @parser
end

Class Method Details

.define_parser(parser = nil) ⇒ Object

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



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

def self.define_parser(parser = nil)
  @parser = parser || Proc.new
end

.inherited(subclass) ⇒ Object



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

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

Instance Method Details

#call(environment) ⇒ Object



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

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

#parse(body) ⇒ Object

Parse the response body.

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



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/faraday_middleware/response_middleware.rb', line 45

def parse(body)
  if self.class.parser
    begin
      self.class.parser.call(body)
    rescue StandardError, SyntaxError => err
      raise err if err.is_a? SyntaxError and err.class.name != 'Psych::SyntaxError'
      raise Faraday::Error::ParsingError, err
    end
  else
    body
  end
end

#parse_response?(env) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/faraday_middleware/response_middleware.rb', line 70

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

#preserve_raw?(env) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#process_response(env) ⇒ Object



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

def process_response(env)
  env[:raw_body] = env[:body] if preserve_raw?(env)
  env[:body] = parse(env[:body])
end

#process_response_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
# File 'lib/faraday_middleware/response_middleware.rb', line 64

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

#response_type(env) ⇒ Object



58
59
60
61
62
# File 'lib/faraday_middleware/response_middleware.rb', line 58

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