Class: FaradayMiddleware::ResponseMiddleware
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- FaradayMiddleware::ResponseMiddleware
- Defined in:
- lib/faraday_middleware/response_middleware.rb
Overview
Internal: The base class for middleware that parses responses.
Direct Known Subclasses
Chunked, ForceUtf, ParseJson, ParseJson::MimeTypeFix, ParseMarshal, ParseNokogiriHtml, ParseNokogiriXml, ParseXml, ParseYaml
Constant Summary collapse
- CONTENT_TYPE =
'Content-Type'.freeze
Class Attribute Summary collapse
-
.parser ⇒ Object
Returns the value of attribute parser.
Class Method Summary collapse
-
.define_parser(parser = nil) ⇒ Object
Store a Proc that receives the body and returns the parsed result.
- .inherited(subclass) ⇒ Object
Instance Method Summary collapse
- #call(environment) ⇒ Object
-
#initialize(app = nil, options = {}) ⇒ ResponseMiddleware
constructor
A new instance of ResponseMiddleware.
-
#parse(body) ⇒ Object
Parse the response body.
- #parse_response?(env) ⇒ Boolean
- #preserve_raw?(env) ⇒ Boolean
- #process_response(env) ⇒ Object
- #process_response_type?(type) ⇒ Boolean
- #response_type(env) ⇒ Object
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, = {}) super(app) @options = @content_types = Array([:content_type]) end |
Class Attribute Details
.parser ⇒ Object
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
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
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
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 |