Sha256: 8fcb261e595b0c0529c92b3ed943941955a631019e80cac731bc3ed92bdeb707

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

module FlexmlsApi
  module FaradayExt
    #=Flexmls API Faraday middle way
    # HTTP Response after filter to package api responses and bubble up basic api errors.
    class FlexmlsMiddleware < Faraday::Response::Middleware
      begin
        def self.register_on_complete(env)
          env[:response].on_complete do |finished_env|
            validate_and_build_response(finished_env)
          end
        end
      rescue LoadError, NameError => e
        self.load_error = e
      end
      
      # Handles pretty much all the api response parsing and error handling.  All responses that
      # indicate a failure will raise a FlexmlsApi::ClientError exception
      def self.validate_and_build_response(finished_env)
        body = finished_env[:body]
        FlexmlsApi.logger.debug("Response Body: #{body.inspect}")
        unless body.is_a?(Hash) && body.key?("D")
          raise InvalidResponse, "The server response could not be understood"
        end
        response = ApiResponse.new body
        case finished_env[:status]
        when 400, 409
          raise BadResourceRequest.new(response.code, finished_env[:status]), response.message
        when 401
          raise PermissionDenied.new(response.code, finished_env[:status]), response.message
        when 404
          raise NotFound.new(response.code, finished_env[:status]), response.message
        when 405
          raise NotAllowed.new(response.code, finished_env[:status]), response.message
        when 500
          raise ClientError.new(response.code, finished_env[:status]), response.message
        when 200..299
          FlexmlsApi.logger.debug("Success!")
        else 
          raise ClientError.new(response.code, finished_env[:status]), response.message
        end
        finished_env[:body] = response
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flexmls_api-0.3.3 lib/flexmls_api/faraday.rb