Sha256: cca305e393aa55b6928db665654ebd0527cc937322a93287d90e7331312733e9

Contents?: true

Size: 654 Bytes

Versions: 1

Compression:

Stored size: 654 Bytes

Contents

require 'faraday/response/json'

module Mrkt
  module FaradayMiddleware
    class Response < ::Faraday::Response::Json
      private

      def process_response(env)
        super

        data = env[:body]

        raise Mrkt::Errors::EmptyResponse if data.nil?
        raise Mrkt::Errors::Error, data[:error_description] if data.key?(:error)

        handle_errors!(data[:errors]) unless data.fetch(:success, true)
      end

      def handle_errors!(errors)
        error = errors.first

        raise Mrkt::Errors::Unknown if error.nil?
        raise Mrkt::Errors.find_by_response_code(error[:code].to_i), error[:message]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mrkt-1.2.2 lib/mrkt/faraday_middleware/response.rb