<commit_before>class QBWC::Session

  attr_reader :user, :company, :ticket, :progress
  attr_accessor :error, :status_code, :status_severity

  @@session = nil

	def self.get(ticket)
		@@session
	end

  def initialize(user = nil, company = nil, ticket = nil)
    @user = user
    @company = company
    @current_job = nil
    @error = nil
    @progress = 0
    @iterator_id = nil
    @initial_job_count = pending_jobs.length

    @ticket = ticket || Digest::SHA1.hexdigest("#{Rails.application.config.secret_token}#{Time.now.to_i}")

    @@session = self
    reset(ticket.nil?)
  end

  def finished?
    self.progress == 100
  end

  def next_request
    return nil if current_job.nil?
    until (request = current_job.next_request) do
      pending_jobs.shift
      reset(true) or break
    end
    jobs_completed = @initial_job_count - pending_jobs.length
    self.progress = ((jobs_completed.to_f  / @initial_job_count.to_f ) * 100).to_i
    request
  end
  alias :next :next_request  # Deprecated method name 'next'

  def current_request
    request = self.next_request
    if request && self.iterator_id.present?
      request = request.to_hash
      request.delete('xml_attributes')
      request.values.first['xml_attributes'] = {'iterator' => 'Continue', 'iteratorID' => self.iterator_id}
      request = QBWC::Request.new(request)
    end 
    request
  end

  def response=(qbxml_response)
    begin
      QBWC.logger.info 'Parsing response.'
      response = QBWC.parser.from_qbxml(qbxml_response)["qbxml"]["qbxml_msgs_rs"].except("xml_attributes")
      response = response[response.keys.first]
      QBWC.logger.info 'Parsing headers.'
      parse_response_header(response)
      QBWC.logger.info "Processing response."
      self.current_job.process_response(response, self, iterator_id.blank?)
      self.next_request # search next request

    rescue => e
      self.error = e.message
      QBWC.logger.warn "An error occured in QBWC::Session: #{e.message}"
      QBWC.logger.warn e.backtrace.join("\n")
    end
  end

  def save
  end

  def destroy
    self.freeze
    @@session = nil<commit_msg>Strengthen response processing and session progress<commit_after>class QBWC::Session

  attr_reader :user, :company, :ticket, :progress
  attr_accessor :error, :status_code, :status_severity

  @@session = nil

	def self.get(ticket)
		@@session
	end

  def initialize(user = nil, company = nil, ticket = nil)
    @user = user
    @company = company
    @current_job = nil
    @error = nil
    @progress = 0
    @iterator_id = nil
    @initial_job_count = pending_jobs.length

    @ticket = ticket || Digest::SHA1.hexdigest("#{Rails.application.config.secret_token}#{Time.now.to_i}")

    @@session = self
    reset(ticket.nil?)
  end

  def finished?
    self.progress == 100
  end

  def next_request
    if current_job.nil?
      self.progress = 100
      return nil
    end
    until (request = current_job.next_request) do
      pending_jobs.shift
      reset(true) or break
    end
    jobs_completed = @initial_job_count - pending_jobs.length
    self.progress = ((jobs_completed.to_f  / @initial_job_count.to_f ) * 100).to_i
    request
  end
  alias :next :next_request  # Deprecated method name 'next'

  def current_request
    request = self.next_request
    if request && self.iterator_id.present?
      request = request.to_hash
      request.delete('xml_attributes')
      request.values.first['xml_attributes'] = {'iterator' => 'Continue', 'iteratorID' => self.iterator_id}
      request = QBWC::Request.new(request)
    end 
    request
  end

  def response=(qbxml_response)
    begin
      QBWC.logger.info 'Parsing response.'
      response = QBWC.parser.from_qbxml(qbxml_response)["qbxml"]["qbxml_msgs_rs"].except("xml_attributes")
      response = response[response.keys.first]
      QBWC.logger.info 'Parsing headers.'
      parse_response_header(response)
      QBWC.logger.info "Processing response."
      self.current_job.process_response(response, self, iterator_id.blank?) unless self.current_job.nil?
      self.next_request # search next request

    rescue => e
      self.error = e.message
      QBWC.logger.warn "An error occured in QBWC::Session: #{e.message}"
      QBWC.logger.warn e.backtrace.join("\n")
    end
  end

  def save
  end

  def destroy
    self.freeze
    @@session = nil
{'fraction_non_alphanumeric': 0.1085503572251671, 'fraction_numerical': 0.004378889144964277, 'mean_word_length': 3.20135527589545, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7418673', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1382, 'n_words': 305}