Class: Ollama::Commands::BlobExists

Inherits:
Object
  • Object
show all
Includes:
Digester
Defined in:
lib/ollama/commands/blob_exists.rb

Overview

A command class that represents the blob existence check API endpoint for Ollama.

This class is used to interact with the Ollama API’s blobs endpoint using a HEAD request to verify if a specific binary blob (identified by its digest) already exists on the server. This is particularly useful for optimizing model uploads by avoiding redundant transfers of large files.

Examples:

Checking if a blob exists

exists = ollama.blob_exists?(digest: 'sha256:...')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(digest: nil, blob: nil) ⇒ BlobExists

The initialize method sets up a new instance with the target digest.

This method initializes the command object with the specific digest of the blob being checked and explicitly disables streaming as HEAD requests are inherently non-streaming.

Parameters:

  • digest (String, nil) (defaults to: nil)

    the SHA256 digest of the blob (e.g., ‘sha256:…’)

  • blob (IO, nil) (defaults to: nil)

    the binary data stream to be hashed if no digest is provided



25
26
27
28
29
30
31
32
# File 'lib/ollama/commands/blob_exists.rb', line 25

def initialize(digest: nil, blob: nil)
  digest = prefix_sha256(digest)
  if digest.nil? && blob
    digest = compute_digest(blob)
  end
  digest or raise ArgumentError, 'require digest or blob to perform'
  @digest, @stream = digest, false
end

Instance Attribute Details

#client=(value) ⇒ Object (writeonly)

The client attribute writer allows setting the client instance associated with the object.



48
49
50
# File 'lib/ollama/commands/blob_exists.rb', line 48

def client=(value)
  @client = value
end

#digestString (readonly)

The digest attribute reader returns the target blob digest.

Returns:

  • (String)

    the SHA256 digest of the blob



37
38
39
# File 'lib/ollama/commands/blob_exists.rb', line 37

def digest
  @digest
end

#streamFalseClass (readonly)

The stream attribute reader returns the streaming behavior setting.

Returns:

  • (FalseClass)

    the streaming behavior flag (always false for this command)



42
43
44
# File 'lib/ollama/commands/blob_exists.rb', line 42

def stream
  @stream
end

Instance Method Details

#pathString

The path method constructs the API endpoint URL for a specific binary blob. It interpolates the target blob’s digest into the path string to create the correct resource identifier for the Ollama API.

Returns:

  • (String)

    the API endpoint path for the blob



55
56
57
# File 'lib/ollama/commands/blob_exists.rb', line 55

def path
  "/api/blobs/#{digest}"
end

#perform(handler) ⇒ self

The perform method executes the existence check request using a HEAD method.

This method initiates a HEAD request to the ‘/api/blobs/:digest’ endpoint. A successful response (typically 200 OK) indicates the blob exists, while a 404 Not Found indicates it does not.

Parameters:

  • handler (Ollama::Handler)

    the handler object responsible for processing API responses

Returns:

  • (self)

    returns the current instance after initiating the request



69
70
71
72
# File 'lib/ollama/commands/blob_exists.rb', line 69

def perform(handler)
  @client.request(method: :head, path:, stream:, handler:)
  self
end