Sha256: 748f5f984371e84d6ce6ec0aafc2c3f95b9360bfeab4b94c28d04173d5259038

Contents?: true

Size: 1.11 KB

Versions: 8

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module PurlFetcher
  class Client
    # This models the JSON that we send to the server.
    DirectUploadRequest = Data.define(:checksum, :byte_size, :content_type, :filename) do
      def self.from_file(hexdigest:, byte_size:, file_name:, content_type:)
        new(checksum: hex_to_base64_digest(hexdigest),
            byte_size: byte_size,
            content_type: clean_content_type(content_type),
            filename: file_name)
      end

      def to_h
        {
          blob: { filename: filename, byte_size: byte_size, checksum: checksum,
                  content_type: self.class.clean_content_type(content_type) }
        }
      end

      def to_json(*_args)
        JSON.generate(to_h)
      end

      def self.clean_content_type(content_type)
        return "application/octet-stream" if content_type.blank?

        # ActiveStorage is expecting "application/x-stata-dta" not "application/x-stata-dta;version=14"
        content_type.split(";").first
      end

      def self.hex_to_base64_digest(hexdigest)
        [ [ hexdigest ].pack("H*") ].pack("m0")
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
purl_fetcher-client-1.5.4 lib/purl_fetcher/client/direct_upload_request.rb
purl_fetcher-client-1.5.3 lib/purl_fetcher/client/direct_upload_request.rb
purl_fetcher-client-1.5.2 lib/purl_fetcher/client/direct_upload_request.rb
purl_fetcher-client-1.5.1 lib/purl_fetcher/client/direct_upload_request.rb
purl_fetcher-client-1.5.0 lib/purl_fetcher/client/direct_upload_request.rb
purl_fetcher-client-1.4.1 lib/purl_fetcher/client/direct_upload_request.rb
purl_fetcher-client-1.4.0 lib/purl_fetcher/client/direct_upload_request.rb
purl_fetcher-client-1.2.1 lib/purl_fetcher/client/direct_upload_request.rb