<commit_before>require 'net/http'
require 'json'

module IssueExporting
  class Importer
    def initialize(files, owner, repo, token)
      @files = files
      @owner = owner
      @repo = repo
      @token = token
    end

    def import
      @files.each do |file|
        file_contents = read_file file
        import_json(file_contents) unless file_contents.nil?
      end
    end

    private
    def import_json(json_obj)
      if json_obj.class == Hash
        import_json_hash json_obj
      elsif json_obj.class == Array
        import_json_array json_obj
      else
        puts "Unknown object: #{json_obj.class}"
      end
    end

    def import_json_array(json_array)
      json_array.each do |json_hash|
        import_json_hash json_hash
      end
    end

    def import_json_hash(json_hash)
      create_issue(filter_json json_hash)
    end

    def filter_json(json_hash)
      json_hash.select { |k,v| allowed_properties.include? k }
    end

    def create_issue(json_obj)
      uri = IssueExporting.make_uri @owner, @repo, @token
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true if uri.scheme == 'https'
      request = Net::HTTP::Post.new uri.request_uri
      request.content_type = 'application/json'
      request.body = json_obj.to_json
      handle_response http.request(request)
    end

    def allowed_properties
      ["title", "body", "assignee", "milestone", "labels"]
    end

    def read_file(filename)
      path = "#{Dir.pwd}/#{filename}"
      if File.exist? path
        raw_text = File.read path
        JSON.parse raw_text
      else
        handle_missing_file path
        nil
      end
    end

    def handle_response(response)
      puts response
    end

    def handle_missing_file(filename)
      puts "Cannot open file: #{filename}"
    end

  end
end<commit_msg>Handle errors on import<commit_after>require 'net/http'
require 'json'

module IssueExporting
  class Importer
    def initialize(files, owner, repo, token)
      @files = files
      @owner = owner
      @repo = repo
      @token = token
    end

    def import
      @files.each do |file|
        file_contents = read_file file
        import_json(file_contents) unless file_contents.nil?
      end
    end

    private
    def import_json(json_obj)
      if json_obj.class == Hash
        import_json_hash json_obj
      elsif json_obj.class == Array
        import_json_array json_obj
      else
        puts "Unknown object: #{json_obj.class}"
      end
    end

    def import_json_array(json_array)
      json_array.each do |json_hash|
        import_json_hash json_hash
      end
    end

    def import_json_hash(json_hash)
      create_issue(filter_json json_hash)
    end

    def filter_json(json_hash)
      json_hash.select { |k,v| allowed_properties.include? k }
    end

    def create_issue(json_obj)
      uri = IssueExporting.make_uri @owner, @repo, @token
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true if uri.scheme == 'https'
      request = Net::HTTP::Post.new uri.request_uri
      request.content_type = 'application/json'
      request.body = json_obj.to_json
      handle_response http.request(request)
    end

    def allowed_properties
      ["title", "body", "assignee", "milestone", "labels"]
    end

    def read_file(filename)
      path = "#{Dir.pwd}/#{filename}"
      if File.exist? path
        raw_text = File.read path
        JSON.parse raw_text
      else
        handle_missing_file path
        nil
      end
    end

    def handle_response(response)
      error_handler = ErrorHandler.new
      if error_handler.response_has_error response
        error_handler.handle_error error_handler.error_message(response.body), true
      end
    end

    def handle_missing_file(filename)
      puts "Cannot open file: #{filename}"
    end

  end
end
{'fraction_non_alphanumeric': 0.08065352697095436, 'fraction_numerical': 0.0, 'mean_word_length': 2.440677966101695, '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': '3801763', 'n_tokens_mistral': 1202, 'n_tokens_neox': 1184, 'n_words': 295}