gururuby/tilda-simple-api

View on GitHub
lib/tilda/simple/api/errors_handler.rb

Summary

Maintainability
A
0 mins
Test Coverage
module Tilda
  module Simple
    module Api
      module ErrorsHandler
        def get(path, params = {})
          handle_errors { super(path, params) }
        end

        private

        def handle_errors(&block)
          response = block.call
          check_response(response) if Api.config.raise_api_errors
          response
        end

        def check_response(response)
          raise api_error_class(response), response['message'] if response['status'] == 'ERROR'
        end

        def api_error_class(response)
          case response['message']
          when /Wrong Secret Key length|Wrong Public Key length/
            Errors::InvalidCredentials
          when /Can't find project with this 'projectid' parameter/
            Errors::ProjectNotFound
          when /Can't find page with this 'pageid' parameter/
            Errors::PageNotFound
          else
            Errors::Error
          end
        end
      end
    end
  end
end