Module: OllamaChat::Utils::Fetcher::ResponseMetadata

Defined in:
lib/ollama_chat/utils/fetcher.rb

Overview

A module that extends IO objects with content type metadata and expiration tracking.

This module provides a way to attach MIME content type information and cache expiration details to IO objects, enabling them to carry metadata about their source and caching behavior. It is primarily used by fetcher implementations to decorate response objects with additional context for processing and caching decisions.

Examples:

Extending an IO object with header metadata

io = StringIO.new("content")
io.extend(OllamaChat::Utils::Fetcher::ResponseMetadata)
io.content_type = MIME::Types['text/plain'].first
io.ex = 3600

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#content_type ⇒ String

Returns the content type of the object.

Returns:

  • (String) —

    the content type of the object.



40
41
42
# File 'lib/ollama_chat/utils/fetcher.rb', line 40

def content_type
  @content_type
end

#ex ⇒ Integer

Returns the expiry value in seconds.

Returns:

  • (Integer) —

    the expiry value in seconds.



44
45
46
# File 'lib/ollama_chat/utils/fetcher.rb', line 44

def ex
  @ex
end

#response_reason ⇒ String

Returns the HTTP response reason phrase.

Returns:

  • (String) —

    the HTTP response reason phrase.



52
53
54
# File 'lib/ollama_chat/utils/fetcher.rb', line 52

def response_reason
  @response_reason
end

#response_status ⇒ Integer

Returns the HTTP response status code.

Returns:

  • (Integer) —

    the HTTP response status code.



48
49
50
# File 'lib/ollama_chat/utils/fetcher.rb', line 48

def response_status
  @response_status
end

Class Method Details

.as_text(text) ⇒ StringIO

Wraps a string in a StringIO decorated with ResponseMetadata and a text/plain content type.

Used as the shared factory for synthetic text responses (e.g. failed) and for converting plain-text tool output into an IO that the rest of the pipeline (parse_source, document_policy, etc.) can consume uniformly.

Parameters:

  • text (String) —

    the content to wrap

Returns:



89
90
91
92
93
# File 'lib/ollama_chat/utils/fetcher.rb', line 89

def self.as_text(text)
  object = StringIO.new(text).extend(self)
  object.content_type = MIME::Types['text/plain'].first
  object
end

.failed(msg) ⇒ StringIO

The failed method creates a StringIO object with a text/plain content type.

This method is used to generate a failed response object that can be used when an operation does not succeed. It initializes a new StringIO object and extends it with the current class, setting its content type to text/plain.

Parameters:

  • msg (String) —

    the error message to write into the response object.

Returns:

  • (StringIO) —

    a StringIO object with text/plain content type



74
75
76
# File 'lib/ollama_chat/utils/fetcher.rb', line 74

def self.failed(msg)
  as_text(msg)
end

Instance Method Details

#http? ⇒ Boolean

Returns true if the metadata belongs to an HTTP response.

This is determined by the presence of a response status code.

Returns:

  • (Boolean) —

    true if the response is an HTTP response, false otherwise.



60
61
62
# File 'lib/ollama_chat/utils/fetcher.rb', line 60

def http?
  !response_status.nil?
end