Sha256: b58099f7bd16a37db19857667c062028616ad432a63aadba5f0cf43585453c38
Contents?: true
Size: 1.42 KB
Versions: 2
Compression:
Stored size: 1.42 KB
Contents
# frozen_string_literal: true module Vindi module Middleware # Vindi Response Parser # # Set metada info to metadata object. class ResponseParser < Faraday::Response::Middleware def on_complete(env) env[:body] = parse(env[:status] == 204 ? "{}" : env) Vindi::RateLimit.update env[:body].dig :metadata, :rate_limit end def parse(env) json = Her::Middleware::ParseJSON.new.parse_json env[:body] errors = translate_errors_to_activemodel_style(json.delete(:errors) || []) metadata = (json.delete(:metadata) || {}).merge(extract_response_headers_info(env[:response_headers])) { data: json, errors: errors, metadata: metadata } end def translate_errors_to_activemodel_style(errors) errors.map do |e| { attribute: e[:parameter], type: e[:id] == "invalid_parameter" ? :invalid : e[:id], message: e[:message] } end end def extract_response_headers_info(response_headers) { items: response_headers[:total], link: response_headers[:link], rate_limit: { limit: response_headers[:"rate-limit-limit"], reset: Time.at(response_headers[:"rate-limit-reset"].to_i), remaining: response_headers[:"rate-limit-remaining"] } } end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
vindi-hermes-0.0.2 | lib/vindi/middleware/response_parser.rb |
vindi-hermes-0.0.1 | lib/vindi/middleware/response_parser.rb |