<commit_before>module BillHicks
  # Inventory item response structure:
  #
  #   {
  #     product:  "...",
  #     upc:      "...",
  #     quantity: "..."
  #   }
  class Inventory < Base

    INVENTORY_FILENAME = 'billhicksinventory.csv'

    def initialize(options = {})
      requires!(options, :username, :password)
      @options = options
    end

    def self.quantity(chunk_size = 15, options = {}, &block)
      requires!(options, :username, :password)
      new(options).all(chunk_size, &block)
    end

    def self.all(chunk_size = 15, options = {}, &block)
      requires!(options, :username, :password)
      new(options).all(chunk_size, &block)
    end

    def all(chunk_size, &block)
      connect(@options) do |ftp|
        begin
          tempfile = Tempfile.new

          ftp.chdir(BillHicks.config.top_level_dir)
          ftp.getbinaryfile(INVENTORY_FILENAME, tempfile.path)

          SmarterCSV.process(tempfile, {
            chunk_size: chunk_size,
            force_utf8: true,
            convert_values_to_numeric: false,
            key_mapping: {
              product: :item_identifier,
              qty_avail: :quantity,
            }
          }) do |chunk|
            chunk.each do |item|
              item.except!(:product)
            end

            yield(chunk)
          end
        ensure
          tempfile.unlink
          ftp.close
        end
      end
    end
<commit_msg>add get_quantity_file method, and cleanup _all_<commit_after>module BillHicks
  # Inventory item response structure:
  #
  #   {
  #     product:  "...",
  #     upc:      "...",
  #     quantity: "..."
  #   }
  class Inventory < Base

    INVENTORY_FILENAME = 'billhicksinventory.csv'

    def initialize(options = {})
      requires!(options, :username, :password)
      @options = options
    end

    def self.get_quantity_file(options = {})
      requires!(options, :username, :password)
      new(options).get_quantity_file
    end

    def self.quantity(chunk_size = 15, options = {}, &block)
      requires!(options, :username, :password)
      new(options).all(chunk_size, &block)
    end

    def self.all(chunk_size = 15, options = {}, &block)
      requires!(options, :username, :password)
      new(options).all(chunk_size, &block)
    end

    def all(chunk_size, &block)
      quantity_tempfile = get_file(INVENTORY_FILENAME)

      SmarterCSV.process(quantity_tempfile.open, {
        chunk_size: chunk_size,
        force_utf8: true,
        convert_values_to_numeric: false,
        key_mapping: {
          product: :item_identifier,
          qty_avail: :quantity,
        }
      }) do |chunk|
        chunk.each do |item|
          item.except!(:product)
        end

        yield(chunk)
      end

      quantity_tempfile.unlink
    end

    def get_quantity_file
      quantity_tempfile = get_file(INVENTORY_FILENAME)
      tempfile          = Tempfile.new

      SmarterCSV.process(quantity_tempfile.open, {
        chunk_size: 100,
        force_utf8: true,
        convert_values_to_numeric: false,
        key_mapping: {
          product: :item_identifier,
          qty_avail: :quantity,
        }
      }) do |chunk|
        chunk.each do |item|
          tempfile.puts("#{item[:item_identifier]},#{item[:quantity]}")
        end
      end

      quantity_tempfile.unlink
      tempfile.close

      tempfile.path
    end

{'fraction_non_alphanumeric': 0.10887573964497041, 'fraction_numerical': 0.004142011834319527, 'mean_word_length': 2.0569620253164556, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4198383', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1032, 'n_words': 197}