Sha256: 2088606358e4728fa0e31604f318187811b7589e425e7133e8535027da93789d

Contents?: true

Size: 751 Bytes

Versions: 6

Compression:

Stored size: 751 Bytes

Contents

require 'faraday_middleware'

module Mrkt
  module FaradayMiddleware
    class Response < ::FaradayMiddleware::ParseJson
      define_parser do |body|
        JSON.parse(body, symbolize_names: true) unless body.strip.empty?
      end

      def process_response(env)
        super

        data = env[:body]

        fail Mrkt::Errors::EmptyResponse if data.nil?
        fail 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

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mrkt-0.7.0 lib/mrkt/faraday_middleware/response.rb
mrkt-0.6.2 lib/mrkt/faraday_middleware/response.rb
mrkt-0.6.1 lib/mrkt/faraday_middleware/response.rb
mrkt-0.6.0 lib/mrkt/faraday_middleware/response.rb
mrkt-0.5.0 lib/mrkt/faraday_middleware/response.rb
mrkt-0.4.0 lib/mrkt/faraday_middleware/response.rb