Class: Alchemrest::Response::Pipeline::ExtractPayload
- Inherits:
-
Transform
- Object
- Morpher::Transform
- Transform
- Alchemrest::Response::Pipeline::ExtractPayload
- Defined in:
- lib/alchemrest/response/pipeline/extract_payload.rb
Overview
A transform that can extract a nested payload from a response body. Takes in an array of symbols, strings, or integers that represent the path to the data you care about. This class assumes the response.body is a hash with string keys, so it calls to_s on all non integer path elements.
Instance Method Summary collapse
- #call(response) ⇒ Object
- #extract_payload(response) ⇒ Object
-
#initialize(_path_to_payload = nil, _allow_empty_response = false) ⇒ ExtractPayload
constructor
# All symbols will be converted to strings, and integers will be left alone.
Methods inherited from Transform
Constructor Details
#initialize(_path_to_payload = nil, _allow_empty_response = false) ⇒ ExtractPayload
# All symbols will be converted to strings, and integers will be left alone.
17 18 19 |
# File 'lib/alchemrest/response/pipeline/extract_payload.rb', line 17 def initialize(_path_to_payload = nil, _allow_empty_response = false) # rubocop:disable Style/OptionalBooleanParameter super end |
Instance Method Details
#call(response) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/alchemrest/response/pipeline/extract_payload.rb', line 21 def call(response) # If response is empty and the status is 204 (No Content), return nil as the payload if response.no_content_response? return final(nil) if allow_empty_response? return failure(ResponsePipelineError.new("Ok but empty response not allowed")) end payload = extract_payload(response) if payload.nil? failure(ResponsePipelineError.new("Response body did not contain expected payload at #{path_to_payload}")) else success(payload) end end |
#extract_payload(response) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/alchemrest/response/pipeline/extract_payload.rb', line 37 def extract_payload(response) if path_to_payload.nil? response.data else normalized_path = path_to_payload.map do |path_element| if path_element.is_a?(Integer) path_element else path_element.to_s end end response.data.dig(*normalized_path) end # If our path tries to dig into a non hash, we'll get a TypeError. We want to rescue that and ensure # our code continues to safely return a result. rescue TypeError nil end |