Sha256: ef85df5efcabad38fcac1d9f5a971ceb983f26d2b7cf356e7a95d377d38738b4
Contents?: true
Size: 1.02 KB
Versions: 18
Compression:
Stored size: 1.02 KB
Contents
# frozen_string_literal: true module FriendlyShipping module Services class UpsFreight class ParseJSONResponse extend Dry::Monads::Result::Mixin class << self def call(response_body, expected_root_tag) json = JSON.parse(response_body) if request_successful?(json, expected_root_tag) Success(json) else Failure(error_message(json)) end rescue JSON::ParserError => e Failure(e) end private def request_successful?(json, expected_root_tag) json[expected_root_tag].present? end def error_message(json) detailed_error = json.dig('Fault', 'detail', 'Errors', 'ErrorDetail', 'PrimaryErrorCode') status = detailed_error['Code'] desc = detailed_error['Description'] [status, desc].compact.join(": ").presence || 'UPS could not process the request.' end end end end end end
Version data entries
18 entries across 18 versions & 1 rubygems