Sha256: 2bf7c68b2e425cbc6c837a9f61b414f8a9ae998eb99c245a970a2d99d3564109

Contents?: true

Size: 656 Bytes

Versions: 22

Compression:

Stored size: 656 Bytes

Contents

module JsonApiClient
  module Middleware
    class Status < Faraday::Middleware
      def call(environment)
        @app.call(environment).on_complete do |env|
          handle_status(env[:status], env)

          # look for meta[:status]
          if env[:body].is_a?(Hash)
            code = env[:body].fetch("meta", {}).fetch("status", 200).to_i
            handle_status(code, env)
          end
        end
      end

      protected

      def handle_status(code, env)
        case code
        when 404
          raise Errors::NotFound, env[:uri]
        when 500..599
          raise Errors::ServerError, env
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
json_api_client-0.9.3 lib/json_api_client/middleware/status.rb
json_api_client-0.9.2 lib/json_api_client/middleware/status.rb
json_api_client-0.9.0 lib/json_api_client/middleware/status.rb
json_api_client-0.8.1 lib/json_api_client/middleware/status.rb
json_api_client-0.8.0 lib/json_api_client/middleware/status.rb
json_api_client-0.7.1 lib/json_api_client/middleware/status.rb
json_api_client-0.7.0 lib/json_api_client/middleware/status.rb
json_api_client-0.6.0 lib/json_api_client/middleware/status.rb
json_api_client-0.5.1 lib/json_api_client/middleware/status.rb
json_api_client-0.5.0 lib/json_api_client/middleware/status.rb
json_api_client-0.4.0 lib/json_api_client/middleware/status.rb
json_api_client-0.3.1 lib/json_api_client/middleware/status.rb
json_api_client-0.3.0 lib/json_api_client/middleware/status.rb
json_api_client-0.2.4 lib/json_api_client/middleware/status.rb
json_api_client-0.2.3 lib/json_api_client/middleware/status.rb
json_api_client-0.2.2 lib/json_api_client/middleware/status.rb
json_api_client-0.2.1 lib/json_api_client/middleware/status.rb
json_api_client-0.2.0 lib/json_api_client/middleware/status.rb
json_api_client-0.1.3 lib/json_api_client/middleware/status.rb
json_api_client-0.1.2 lib/json_api_client/middleware/status.rb