<commit_before>require "imgix"
require "forwardable"
require "net/http"
require "uri"

class Shrine
  module Storage
    class Imgix
      PURGE_URL = "https://api.imgix.com/v2/image/purger"

      attr_reader :client, :storage

      def initialize(storage:, **options)
        @client = ::Imgix::Client.new(options)
        @token = options[:token]
        @storage = storage
      end

      extend Forwardable
      delegate [:upload, :download, :open, :read, :exists?, :clear!] => :storage

      def move(io, id, metadata = {})
        @storage.move(io, id, metadata)
        purge(io.id)
      end

      def movable?(io, id)
        @storage.movable?(io, id) if @storage.respond_to?(:movable?)
      end

      def delete(id)
        @storage.delete(id)
        purge(id)
      end

      # Imgix-specific method
      def purge(id)
        uri = URI.parse(PURGE_URL)
        uri.user = @token

        post(uri, "url" => url(id))
      end

      def url(id, **options)
        client.path(id).to_url(**options)
      end

      private

      def post(uri, params = {})
        response = Net::HTTP.post_form(uri, params)
        response.error! if (400..599).cover?(response.code.to_i)
        response
      end
    end
  end
end
<commit_msg>Add API documentation<commit_after>require "imgix"
require "forwardable"
require "net/http"
require "uri"

class Shrine
  module Storage
    class Imgix
      PURGE_URL = "https://api.imgix.com/v2/image/purger"

      attr_reader :client, :storage

      # We initialize the Imgix client, and save the storage. We additionally
      # save the token as well, because `Imgix::Client` doesn't provide a
      # reader for the token.
      def initialize(storage:, **options)
        @client = ::Imgix::Client.new(options)
        @token = options[:token]
        @storage = storage
      end

      # We delegate all methods that are the same.
      extend Forwardable
      delegate [:upload, :download, :open, :read, :exists?, :clear!] => :storage

      # Purges the file from the source storage after moving it.
      def move(io, id, metadata = {})
        @storage.move(io, id, metadata)
        purge(io.id)
      end

      def movable?(io, id)
        @storage.movable?(io, id) if @storage.respond_to?(:movable?)
      end

      # Purges the deleted file.
      def delete(id)
        @storage.delete(id)
        purge(id)
      end

      # Removes the file from Imgix, along with the generated versions.
      def purge(id)
        uri = URI.parse(PURGE_URL)
        uri.user = @token

        post(uri, "url" => url(id))
      end

      # Generates an Imgix URL to the file. All options passed in will be
      # transformed into URL parameters, check out the [reference] for all
      # available query parameters.
      #
      # [reference]: https://www.imgix.com/docs/reference
      def url(id, **options)
        client.path(id).to_url(**options)
      end

      private

      def post(uri, params = {})
        response = Net::HTTP.post_form(uri, params)
        response.error! if (400..599).cover?(response.code.to_i)
        response
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.11751592356687898, 'fraction_numerical': 0.0044585987261146496, 'mean_word_length': 2.2381443298969073, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6059200', 'n_tokens_mistral': 1005, 'n_tokens_neox': 975, 'n_words': 271}