<commit_before>module Socialcast
  module CommandLine
    class Authenticate
      attr_accessor :authenticate_type, :options, :params, :headers

      def initialize(authenticate_type, options, params, headers = {})
        self.authenticate_type = authenticate_type
        self.options = options
        self.params = params
        self.headers = headers
      end

      def request
        @request ||= send_request
      end

      private

      def send_request
        puts "Authenticating to #{url}"
        RestClient.log = Logger.new($stdout) if options[:trace]
        RestClient.proxy = options[:proxy] if options[:proxy]
        resource = RestClient::Resource.new url, headers
        response = resource.post params, :accept => :json
        puts "API response: #{response.body.to_s}" if options[:trace]
        set_default_credentials
        response
      end

      def set_default_credentials
        Socialcast::CommandLine.credentials = {
          :domain => domain,
          :proxy => options[:proxy]
        }
      end

      def url
        @url ||= ['https://', domain, '/api/', (authenticate_type == :external_system ? 'external_systems/' : nil), 'authentication'].join
      end

      def domain
        options[:domain]
      end
    end
  end
end<commit_msg>using URI join for URL construction<commit_after>require 'uri'

module Socialcast
  module CommandLine
    class Authenticate
      attr_accessor :authenticate_type, :options, :params, :headers

      def initialize(authenticate_type, options, params, headers = {})
        self.authenticate_type = authenticate_type
        self.options = options
        self.params = params
        self.headers = headers
      end

      def request
        @request ||= send_request
      end

      private

      def send_request
        puts "Authenticating to #{url}"
        RestClient.log = Logger.new($stdout) if options[:trace]
        RestClient.proxy = options[:proxy] if options[:proxy]
        resource = RestClient::Resource.new url, headers
        response = resource.post params, :accept => :json
        puts "API response: #{response.body.to_s}" if options[:trace]
        set_default_credentials
        response
      end

      def set_default_credentials
        Socialcast::CommandLine.credentials = {
          :domain => domain,
          :proxy => options[:proxy]
        }
      end

      def url
        @url ||= URI.join('https://', domain, '/api/', (authenticate_type == :external_system ? 'external_systems/' : nil), 'authentication').to_s
      end

      def domain
        options[:domain]
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.0967741935483871, 'fraction_numerical': 0.0, 'mean_word_length': 2.3622448979591835, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '6191365', 'n_tokens_mistral': 721, 'n_tokens_neox': 700, 'n_words': 186}