Sha256: 4110e4bd9576802df7af438d508b16c9eda9d4f37dd5e9197024f4dae1fe1b99

Contents?: true

Size: 662 Bytes

Versions: 3

Compression:

Stored size: 662 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[:url]
        when 500..599
          raise Errors::ServerError, env[:url]
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
json_api_client-0.9.6 lib/json_api_client/middleware/status.rb
json_api_client-0.9.5 lib/json_api_client/middleware/status.rb
json_api_client-0.9.4 lib/json_api_client/middleware/status.rb