<commit_before>module Phonelib
  # phone analyzing methods module
  module PhoneAnalyzer
    # array of types not included for validation check in cycle
    NOT_FOR_CHECK = [:general_desc, :fixed_line, :mobile, :fixed_or_mobile]

    # parses provided phone if it is valid for  country data and returns result of
    # analyze
    #
    # ==== Attributes
    #
    # * +phone+ - Phone number for parsing
    # * +passed_country+ - Country provided for parsing. Must be ISO code of
    #   country (2 letters) like 'US', 'us' or :us for United States
    def analyze(phone, passed_country)
      country = country_or_default_country passed_country

      result = try_to_parse_single_country(phone, country)
      # if previous parsing failed, trying for all countries
      if result.nil? || result.values.first[:valid].empty?
        result = detect_and_parse phone
      end
      result
    end

    private

    # trying to parse phone for single country including international prefix
    # check for provided country
    #
    # ==== Attributes
    #
    # * +phone+ - phone for parsing
    # * +country+ - country to parse phone with
    def try_to_parse_single_country(phone, country)
      if country && Phonelib.phone_data[country]
        # if country was provided and it's a valid country, trying to
        # create e164 representation of phone number,
        # kind of normalization for parsing
        e164 = convert_to_e164 phone, Phonelib.phone_data[country]
        # if phone starts with international prefix of provided
        # country try to reanalyze without international prefix for
        # all countries
        return analyze(e164.gsub('+', ''), nil) if e164[0] == '+'
        # trying to parse number for provided country
        parse_single_country e164, Phonelib.phone_data[country]
      end
    end

    # method checks if phone is valid against single provided country data<commit_msg>Fix #27<commit_after>module Phonelib
  # phone analyzing methods module
  module PhoneAnalyzer
    # array of types not included for validation check in cycle
    NOT_FOR_CHECK = [:general_desc, :fixed_line, :mobile, :fixed_or_mobile]

    # parses provided phone if it is valid for  country data and returns result of
    # analyze
    #
    # ==== Attributes
    #
    # * +phone+ - Phone number for parsing
    # * +passed_country+ - Country provided for parsing. Must be ISO code of
    #   country (2 letters) like 'US', 'us' or :us for United States
    def analyze(phone, passed_country)
      country = country_or_default_country passed_country

      result = try_to_parse_single_country(phone, country)
      # if previous parsing failed, trying for all countries
      if result.nil? || result.empty? || result.values.first[:valid].empty?
        result = detect_and_parse phone
      end
      result
    end

    private

    # trying to parse phone for single country including international prefix
    # check for provided country
    #
    # ==== Attributes
    #
    # * +phone+ - phone for parsing
    # * +country+ - country to parse phone with
    def try_to_parse_single_country(phone, country)
      if country && Phonelib.phone_data[country]
        # if country was provided and it's a valid country, trying to
        # create e164 representation of phone number,
        # kind of normalization for parsing
        e164 = convert_to_e164 phone, Phonelib.phone_data[country]
        # if phone starts with international prefix of provided
        # country try to reanalyze without international prefix for
        # all countries
        return analyze(e164.gsub('+', ''), nil) if e164[0] == '+'
        # trying to parse number for provided country
        parse_single_country e164, Phonelib.phone_data[country]
      end
    end

    # method checks if phone is valid against single provided country data
{'fraction_non_alphanumeric': 0.06977950713359274, 'fraction_numerical': 0.010894941634241245, 'mean_word_length': 2.891019172552977, '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': '7224092', 'n_tokens_mistral': 1069, 'n_tokens_neox': 1015, 'n_words': 391}