<commit_before>require 'intercom_export/reference'
require 'intercom_export/model/intercom_conversation'
require 'intercom_export/model/intercom_user'
require 'intercom_export/model/intercom_admin'

module IntercomExport
  module Splitter
    class Intercom
      def initialize(client)
        @client = client
      end

      def split(conversation)
        conversation_hash = flatten(conversation)

        dependencies_for_conversation!(conversation_hash) + [
          IntercomExport::Model::IntercomConversation.new(conversation_hash)
        ]
      end

      private

      attr_reader :client

      def flatten(conversation)
        hash = conversation.to_hash
        hash['conversation_parts'] = hash.fetch('conversation_parts').map { |p| p.to_hash }
        hash['conversation_message'] = hash.fetch('conversation_message').to_hash
        hash
      end

      def model_for(object)
        case object
        when ::Intercom::User
          load_user(object)
        when ::Intercom::Admin
          load_admin(object)
        when ::Intercom::NobodyAdmin
          nil
        else
          raise "Unrecognised object #{object.inspect}"
        end
      end

      def load_user(object)
        IntercomExport::Model::IntercomUser.new(client.users.find(id: object.id).to_hash)
      end

      def load_admin(object)
        IntercomExport::Model::IntercomAdmin.new(
          client.admins.all.find { |a| a.id == Integer(object.id) }.to_hash
        )
      end

      def replace_reference!(hash, key)
        value = hash[key]<commit_msg>Handle cases where Intercom admin is not found<commit_after>require 'intercom_export/reference'
require 'intercom_export/model/intercom_conversation'
require 'intercom_export/model/intercom_user'
require 'intercom_export/model/intercom_admin'

module IntercomExport
  module Splitter
    class Intercom
      def initialize(client)
        @client = client
      end

      def split(conversation)
        conversation_hash = flatten(conversation)

        dependencies_for_conversation!(conversation_hash) + [
          IntercomExport::Model::IntercomConversation.new(conversation_hash)
        ]
      end

      private

      attr_reader :client

      def flatten(conversation)
        hash = conversation.to_hash
        hash['conversation_parts'] = hash.fetch('conversation_parts').map { |p| p.to_hash }
        hash['conversation_message'] = hash.fetch('conversation_message').to_hash
        hash
      end

      def model_for(object)
        case object
        when ::Intercom::User
          load_user(object)
        when ::Intercom::Admin
          load_admin(object)
        when ::Intercom::NobodyAdmin
          nil
        else
          raise "Unrecognised object #{object.inspect}"
        end
      end

      def load_user(object)
        IntercomExport::Model::IntercomUser.new(client.users.find(id: object.id).to_hash)
      end

      def load_admin(object)
        admin = client.admins.all.find { |a| a.id == Integer(object.id) }
        return unless admin
        IntercomExport::Model::IntercomAdmin.new(admin.to_hash)
      end

      def replace_reference!(hash, key)
        value = hash[key]
{'fraction_non_alphanumeric': 0.08936970837253057, 'fraction_numerical': 0.0, 'mean_word_length': 2.6666666666666665, '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': '4878985', 'n_tokens_mistral': 924, 'n_tokens_neox': 899, 'n_words': 169}