Sha256: d42dfd2d43978a8e732cab2146d00a2c309277eef34b14ce9807542f09399965

Contents?: true

Size: 1.08 KB

Versions: 8

Compression:

Stored size: 1.08 KB

Contents

require 'faraday'
require 'flattr/error/bad_request'
require 'flattr/error/forbidden'
require 'flattr/error/not_acceptable'
require 'flattr/error/not_found'
require 'flattr/error/unauthorized'

module Flattr
  module Response
    class RaiseClientError < Faraday::Response::Middleware

      def on_complete(env)
        case env[:status].to_i
        when 400
          raise Flattr::Error::BadRequest.new(error_body(env[:body]), env[:response_headers])
        when 401
          raise Flattr::Error::Unauthorized.new(error_body(env[:body]), env[:response_headers])
        when 403
          raise Flattr::Error::Forbidden.new(error_body(env[:body]), env[:response_headers])
        when 404
          raise Flattr::Error::NotFound.new(error_body(env[:body]), env[:response_headers])
        when 406
          raise Flattr::Error::NotAcceptable.new(error_body(env[:body]), env[:response_headers])
        end
      end

    private

      def error_body(body)
        if body.nil?
          ''
        elsif body['error']
          body['error_description']
        end
      end

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
flattr-0.3.7 lib/flattr/response/raise_client_error.rb
flattr-0.3.6 lib/flattr/response/raise_client_error.rb
flattr-0.3.5 lib/flattr/response/raise_client_error.rb
flattr-0.3.4 lib/flattr/response/raise_client_error.rb
flattr-0.3.3 lib/flattr/response/raise_client_error.rb
flattr-0.3.2 lib/flattr/response/raise_client_error.rb
flattr-0.3.1 lib/flattr/response/raise_client_error.rb
flattr-0.3.0 lib/flattr/response/raise_client_error.rb