Class: OllamaChat::Utils::Fetcher
- Inherits:
-
Object
- Object
- OllamaChat::Utils::Fetcher
show all
- Defined in:
- lib/ollama_chat/utils/fetcher.rb
Defined Under Namespace
Modules: HeaderExtension
Classes: RetryWithoutStreaming
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(debug: false, http_options: {}) ⇒ Fetcher
Returns a new instance of Fetcher.
83
84
85
86
87
88
|
# File 'lib/ollama_chat/utils/fetcher.rb', line 83
def initialize(debug: false, http_options: {})
@debug = debug
@started = false
@streaming = true
@http_options = http_options
end
|
Class Method Details
.execute(command, &block) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/ollama_chat/utils/fetcher.rb', line 60
def self.execute(command, &block)
Tempfile.open do |tmp|
unless command =~ /2>&1/
command += ' 2>&1'
end
IO.popen(command) do |io|
until io.eof?
tmp.write io.read(1 << 14)
end
tmp.rewind
tmp.extend(OllamaChat::Utils::Fetcher::HeaderExtension)
tmp.content_type = MIME::Types['text/plain'].first
block.(tmp)
end
end
rescue => e
STDERR.puts "Cannot execute #{command.inspect} (#{e})"
if @debug && !e.is_a?(RuntimeError)
STDERR.puts "#{e.backtrace * ?\n}"
end
yield HeaderExtension.failed
end
|
.get(url, headers: {}, **options, &block) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/ollama_chat/utils/fetcher.rb', line 23
def self.get(url, headers: {}, **options, &block)
cache = options.delete(:cache) and
cache = OllamaChat::Utils::CacheFetcher.new(cache)
if result = cache&.get(url, &block)
infobar.puts "Getting #{url.to_s.inspect} from cache."
return result
else
new(**options).send(:get, url, headers:) do |tmp|
result = block.(tmp)
if cache && !tmp.is_a?(StringIO)
tmp.rewind
cache.put(url, tmp)
end
result
end
end
end
|
.normalize_url(url) ⇒ Object
41
42
43
44
45
46
|
# File 'lib/ollama_chat/utils/fetcher.rb', line 41
def self.normalize_url(url)
url = url.to_s
url = URI.decode_uri_component(url)
url = url.sub(/#.*/, '')
URI::Parser.new.escape(url).to_s
end
|
.read(filename, &block) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/ollama_chat/utils/fetcher.rb', line 48
def self.read(filename, &block)
if File.exist?(filename)
File.open(filename) do |file|
file.extend(OllamaChat::Utils::Fetcher::HeaderExtension)
file.content_type = MIME::Types.type_for(filename).first
block.(file)
end
else
STDERR.puts "File #{filename.to_s.inspect} doesn't exist."
end
end
|