Module: GeminiCache
- Defined in:
- lib/gemini_cache.rb,
lib/gemini_cache/api_client.rb,
lib/gemini_cache/configuration.rb
Defined Under Namespace
Classes: ApiClient, Configuration, Error
Class Method Summary
collapse
-
.configuration ⇒ Object
-
.configure {|configuration| ... } ⇒ Object
-
.create(parts:, display_name:, on_conflict: :raise_error, model: nil, ttl: nil) ⇒ Object
-
.create_from_local_file(path:, mime_type:, **options) ⇒ Object
-
.create_from_remote_file(url:, mime_type:, **options) ⇒ Object
-
.create_from_text(text:, **options) ⇒ Object
-
.create_from_webpage(url:, **options) ⇒ Object
-
.delete(name:) ⇒ Object
-
.delete_all ⇒ Object
(also: clear)
-
.find_by_display_name(display_name:) ⇒ Object
-
.find_by_name(name:) ⇒ Object
-
.list ⇒ Object
-
.parse_html(url:, default_remover: true) ⇒ Object
-
.read_local_file(path:, mime_type:) ⇒ Object
-
.read_remote_file(url:, mime_type:) ⇒ Object
-
.read_webpage_text(url:, default_remover: true) ⇒ Object
-
.update(name:, content:) ⇒ Object
Class Method Details
.configuration ⇒ Object
13
14
15
|
# File 'lib/gemini_cache/configuration.rb', line 13
def configuration
@configuration ||= Configuration.new
end
|
17
18
19
|
# File 'lib/gemini_cache/configuration.rb', line 17
def configure
yield(configuration)
end
|
.create(parts:, display_name:, on_conflict: :raise_error, model: nil, ttl: nil) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/gemini_cache.rb', line 31
def create(parts:, display_name:, on_conflict: :raise_error, model: nil, ttl: nil)
existing_cache = find_by_display_name(display_name:)
if existing_cache
case on_conflict
when :raise_error
raise Error, "Cache with display name '#{display_name}' already exists"
when :get_existing
return existing_cache
end
end
content = {
model: "models/#{model || configuration.default_model}",
display_name: display_name,
contents: [{ parts:, role: 'user' }],
ttl: "#{ttl || configuration.default_ttl}s"
}
response = api_client.create_cache(content.to_json)
find_by_name(name: response['name'])
end
|
.create_from_local_file(path:, mime_type:, **options) ⇒ Object
56
57
58
59
|
# File 'lib/gemini_cache.rb', line 56
def create_from_local_file(path:, mime_type:, **options)
file_data = read_local_file(path: path, mime_type: mime_type)
create(parts: [file_data], **options)
end
|
.create_from_remote_file(url:, mime_type:, **options) ⇒ Object
61
62
63
64
|
# File 'lib/gemini_cache.rb', line 61
def create_from_remote_file(url:, mime_type:, **options)
file_data = read_remote_file(url: url, mime_type: mime_type)
create(parts: [file_data], **options)
end
|
.create_from_text(text:, **options) ⇒ Object
54
|
# File 'lib/gemini_cache.rb', line 54
def create_from_text(text:, **options) = create(parts: [{ text: }], **options)
|
.create_from_webpage(url:, **options) ⇒ Object
55
|
# File 'lib/gemini_cache.rb', line 55
def create_from_webpage(url:, **options) = create_from_text(text: read_webpage_text(url:)[:text], **options)
|
.delete(name:) ⇒ Object
78
79
80
81
|
# File 'lib/gemini_cache.rb', line 78
def delete(name:)
api_client.delete_cache(name)
true
end
|
.delete_all ⇒ Object
Also known as:
clear
83
|
# File 'lib/gemini_cache.rb', line 83
def delete_all() = list.each { |item| item.delete }
|
.find_by_display_name(display_name:) ⇒ Object
74
|
# File 'lib/gemini_cache.rb', line 74
def find_by_display_name(display_name:) = list.find { |item| item['displayName'].eql?(display_name) }
|
.find_by_name(name:) ⇒ Object
73
|
# File 'lib/gemini_cache.rb', line 73
def find_by_name(name:) = list.find { |item| item['name'].eql?(name) }
|
.list ⇒ Object
66
67
68
69
70
71
|
# File 'lib/gemini_cache.rb', line 66
def list
response = api_client.list_caches
return [] if response.empty?
response['cachedContents'].map { |item| item.extend(ItemExtender) }
end
|
.parse_html(url:, default_remover: true) ⇒ Object
15
16
17
18
19
|
# File 'lib/gemini_cache.rb', line 15
def parse_html(url:, default_remover: true)
doc = Nokogiri::HTML(URI.open(url, "User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"))
%w[script style].each { |element| doc.css(element).each(&:remove) } if default_remover
doc
end
|
.read_local_file(path:, mime_type:) ⇒ Object
21
22
23
|
# File 'lib/gemini_cache.rb', line 21
def read_local_file(path:, mime_type:)
{ inline_data: { mime_type:, data: Base64.strict_encode64(File.read(path)) } }
end
|
.read_remote_file(url:, mime_type:) ⇒ Object
25
26
27
|
# File 'lib/gemini_cache.rb', line 25
def read_remote_file(url:, mime_type:)
{ inline_data: { mime_type:, data: Base64.strict_encode64(URI.open(url).read) } }
end
|
.read_webpage_text(url:, default_remover: true) ⇒ Object
29
|
# File 'lib/gemini_cache.rb', line 29
def read_webpage_text(url:, default_remover: true) = { text: parse_html(url:, default_remover:).inner_text }
|
.update(name:, content:) ⇒ Object
76
|
# File 'lib/gemini_cache.rb', line 76
def update(name:, content:) = api_client.update_cache(name, content)
|