module TicketingHub
  class Error < StandardError
    attr_accessor :response

    def initialize response=nil
      @response = response
    end

    def response_body
      @response[:body]
    end

    private
      def build_error_message
        return nil  if @response.nil?

        message = if response_body
          ": #{response_body[:error] || response_body[:error_message] || ''}"
        else
          ''
        end
        errors = unless message.empty?
          response_body[:errors] ?  ": #{response_body[:errors].map{|e|e[:error_message]}.join(', ')}" : ''
        end
        "#{@response[:method].to_s.upcase} #{@response[:url].to_s}: #{@response[:status]}#{message}#{errors}"
      end
  end

  # Raised when TicketingHub returns a 400 HTTP status code
  class BadRequest < Error; end

  # Raised when TicketingHub returns a 401 HTTP status code
  class Unauthorized < Error; end

  # Raised when TicketingHub returns a 403 HTTP status code
  class Forbidden < Error; end

  # Raised when TicketingHub returns a 404 HTTP status code
  class NotFound < Error; end

  # Raised when TicketingHub returns a 405 HTTP status code
  class Gone < Error; end

  # Raised when TicketingHub returns a 406 HTTP status code
  class NotAcceptable < Error; end

  # Raised when TicketingHub returns a 422 HTTP status code
  class UnprocessableEntity < Error; end

  # Raised when TicketingHub returns a 500 HTTP status code
  class InternalServerError < Error; end

  # Raised when TicketingHub returns a 501 HTTP status code
  class NotImplemented < Error; end

  # Raised when TicketingHub returns a 502 HTTP status code
  class BadGateway < Error; end

  # Raised when TicketingHub returns a 503 HTTP status code
  class ServiceUnavailable < Error; end
end