Sha256: b384311ae9265b4ad02de1308dcf37a1aea4a663d9df179556759ac48d11c978

Contents?: true

Size: 832 Bytes

Versions: 1

Compression:

Stored size: 832 Bytes

Contents

require 'json'
require 'faraday'
require_relative '../postmates/error'

module FaradayMiddleware
  class RaiseHTTPException < Faraday::Middleware
    def call(env)
      @app.call(env).on_complete do |response|
        response_hash = JSON.parse(response.body)
        msg = "#{response[:status]} #{response_hash['message']}"

        case response[:status]
        when 400 ; raise Postmates::BadRequest,          msg
        when 401 ; raise Postmates::Unauthorized,        msg
        when 403 ; raise Postmates::Forbidden,           msg
        when 404 ; raise Postmates::NotFound,            msg
        when 500 ; raise Postmates::InternalServerError, msg
        when 503 ; raise Postmates::ServiceUnavailable,  msg
        end
      end
    end

    def initialize(app)
      super app
      @parser = nil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
postmates-0.1.0 lib/faraday/raise_http_exception.rb