<commit_before>require "socket"
require "./cartbotsy/api"
require "./cartbotsy/config"
require "./cartbotsy/parser"
require "pry"

module Cartbotsy
  class Worker

    def self.start
      server = TCPServer.new(3001)
      loop do
        puts 'Waiting for hook...'
        client = server.accept
        if client
          puts 'Request received... parsing...'
          parsed = Cartbotsy::Parser.new(client).parse
          if parsed.is_a?(Hash)
            handle_success(parsed)
          else
            handle_failure(parsed)
          end
          puts 'Request parsed...'
        end
      end
    end

    private

    def self.parse_socket(socket)
      response = []
      while line = socket.gets
        response << line
      end
      CGI::parse(response.last)
    end

    def self.handle_success(parsed_hash)
      message = "#{parsed_hash[:product_amount]} produkty dodane do " +
        "koszyka dla miasta #{parsed_hash[:channel_name].capitalize} przez użytkownika #{parsed_hash[:username]}."
      Cartbotsy::API.new(Cartbotsy::Config::API_TOKEN).post_message(message, "##{parsed_hash[:channel_name]}")
      puts 'Message to Slack sent: ' + message
    end

    def self.handle_failure(parsed_hash)
      message = "COS ZEPSUŁEŚ KOLEGO #{parsed_hash[:username]}"
      Cartbotsy::API.new(Cartbotsy::Config::API_TOKEN).post_message(message, "##{parsed_hash[:channel_name]}")
    end
  end
end
Cartbotsy::Worker.start<commit_msg>add api call to rails app<commit_after>require "socket"
require "./cartbotsy/api"
require "./cartbotsy/config"
require "./cartbotsy/parser"
require "pry"

module Cartbotsy
  class Worker

    def self.start
      server = TCPServer.new(3001)
      loop do
        puts 'Waiting for hook...'
        client = server.accept
        if client
          puts 'Request received... parsing...'
          parsed = Cartbotsy::Parser.new(client).parse
          if parsed.is_a?(Hash)
            handle_success(parsed)
          else
            handle_failure(parsed)
          end
          puts 'Request parsed...'
        end
      end
    end

    private

    def self.parse_socket(socket)
      response = []
      while line = socket.gets
        response << line
      end
      CGI::parse(response.last)
    end

    def self.handle_success(parsed_hash)
      message = "#{parsed_hash[:product_amount]} produkty dodane do " +
        "koszyka dla miasta #{parsed_hash[:channel_name].capitalize} przez użytkownika #{parsed_hash[:username]}."
      Cartbotsy::API.new(Cartbotsy::Config::API_TOKEN).post_message(message, "##{parsed_hash[:channel_name]}")
      api_call(parsed_hash[:channel_name], parsed_hash[:product_link], parsed_hash[:product_amount])
      puts 'Message to Slack sent: ' + message
    end

    def self.handle_failure(parsed_hash)
      message = "COS ZEPSUŁEŚ KOLEGO #{parsed_hash[:username]}"
      Cartbotsy::API.new(Cartbotsy::Config::API_TOKEN).post_message(message, "##{parsed_hash[:channel_name]}")
    end

    def self.api_call(channel_name, product_link, product_amount)
      api_hash = {
        region_name: channel_name,
        product_link: product_link,
        product_amount: product_amount,
      }
      puts HTTMultiParty.post(Cartbotsy::Config::APP_URL + "/api/update", query: api_hash)
    end
  end
end
Cartbotsy::Worker.start
{'fraction_non_alphanumeric': 0.11398496240601504, 'fraction_numerical': 0.002406015037593985, 'mean_word_length': 2.775255391600454, 'pattern_counts': {'":': 0, '<': 7, '<?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': '5032477', 'n_tokens_mistral': 1049, 'n_tokens_neox': 1075, 'n_words': 212}