Class: Footrest::ResponseMiddleware

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

Overview

Internal: The base class for middleware that parses responses.

Direct Known Subclasses

ParseJson, ParseJson::MimeTypeFix

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/footrest/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/footrest/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/footrest/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/footrest/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/footrest/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`.


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

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)

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

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

#process_response(env) ⇒ Object


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

def process_response(env)
  env[:body] = parse(env[:body])
end

#process_response_type?(type) ⇒ Boolean

Returns:

  • (Boolean)

68
69
70
71
72
# File 'lib/footrest/response_middleware.rb', line 68

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


57
58
59
60
61
62
63
64
65
66
# File 'lib/footrest/response_middleware.rb', line 57

def response_type(env)
  if env[:response_headers]
    type = env[:response_headers][CONTENT_TYPE].to_s
    type = type.split(';', 2).first if type.index(';')
    type
  else
    warn("response_headers are nil. Did you use http when you meant https?")
    nil
  end
end