<commit_before># frozen_string_literal: true

require 'digest'
require 'addressable/uri'
require 'net/http'
require 'uri'

module Imgix
  class Client
    DEFAULTS = { use_https: true }.freeze

    def initialize(options = {})
      options = DEFAULTS.merge(options)
      host, domain = options[:host], options[:domain]

      host_deprecated = "Warning: The identifier `host' has been deprecated and " \
        "will\nappear as `domain' in the next major version, e.g. " \
        "`@host'\nbecomes `@domain', `options[:host]' becomes " \
        "`options[:domain]'.\n"

      if host
        warn host_deprecated
        @host = host
      elsif domain
        @host = domain
      else
        @host = host
      end

      validate_host!

      @secure_url_token = options[:secure_url_token]
      @api_key = options[:api_key]
      @use_https = options[:use_https]
      @include_library_param = options.fetch(:include_library_param, true)
      @library = options.fetch(:library_param, 'rb')
      @version = options.fetch(:library_version, Imgix::VERSION)
    end

    def path(path)
      p = Path.new(new_prefix, @secure_url_token, path)
      p.ixlib("#{@library}-#{@version}") if @include_library_param
      p
    end

    def purge(path)
      token_error = 'Authentication token required'
      raise token_error if @api_key.nil?

      url = new_prefix + path
      uri = URI.parse('https://api.imgix.com/v2/image/purger')

      user_agent = { 'User-Agent' => "imgix #{@library}-#{@version}" }

      req = Net::HTTP::Post.new(uri.path, user_agent)
      req.basic_auth @api_key, ''
      req.set_form_data({ url: url })

      sock = Net::HTTP.new(uri.host, uri.port)
      sock.use_ssl = true
      res = sock.start { |http| http.request(req) }<commit_msg>fix: warn api key versions deprecated<commit_after># frozen_string_literal: true

require 'digest'
require 'addressable/uri'
require 'net/http'
require 'uri'

module Imgix
  class Client
    DEFAULTS = { use_https: true }.freeze

    def initialize(options = {})
      options = DEFAULTS.merge(options)
      host, domain = options[:host], options[:domain]

      host_deprecated = "Warning: The identifier `host' has been deprecated and " \
        "will\nappear as `domain' in the next major version, e.g. " \
        "`@host'\nbecomes `@domain', `options[:host]' becomes " \
        "`options[:domain]'.\n"

      if host
        warn host_deprecated
        @host = host
      elsif domain
        @host = domain
      else
        @host = host
      end

      validate_host!

      @secure_url_token = options[:secure_url_token]
      @api_key = options[:api_key]
      @use_https = options[:use_https]
      @include_library_param = options.fetch(:include_library_param, true)
      @library = options.fetch(:library_param, 'rb')
      @version = options.fetch(:library_version, Imgix::VERSION)
    end

    def path(path)
      p = Path.new(new_prefix, @secure_url_token, path)
      p.ixlib("#{@library}-#{@version}") if @include_library_param
      p
    end

    def purge(path)
      api_key_deprecated = \
        "Warning: Your `api_key` will no longer work after upgrading to\n" \
        "imgix-rb version >= 4.0.0.\n"
      warn api_key_deprecated

      token_error = 'Authentication token required'
      raise token_error if @api_key.nil?

      url = new_prefix + path
      uri = URI.parse('https://api.imgix.com/v2/image/purger')

      user_agent = { 'User-Agent' => "imgix #{@library}-#{@version}" }

      req = Net::HTTP::Post.new(uri.path, user_agent)
      req.basic_auth @api_key, ''
      req.set_form_data({ url: url })

      sock = Net::HTTP.new(uri.host, uri.port)
      sock.use_ssl = true
      res = sock.start { |http| http.request(req) }
{'fraction_non_alphanumeric': 0.12972828982418752, 'fraction_numerical': 0.0013319126265316996, 'mean_word_length': 2.751248751248751, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '190732', 'n_tokens_mistral': 1226, 'n_tokens_neox': 1202, 'n_words': 277}