<commit_before>require 'json'
require 'faraday'

module Piebits
  class App
    attr_reader :environment
    attr_reader :working_dir
    attr_reader :arguments
    
    DEFAULT_CONFIG_FILENAME = ".piebits.yml"
    PIEBITS_URL = "http://piebits.com"
    
    def initialize(environment:, arguments:, faraday:nil, config_filename:DEFAULT_CONFIG_FILENAME, 
        input_io:STDIN, output_io:STDOUT, error_io:STDERR)
      @environment = environment
      @working_dir = environment['PWD'] # TODO: allow override with an argument
      @faraday = faraday || Faraday.new(PIEBITS_URL)
      @config_filename = config_filename
      @arguments = arguments
      @input_io = input_io
      @output_io = output_io
      @error_io = error_io
    end
    
    def run
      # redirect STDERR
      previous_stderr = $stderr
      $stderr = @error_io
      
      # ensure we have a sane environment
      
      # intialize a BuildGenerator
      build_generator = BuildGenerator.new(environment: environment, 
        working_dir: working_dir, 
        config_filename: File.join(working_dir, @config_filename)
        )
      # generate a build
      build = build_generator.generate_build
      # submit the build to the service
      submitter = BuildSubmitter.new(build: build, faraday: @faraday)
      response = submitter.submit_build
      if response.success?
        @output_io.puts "Build submitted successfully."
        return 0
      else
        @error_io.puts "Failed to submit build. Response: #{response}"<commit_msg>Accepts alternate PIEBITS_URL from environment<commit_after>require 'json'
require 'faraday'

module Piebits
  class App
    attr_reader :environment
    attr_reader :working_dir
    attr_reader :arguments
    
    DEFAULT_CONFIG_FILENAME = ".piebits.yml"
    PIEBITS_URL = "http://piebits.com"
    
    def initialize(environment:, arguments:, faraday:nil, config_filename:DEFAULT_CONFIG_FILENAME, 
        input_io:STDIN, output_io:STDOUT, error_io:STDERR)
      @environment = environment
      @working_dir = environment['PWD'] # TODO: allow override with an argument
      url = environment['PIEBITS_URL'] || PIEBITS_URL
      @faraday = faraday || Faraday.new(url)
      @config_filename = config_filename
      @arguments = arguments
      @input_io = input_io
      @output_io = output_io
      @error_io = error_io
    end
    
    def run
      # redirect STDERR
      previous_stderr = $stderr
      $stderr = @error_io
      
      # ensure we have a sane environment
      
      # intialize a BuildGenerator
      build_generator = BuildGenerator.new(environment: environment, 
        working_dir: working_dir, 
        config_filename: File.join(working_dir, @config_filename)
        )
      # generate a build
      build = build_generator.generate_build
      # submit the build to the service
      submitter = BuildSubmitter.new(build: build, faraday: @faraday)
      response = submitter.submit_build
      if response.success?
        @output_io.puts "Build submitted successfully."
        return 0
      else
        @error_io.puts "Failed to submit build. Response: #{response}"
{'fraction_non_alphanumeric': 0.07638223074464685, 'fraction_numerical': 0.0006391818472355385, 'mean_word_length': 2.8641975308641974, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5242561', 'n_tokens_mistral': 930, 'n_tokens_neox': 891, 'n_words': 224}