<commit_before>require 'watir-webdriver'
require_relative './db'
require_relative './middleman'

class Check

	def initialize
		puts "\033[33mCHECK\033[0m :: Running"
		@db_file = File.join(File.dirname(File.expand_path("./stockless/")), 'product.db')
		@added_sku = Array.new
		
    create_client
    login
    handle_404

		SQLite3::Database.new @db_file do |db|
			db.execute "SELECT * FROM items" do |row|

        sup_sku = build_sku(row[0])
				current_inventory = row[2].to_i
				@b.text_field(:id => "pageHeader_SLDSearchControl1_searchInput").set sup_sku
				@b.button(:id => "pageHeader_SLDSearchControl1_searchSubmit").click
				upc = sup_sku.to_i
				if @b.table(:id => "dlistProducts").td(index: 0).a(index: 0).exists?
					@b.table(:id => "dlistProducts").td(index: 0).a(index: 0).click
					@b.driver.manage.timeouts.implicit_wait = 3
					@b.table(:id => "dlColorList").links.each { |color| color.click }
					@b.trs(:class, "dataTableRowBg").each do |trow|
						if trow.text.include? sup_sku
							quantity = trow[6].text.to_i
							if current_inventory != quantity
								puts "\033[32mSQL\033[0m :: UPDATE items SET quantity = #{quantity} WHERE upc = '#{upc}'"
								result = db.prepare("UPDATE items SET quantity = #{quantity} WHERE upc = '#{upc}'").execute
                add_to_new_inventory
							end
						end
						close_result result
					end
				else
					puts "\033[31mWARN\033[0m :: Item now nonexistent, remove from list"
					puts "\033[32mSQL\033[0m :: DELETE FROM items WHERE upc = '#{upc}'"
					result = db.prepare("DELETE FROM items WHERE upc = '#{upc}'").execute
				end

				close_result result
			end

			@b.close
		end

		MiddleMan.new(:send, :sku => @added_sku) unless @added_sku.empty?

	rescue Watir::Exception::UnknownObjectException, Net::ReadTimeout
		@b.close
		puts "\033[31mERROR\033[0m :: Error on webpage"

	end

  def build_sku sku
    sup_sku = "0" * 8
    sup_sku += sku
    sup_sku = sup_sku[sup_sku.length - 14..-1]
  end

  def create_client
		client = Selenium::WebDriver::Remote::Http::Default.new
		client.timeout = 180 # seconds – default is 60
		@b = Watir::Browser.new :firefox, :http_client => client
  end

	def close_result res
		res.close if res
	end

  def handle_404
    @b.goto("https://www.visr.net/msib21vb") if @b.url.match /(aspxerror|NotAvailable)/
  end

  def login
		@b.driver.manage.timeouts.implicit_wait = 5
		@b.goto("https://www.visr.net/msib21vb")
		@b.text_field(:name => "Loginform1:UserName").set CNF['site_user']
		@b.text_field(:name => "Loginform1:Password").set CNF['site_pass']
		@b.button(:name => "Loginform1:Login_Command").click
  end

  def add_to_new_inventory
    @added_sku << row[1] if current_inventory + 5 < quantity
  end

end<commit_msg>Split up nested conditionals<commit_after>require 'watir-webdriver'
require_relative './db'
require_relative './middleman'

class Check

	def initialize
		puts "\033[33mCHECK\033[0m :: Running"
		@db_file = File.join(File.dirname(File.expand_path("./stockless/")), 'product.db')
		@added_sku = Array.new
		
    create_client
    login
    handle_404

		SQLite3::Database.new @db_file do |db|
			db.execute "SELECT * FROM items" do |row|
        @row = @row
        @current_inventory = @row[2].to_i
        @sup_sku = build_sku(@row[0])
        @sku = @row[1]
				@upc = @sup_sku.to_i
        search
        find_product
				close_result result
			end

			@b.close
		end

		MiddleMan.new(:send, :sku => @added_sku) unless @added_sku.empty?

	rescue Watir::Exception::UnknownObjectException, Net::ReadTimeout
		@b.close
		puts "\033[31mERROR\033[0m :: Error on webpage"

	end

  def find_product
    if @b.table(:id => "dlistProducts").td(index: 0).a(index: 0).exists?
      @b.table(:id => "dlistProducts").td(index: 0).a(index: 0).click
      @b.driver.manage.timeouts.implicit_wait = 3
      @b.table(:id => "dlColorList").links.each { |color| color.click }
      @b.trs(:class, "dataTableRowBg").each do |trow|
      handle_row(trow)
      end
    else
      puts "\033[31mWARN\033[0m :: Item now nonexistent, remove from list"
      puts "\033[32mSQL\033[0m :: DELETE FROM items WHERE @upc = '#{@upc}'"
      result = db.prepare("DELETE FROM items WHERE @upc = '#{@upc}'").execute
    end
  end
  
  def handle_row trow
    if trow.text.include? @sup_sku
      @quantity = trow[6].text.to_i
      update_inventory(db) if @current_inventory != @quantity
        puts "\033[32mSQL\033[0m :: UPDATE items SET quantity = #{quantity} WHERE @upc = '#{@upc}'"
        result = db.prepare("UPDATE items SET quantity = #{quantity} WHERE @upc = '#{@upc}'").execute
        add_to_new_inventory
      end
    end
    close_result result
  end

  def build_sku sku
    @sup_sku = "0" * 8
    @sup_sku += sku
    @sup_sku = @sup_sku[@sup_sku.length - 14..-1]
  end

  def create_client
		client = Selenium::WebDriver::Remote::Http::Default.new
		client.timeout = 180 # seconds – default is 60
		@b = Watir::Browser.new :firefox, :http_client => client
  end

	def close_result res
		res.close if res
	end

  def handle_404
    @b.goto("https://www.visr.net/msib21vb") if @b.url.match /(aspxerror|NotAvailable)/
  end

  def login
		@b.driver.manage.timeouts.implicit_wait = 5
		@b.goto("https://www.visr.net/msib21vb")
		@b.text_field(:name => "Loginform1:UserName").set CNF['site_user']
		@b.text_field(:name => "Loginform1:Password").set CNF['site_pass']
		@b.button(:name => "Loginform1:Login_Command").click
  end

  def add_to_new_inventory
    @added_sku << @sku if @current_inventory + 5 < @quantity
  end

  def search
    @b.text_field(:id => "pageHeader_SLDSearchControl1_searchInput").set @sup_sku
    @b.button(:id => "pageHeader_SLDSearchControl1_searchSubmit").click
  end

end
{'fraction_non_alphanumeric': 0.14427773900907187, 'fraction_numerical': 0.028611304954640614, 'mean_word_length': 3.38302752293578, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, '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': '774269', 'n_tokens_mistral': 2400, 'n_tokens_neox': 2088, 'n_words': 432}