Module: OllamaChat::HTTPHandling

Included in:
Chat
Defined in:
lib/ollama_chat/http_handling.rb

Overview

Provides helper methods for HTTP interactions used by the OllamaChat library. The module is mixed into various classes (e.g. OllamaChat::Chat) to give them access to a convenient, configuration‑aware HTTP client.

The primary responsibilities are:

  • Build a hash of HTTP options (e.g. SSL verification and proxy settings) based on the current configuration.
  • Perform a GET request with those options, optionally using a cache and debugging information.

The module is deliberately lightweight – it delegates the actual network call to OllamaChat::Utils::Fetcher.

Instance Method Summary collapse

Instance Method Details

#get_url(url, headers: nil, cache: nil, remember: true) {|IO| ... } ⇒ Object

Performs an HTTP GET request.

The method merges the caller‑supplied headers with the headers configured in config.request_headers?. It then delegates to OllamaChat::Utils::Fetcher.get, passing along any cache or debug flags and the HTTP options generated by http_options.

Parameters:

  • url (String)

    the target URL.

  • headers (Hash) (defaults to: nil)

    optional additional headers.

  • cache (Object) (defaults to: nil)

    optional cache object to use.

  • remember (Boolean) (defaults to: true)

    whether to add the URL to the session's links set (defaults to true).

Yields:

  • (IO)

    yields a temporary file handle for the caller to consume.

Returns:

  • (Object)

    the result of the block or the fetcher.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ollama_chat/http_handling.rb', line 63

def get_url(url, headers: nil, cache: nil, remember: true, &block)
  remember and links.add(url.to_s)
  headers = config.request_headers?.to_h | headers
  OllamaChat::Utils::Fetcher.get(
    url,
    headers:      ,
    cache:        ,
    debug:        ,
    http_options: http_options(OllamaChat::Utils::Fetcher.normalize_url(url)),
    &block
  )
end

#http_options(url) ⇒ Hash

Returns a hash of HTTP options suitable for Net::HTTP or a compatible client.

The options include:

  • :ssl_verify_peer – a boolean that disables peer verification for hostnames listed in the configuration’s ssl_no_verify set.
  • :proxy – the proxy URL if one is configured.

Parameters:

  • url (String)

    the URL for which the options should be generated.

Returns:

  • (Hash)

    a hash containing the HTTP options.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ollama_chat/http_handling.rb', line 37

def http_options(url)
  options = {}
  if ssl_no_verify = config.ssl_no_verify?
    hostname = URI.parse(url).hostname
    options |= { ssl_verify_peer: !ssl_no_verify.include?(hostname) }
  end
  if proxy = config.proxy?
    options |= { proxy: }
  end
  options
end

Returns the links set for this object, initializing it lazily if needed.

The links set is memoized, meaning it will only be created once per object instance and subsequent calls will return the same Set instance.

Returns:

  • (Set)

    A Set object containing all links associated with this instance



22
23
24
# File 'lib/ollama_chat/http_handling.rb', line 22

def links
  @links ||= OllamaChat::LinksSet.new(chat)
end