<commit_before>module ZendeskAppsTools
  module APIConnection
    SUBDOMAIN    = /\A[a-z0-9][a-z0-9\-]{1,}[a-z0-9]\z/
    FULL_URL     = Regexp::new(/https?:\/\//.source + SUBDOMAIN.source + /\.zendesk\.com/.source)
    URL_TEMPLATE = 'https://%s.zendesk.com/'

    # For Ruby 2 and 2.1 compatible
    EMAIL_REGEX  = URI::MailTo.constants.include?(:EMAIL_REGEXP) ? URI::MailTo::EMAIL_REGEXP : URI::MailTo::MAILTO_REGEXP

    EMAIL_ERROR_MSG     = 'Please enter a valid email address'
    SUBDOMAIN_ERROR_MSG = 'Make sure you entered the right subdomain name '\
      '(e.g. if account url is https://mysubdomain.zendesk.com, then enter mysubdomain).'

    def prepare_api_auth
      @subdomain ||= cache.fetch('subdomain') || get_value_from_stdin('Enter your Zendesk subdomain:')
      say_error_and_exit SUBDOMAIN_ERROR_MSG unless @subdomain =~ SUBDOMAIN

      @username  ||= cache.fetch('username', @subdomain) || get_value_from_stdin('Enter your username:')
      say_error_and_exit EMAIL_ERROR_MSG unless @username =~ EMAIL_REGEX

      @password  ||= cache.fetch('password', @subdomain) || get_password_from_stdin('Enter your password:')

      cache.save 'subdomain' => @subdomain, 'username' => @username
    end

    def get_connection(encoding = :url_encoded)
      require 'net/http'
      require 'faraday'
      prepare_api_auth
      Faraday.new full_url do |f|
        f.request encoding if encoding
        f.adapter :net_http
        f.basic_auth @username, @password
      end
    end

    private

    def full_url
      if FULL_URL =~ @subdomain
        @subdomain
      else
        URL_TEMPLATE % @subdomain
      end
    end
  end
end
<commit_msg>Use a more robust URL Validation<commit_after>module ZendeskAppsTools
  module APIConnection
    # taken from zendesk_console/lib/vars.rb
    SUBDOMAIN_VALIDATION_PATTERN = /\A[a-z0-9][a-z0-9\-]{1,}[a-z0-9]\z/
    ZENDESK_URL_VALIDATION_PATTERN = /\A(http|https):\/\/[a-z0-9]+(([\.]|[\-]{1,2})[a-z0-9]+)*\.([a-z]{2,16}|[0-9]{1,3})((:[0-9]{1,5})?(\/?|\/.*))?\z/ix
    DEFAULT_URL_TEMPLATE = 'https://%s.zendesk.com/'

    # Ruby versions compatible
    EMAIL_REGEX  = URI::MailTo.constants.include?(:EMAIL_REGEXP) ? URI::MailTo::EMAIL_REGEXP : URI::MailTo::MAILTO_REGEXP

    EMAIL_ERROR_MSG = 'Please enter a valid email address.'
    PROMPT_FOR_URL  = 'Enter your Zendesk subdomain or FULL url (including protocol):'
    URL_ERROR_MSG   = [
      'URL error. Example URL: https://mysubdomain.zendesk.com ',
      'If you are using FULL url, please follow the url as shown above.',
      'If you are using URL subdomain, please enter the equivalent of \'mysubdomain\' of the url above.'
    ].join("\n")

    def prepare_api_auth
      @subdomain ||= cache.fetch('subdomain') || get_value_from_stdin(PROMPT_FOR_URL)
      say_error_and_exit URL_ERROR_MSG unless valid_subdomain? || valid_full_url?

      @username  ||= cache.fetch('username', @subdomain) || get_value_from_stdin('Enter your username:')
      say_error_and_exit EMAIL_ERROR_MSG unless valid_email?

      @password  ||= cache.fetch('password', @subdomain) || get_password_from_stdin('Enter your password:')

      cache.save 'subdomain' => @subdomain, 'username' => @username
    end

    def get_connection(encoding = :url_encoded)
      require 'net/http'
      require 'faraday'
      prepare_api_auth
      Faraday.new full_url do |f|
        f.request encoding if encoding
        f.adapter :net_http
        f.basic_auth @username, @password
      end
    end

    private

    def full_url
      valid_full_url? ? @subdomain : (DEFAULT_URL_TEMPLATE % @subdomain)
    end

    def valid_full_url?
      ZENDESK_URL_VALIDATION_PATTERN.match? @subdomain
    end

    def valid_subdomain?
      SUBDOMAIN_VALIDATION_PATTERN.match? @subdomain
    end

    def valid_email?
      EMAIL_REGEX.match? @username
    end
  end
end

{'fraction_non_alphanumeric': 0.12445019404915912, 'fraction_numerical': 0.008796895213454075, 'mean_word_length': 3.2343921139101863, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 4, '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': '118652', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1325, 'n_words': 278}