<commit_before>require 'mechanize'
require 'security'
require 'uri'
require 'json'

module Cupertino
  module ProvisioningPortal
    class Agent < ::Mechanize
      attr_accessor :username, :password, :team

      def initialize
        super
        self.user_agent_alias = 'Mac Safari'

        get_proxy_from_env

        pw = Security::InternetPassword.find(:server => Cupertino::ProvisioningPortal::HOST)
        @username, @password = pw.attributes['acct'], pw.password if pw
      end

      def use_proxy(arg)
        puts "Using proxy #{arg}"
        puts '-----------'
        uri = URI.parse(normalize_uri(arg))
        if uri and uri.user.nil? and uri.password.nil? then
          # Probably we have http_proxy_* variables?
          user = (ENV['http_proxy_user'] || ENV['HTTP_PROXY_USER'])
          pass = (ENV['http_proxy_pass'] || ENV['HTTP_PROXY_PASS'])
          uri.user = user
          uri.password = pass
        end
        set_proxy(uri.host, uri.port, user, pass)
        uri
      end

      ##
      # Returns an HTTP proxy URI if one is set in the environment variables.
      def get_proxy_from_env
        env_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
        return nil if env_proxy.nil? or env_proxy.empty?

        use_proxy(env_proxy)
      end

      ##
      # Normalize the URI by adding "http://" if it is missing.
      def normalize_uri(uri)
        (uri =~ /^(https?|ftp|file):/) ? uri : "http://#{uri}"
      end

      def get(uri, parameters = [], referer = nil, headers = {})
        uri = ::File.join("https://#{Cupertino::ProvisioningPortal::HOST}", uri) unless /^https?/ === uri

        3.times do
          super(uri, parameters, referer, headers)

          return page unless page.respond_to?(:title)

#TODO could rescue Net::HTTP::Persistent::Error here, and if there's a proxy, suggest that the proxy is not available
          case page.title
          when /Sign in with your Apple ID/
            login! and redo
          when /Select Team/
            select_team! and redo
          else
            return page
          end
        end

        raise UnsuccessfulAuthenticationError
      end

      def list_certificates(type = :development)
        url = case type
        when :development
          "https://developer.apple.com/account/ios/certificate/certificateList.action?type=development"
        when :distribution
          "https://developer.apple.com/account/ios/certificate/certificateList.action?type=distribution"
        else
          raise ArgumentError, "Certificate type must be :development or :distribution"
        end

        get(url)

        regex = /certificateDataURL = "([^"]*)"/
        certificate_data_url = (page.body.match regex or raise UnexpectedContentError)[1]

        regex = /certificateRequestTypes = "([^"]*)"/
        certificate_request_types = (page.body.match regex or raise UnexpectedContentError)[1]<commit_msg>Minor refactoring<commit_after>require 'mechanize'
require 'security'
require 'uri'
require 'json'

module Cupertino
  module ProvisioningPortal
    class Agent < ::Mechanize
      attr_accessor :username, :password, :team

      def initialize
        super

        self.user_agent_alias = 'Mac Safari'

        if ENV['HTTP_PROXY']
          uri = URI.parse(ENV['HTTP_PROXY'])
          user = ENV['HTTP_PROXY_USER'] if ENV['HTTP_PROXY_USER']
          password = ENV['HTTP_PROXY_PASSWORD'] if ENV['HTTP_PROXY_PASSWORD']

          set_proxy(uri.host, uri.port, user || uri.user, password || uri.password)
        end

        pw = Security::InternetPassword.find(:server => Cupertino::ProvisioningPortal::HOST)
        @username, @password = pw.attributes['acct'], pw.password if pw
      end

      def get(uri, parameters = [], referer = nil, headers = {})
        uri = ::File.join("https://#{Cupertino::ProvisioningPortal::HOST}", uri) unless /^https?/ === uri

        3.times do
          super(uri, parameters, referer, headers)

          return page unless page.respond_to?(:title)

          case page.title
          when /Sign in with your Apple ID/
            login! and redo
          when /Select Team/
            select_team! and redo
          else
            return page
          end
        end

        raise UnsuccessfulAuthenticationError
      end

      def list_certificates(type = :development)
        url = case type
        when :development
          "https://developer.apple.com/account/ios/certificate/certificateList.action?type=development"
        when :distribution
          "https://developer.apple.com/account/ios/certificate/certificateList.action?type=distribution"
        else
          raise ArgumentError, "Certificate type must be :development or :distribution"
        end

        get(url)

        regex = /certificateDataURL = "([^"]*)"/
        certificate_data_url = (page.body.match regex or raise UnexpectedContentError)[1]

        regex = /certificateRequestTypes = "([^"]*)"/
        certificate_request_types = (page.body.match regex or raise UnexpectedContentError)[1]
{'fraction_non_alphanumeric': 0.10860979462875198, 'fraction_numerical': 0.001184834123222749, 'mean_word_length': 2.4038978494623655, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 6, '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': '3858655', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1427, 'n_words': 364}