module DS
  module Authenticate
    def authenticate
      op = AuthenticateOperation.new
      op.perform({username: user, password: password})
    end

    def check_session(response)
      return true if response[:success]
      xml = result(response)
      !session_failed?(xml)
    end

    def session_failed?(xml)
      xml && xml['root'] && xml['root']['message'].to_s.match(/.*session.*/i)
    end

    def result(response)
      Hash.from_xml(response[:xml])
    end

    def get_error
      op = GetLastErrorOperation.new
      op.perform
    end
  end
end
