Sha256: 6efb0e20a6f0c3ab82141b2fb2bb306f9d67a8714942a690089ac78c188e88e7

Contents?: true

Size: 755 Bytes

Versions: 11

Compression:

Stored size: 755 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]

        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

11 entries across 11 versions & 1 rubygems

Version Path
mrkt-1.2.1 lib/mrkt/faraday_middleware/response.rb
mrkt-1.2.0 lib/mrkt/faraday_middleware/response.rb
mrkt-1.1.1 lib/mrkt/faraday_middleware/response.rb
mrkt-1.1.0 lib/mrkt/faraday_middleware/response.rb
mrkt-1.0.1 lib/mrkt/faraday_middleware/response.rb
mrkt-1.0.0 lib/mrkt/faraday_middleware/response.rb
mrkt-0.11.1 lib/mrkt/faraday_middleware/response.rb
mrkt-0.11.0 lib/mrkt/faraday_middleware/response.rb
mrkt-0.10.0 lib/mrkt/faraday_middleware/response.rb
mrkt-0.9.0 lib/mrkt/faraday_middleware/response.rb
mrkt-0.8.0 lib/mrkt/faraday_middleware/response.rb