Class: FaradayMiddleware::ConvertSdataToHeaders Private
- Inherits:
-
ResponseMiddleware
- Object
- ResponseMiddleware
- FaradayMiddleware::ConvertSdataToHeaders
- Defined in:
- lib/faraday/response/convert_sdata_to_headers.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Middleware to strip out Sage’s pagination SData from the body and place it in a custom response header instead (Using familiar ‘Link’ header syntax). This just leaves the resources in the body which can then be recursively collected later by following the links.
Constant Summary collapse
- SDATA_START_INDEX =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"$startIndex".freeze
- SDATA_TOTAL_RESULTS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"$totalResults".freeze
- SDATA_ITEMS_PER_PAGE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"$itemsPerPage".freeze
- SDATA_RESOURCES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"$resources".freeze
- SDATA_HEADER_NAME =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"X-SData-Pagination-Links".freeze
Instance Method Summary collapse
- #call(env) ⇒ Object private
-
#initialize(app) ⇒ ConvertSdataToHeaders
constructor
private
A new instance of ConvertSdataToHeaders.
Constructor Details
#initialize(app) ⇒ ConvertSdataToHeaders
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of ConvertSdataToHeaders.
49 50 51 52 |
# File 'lib/faraday/response/convert_sdata_to_headers.rb', line 49 def initialize(app) @links = [] super app end |
Instance Method Details
#call(env) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/faraday/response/convert_sdata_to_headers.rb', line 22 def call(env) @app.call(env).on_complete do @response = env[:response] # Only proceed if SData is actually present next unless @response.body && @response.body.kind_of?(Hash) && @response.body[SDATA_TOTAL_RESULTS] @url = Addressable::URI::parse(env[:url]) @url.query_values ||= {} # Add 'next' link, if we're not on the last page add_link(next_start_index, 'next') if next_start_index < @response.body[SDATA_TOTAL_RESULTS] # Add 'prev' link if we're not on the first page add_link(prev_start_index, 'prev') if prev_start_index >= 0 # Special case: If we're halfway through the first page, don't allow negative start indices add_link(0, 'prev') if @response.body[SDATA_START_INDEX] != 0 && prev_start_index < 0 # Add the page links into the header @response.headers[SDATA_HEADER_NAME] = @links.join(', ') unless @links.empty? # Strip out the SData from the body env[:body] = @response.body[SDATA_RESOURCES] end end |