gemini_cache 0.0.11 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +118 -110
- data/lib/gemini_cache/api_client.rb +1 -26
- data/lib/gemini_cache/configuration.rb +1 -15
- data/lib/gemini_cache/item_extender.rb +3 -19
- data/lib/gemini_cache.rb +13 -93
- metadata +3 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c7e9a54d31ede523084b82ed3d94912bcbbd33ac2f0452bbc0a87d22ddc3d749
|
4
|
+
data.tar.gz: 7ca621fad09fc18f1a87938d2a42b289841ed7c9a6eaadd290869af4cd132613
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7281d91213fa3b6243e6013e352653b89e15c0be1545ad62c11c97f3259f7afcdb673ffd82d9130c3fd61087ef555844ebe35f684c5ebb0ccd1e9fa0a9a65f7f
|
7
|
+
data.tar.gz: 9648a180a9c48e3ddddc52d3006603b9a8b8f418712474311c578660d8c329ceb4ca5ee23f89ab6262eb4058818e2289910dc58979702662852d50aa3ed2a85f
|
data/README.md
CHANGED
@@ -1,145 +1,153 @@
|
|
1
|
-
# GeminiCache
|
1
|
+
# GeminiCache Module Usage Documentation
|
2
2
|
|
3
|
-
|
3
|
+
## Introduction
|
4
|
+
The `GeminiCache` module is a library designed for managing API caches, with features to process web pages, local and remote files, as well as interact with an API to create, update, list, and delete caches. This document describes its functionalities and usage.
|
4
5
|
|
5
|
-
##
|
6
|
+
## Requirements
|
7
|
+
To use the module, ensure the following Ruby libraries are installed:
|
6
8
|
|
7
|
-
|
9
|
+
- `faraday`
|
10
|
+
- `open-uri`
|
11
|
+
- `nokogiri`
|
12
|
+
- `json`
|
13
|
+
- `base64`
|
8
14
|
|
9
|
-
|
10
|
-
gem 'gemini_cache'
|
11
|
-
```
|
12
|
-
|
13
|
-
E então execute:
|
14
|
-
|
15
|
-
```bash
|
16
|
-
$ bundle install
|
17
|
-
```
|
18
|
-
|
19
|
-
Ou instale manualmente:
|
20
|
-
|
21
|
-
```bash
|
22
|
-
$ gem install gemini_cache
|
23
|
-
```
|
24
|
-
|
25
|
-
## Configuração
|
15
|
+
## Code Structure
|
26
16
|
|
27
|
-
|
17
|
+
The module includes the following components:
|
28
18
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
export GEMINI_API_KEY='sua_chave_api_aqui'
|
33
|
-
```
|
19
|
+
1. **Classes and Modules**:
|
20
|
+
- `GeminiCache::Error`: Class for handling custom errors.
|
21
|
+
- `GeminiCache`: Contains the main methods for cache management and file processing.
|
34
22
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
config.api_key = 'sua_chave_api_aqui'
|
40
|
-
config.timeout = 30 # opcional, tempo limite em segundos
|
41
|
-
config.cache_dir = '/path/to/cache' # opcional, diretório para cache local
|
42
|
-
end
|
43
|
-
```
|
23
|
+
2. **Dependencies**:
|
24
|
+
- `gemini_cache/configuration`
|
25
|
+
- `gemini_cache/api_client`
|
26
|
+
- `gemini_cache/item_extender`
|
44
27
|
|
45
|
-
##
|
28
|
+
## Features
|
46
29
|
|
47
|
-
###
|
30
|
+
### 1. HTML Parsing
|
31
|
+
Allows processing and cleaning the content of a web page.
|
48
32
|
|
33
|
+
#### Syntax
|
49
34
|
```ruby
|
50
|
-
|
35
|
+
GeminiCache.parse_html(url:, default_remover: true)
|
51
36
|
```
|
37
|
+
- **Parameters**:
|
38
|
+
- `url`: The URL of the page to process.
|
39
|
+
- `default_remover`: Automatically removes `<script>` and `<style>` elements (default: `true`).
|
40
|
+
- **Returns**: A `Nokogiri::HTML` object containing the processed HTML.
|
52
41
|
|
53
|
-
###
|
54
|
-
|
55
|
-
#### Armazenando dados no cache
|
42
|
+
### 2. File Reading
|
56
43
|
|
44
|
+
#### a) Local Files
|
45
|
+
Reads a local file and returns its Base64 encoded data.
|
57
46
|
```ruby
|
58
|
-
|
59
|
-
cache.set('minha_chave', 'meu_valor')
|
60
|
-
|
61
|
-
# Armazena com tempo de expiração (em segundos)
|
62
|
-
cache.set('minha_chave', 'meu_valor', expires_in: 3600)
|
47
|
+
GeminiCache.read_local_file(path:, mime_type:)
|
63
48
|
```
|
49
|
+
- **Parameters**:
|
50
|
+
- `path`: Path to the file.
|
51
|
+
- `mime_type`: MIME type of the file.
|
52
|
+
- **Returns**: Hash containing the encoded data.
|
64
53
|
|
65
|
-
####
|
66
|
-
|
54
|
+
#### b) Remote Files
|
55
|
+
Reads a remote file and returns its Base64 encoded data.
|
67
56
|
```ruby
|
68
|
-
|
69
|
-
valor = cache.get('minha_chave')
|
70
|
-
|
71
|
-
# Recupera com valor padrão se a chave não existir
|
72
|
-
valor = cache.get('minha_chave', default: 'valor_padrao')
|
57
|
+
GeminiCache.read_remote_file(url:, mime_type:)
|
73
58
|
```
|
59
|
+
- **Parameters**:
|
60
|
+
- `url`: URL of the file.
|
61
|
+
- `mime_type`: MIME type of the file.
|
62
|
+
- **Returns**: Hash containing the encoded data.
|
74
63
|
|
75
|
-
|
76
|
-
|
64
|
+
### 3. Webpage Text Reading
|
65
|
+
Extracts text content from a web page, removing unnecessary elements.
|
77
66
|
```ruby
|
78
|
-
|
79
|
-
cache.delete('minha_chave')
|
80
|
-
|
81
|
-
# Limpa todo o cache
|
82
|
-
cache.clear
|
67
|
+
GeminiCache.read_webpage_text(url:, default_remover: true)
|
83
68
|
```
|
69
|
+
- **Parameters**:
|
70
|
+
- `url`: URL of the page.
|
71
|
+
- `default_remover`: Automatically removes `<script>` and `<style>` elements (default: `true`).
|
72
|
+
- **Returns**: Hash containing the page text.
|
84
73
|
|
85
|
-
###
|
86
|
-
|
87
|
-
#### Cache em Lote
|
74
|
+
### 4. Cache Creation
|
75
|
+
Creates a new cache from different data sources.
|
88
76
|
|
77
|
+
#### General Syntax
|
89
78
|
```ruby
|
90
|
-
|
91
|
-
cache.set_multi({
|
92
|
-
'chave1' => 'valor1',
|
93
|
-
'chave2' => 'valor2'
|
94
|
-
})
|
95
|
-
|
96
|
-
# Recupera múltiplos valores
|
97
|
-
valores = cache.get_multi(['chave1', 'chave2'])
|
79
|
+
GeminiCache.create(parts:, display_name:, on_conflict: :raise_error, model: nil, ttl: nil)
|
98
80
|
```
|
99
|
-
|
100
|
-
|
101
|
-
|
81
|
+
- **Parameters**:
|
82
|
+
- `parts`: Cache data.
|
83
|
+
- `display_name`: Display name of the cache.
|
84
|
+
- `on_conflict`: Action on conflict (`:raise_error` or `:get_existing`).
|
85
|
+
- `model`: Model used (default: system configuration).
|
86
|
+
- `ttl`: Time-to-live for the cache (default: system configuration).
|
87
|
+
- **Returns**: The created cache object.
|
88
|
+
|
89
|
+
#### Creation Methods
|
90
|
+
- Text:
|
91
|
+
```ruby
|
92
|
+
GeminiCache.create_from_text(text:, **options)
|
93
|
+
```
|
94
|
+
- Web Page:
|
95
|
+
```ruby
|
96
|
+
GeminiCache.create_from_webpage(url:, **options)
|
97
|
+
```
|
98
|
+
- Local File:
|
99
|
+
```ruby
|
100
|
+
GeminiCache.create_from_local_file(path:, mime_type:, **options)
|
101
|
+
```
|
102
|
+
- Remote File:
|
103
|
+
```ruby
|
104
|
+
GeminiCache.create_from_remote_file(url:, mime_type:, **options)
|
105
|
+
```
|
106
|
+
|
107
|
+
### 5. Cache Management
|
108
|
+
|
109
|
+
#### Cache Listing
|
110
|
+
Lists all available caches.
|
102
111
|
```ruby
|
103
|
-
|
104
|
-
resultado = cache.fetch('minha_chave') do
|
105
|
-
# código computacionalmente intensivo aqui
|
106
|
-
resultado_computado
|
107
|
-
end
|
112
|
+
GeminiCache.list
|
108
113
|
```
|
109
|
-
|
110
|
-
|
111
|
-
|
114
|
+
- **Returns**: Array of cache objects.
|
115
|
+
|
116
|
+
#### Cache Retrieval
|
117
|
+
- By Name:
|
118
|
+
```ruby
|
119
|
+
GeminiCache.find_by_name(name:)
|
120
|
+
```
|
121
|
+
- By Display Name:
|
122
|
+
```ruby
|
123
|
+
GeminiCache.find_by_display_name(display_name:)
|
124
|
+
```
|
125
|
+
|
126
|
+
#### Cache Updating
|
127
|
+
Updates an existing cache.
|
112
128
|
```ruby
|
113
|
-
|
114
|
-
resposta = cache.fetch('consulta_gemini') do
|
115
|
-
gemini_client.generate_content('Qual é o sentido da vida?')
|
116
|
-
end
|
117
|
-
|
118
|
-
# Cache com namespace para diferentes modelos
|
119
|
-
cache_pro = GeminiCache::Client.new(namespace: 'gemini-pro')
|
120
|
-
cache_vision = GeminiCache::Client.new(namespace: 'gemini-vision')
|
129
|
+
GeminiCache.update(name:, content:)
|
121
130
|
```
|
131
|
+
- **Parameters**:
|
132
|
+
- `name`: Name of the cache.
|
133
|
+
- `content`: Updated content.
|
122
134
|
|
123
|
-
|
124
|
-
|
125
|
-
```ruby
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
end
|
133
|
-
```
|
135
|
+
#### Cache Deletion
|
136
|
+
- By Name:
|
137
|
+
```ruby
|
138
|
+
GeminiCache.delete(name:)
|
139
|
+
```
|
140
|
+
- All Caches:
|
141
|
+
```ruby
|
142
|
+
GeminiCache.delete_all
|
143
|
+
```
|
134
144
|
|
135
|
-
##
|
145
|
+
## Configuration
|
146
|
+
The methods use configurations defined in the `gemini_cache/configuration` module, and communication is handled via `gemini_cache/api_client`.
|
136
147
|
|
137
|
-
|
138
|
-
|
139
|
-
3. Faça commit das suas alterações (`git commit -am 'Adiciona nova feature'`)
|
140
|
-
4. Faça push para a branch (`git push origin feature/nova-feature`)
|
141
|
-
5. Crie um novo Pull Request
|
148
|
+
## Errors
|
149
|
+
In case of conflicts or API errors, the module raises a custom exception `GeminiCache::Error`.
|
142
150
|
|
143
|
-
##
|
151
|
+
## Conclusion
|
152
|
+
This documentation covers the main functionalities of the `GeminiCache` module. For more details on specific configurations, refer to the source code or official documentation.
|
144
153
|
|
145
|
-
Esta gem está disponível como código aberto sob os termos da [Licença MIT](https://opensource.org/licenses/MIT).
|
@@ -1,10 +1,7 @@
|
|
1
1
|
module GeminiCache
|
2
|
-
# Client for making HTTP requests to the Gemini API
|
3
2
|
class ApiClient
|
4
|
-
# Error class for API-related errors
|
5
3
|
class ApiError < StandardError; end
|
6
4
|
|
7
|
-
# Initializes a new API client
|
8
5
|
def initialize
|
9
6
|
@conn = Faraday.new(
|
10
7
|
url: GeminiCache.configuration.api_base_url,
|
@@ -12,10 +9,6 @@ module GeminiCache
|
|
12
9
|
)
|
13
10
|
end
|
14
11
|
|
15
|
-
# Creates a new cache
|
16
|
-
# @param content [String] JSON string of cache content
|
17
|
-
# @return [Hash] API response
|
18
|
-
# @raise [ApiError] if the request fails
|
19
12
|
def create_cache(content)
|
20
13
|
response = @conn.post('/v1beta/cachedContents') do |req|
|
21
14
|
req.params['key'] = api_key
|
@@ -25,9 +18,6 @@ module GeminiCache
|
|
25
18
|
handle_response(response)
|
26
19
|
end
|
27
20
|
|
28
|
-
# Lists all caches
|
29
|
-
# @return [Hash] API response
|
30
|
-
# @raise [ApiError] if the request fails
|
31
21
|
def list_caches
|
32
22
|
response = @conn.get('/v1beta/cachedContents') do |req|
|
33
23
|
req.params['key'] = api_key
|
@@ -36,11 +26,6 @@ module GeminiCache
|
|
36
26
|
handle_response(response)
|
37
27
|
end
|
38
28
|
|
39
|
-
# Updates an existing cache
|
40
|
-
# @param name [String] cache name
|
41
|
-
# @param content [String] JSON string of new content
|
42
|
-
# @return [Hash] API response
|
43
|
-
# @raise [ApiError] if the request fails
|
44
29
|
def update_cache(name, content)
|
45
30
|
response = @conn.patch("/v1beta/#{name}") do |req|
|
46
31
|
req.params['key'] = api_key
|
@@ -50,10 +35,6 @@ module GeminiCache
|
|
50
35
|
handle_response(response)
|
51
36
|
end
|
52
37
|
|
53
|
-
# Deletes a cache
|
54
|
-
# @param name [String] cache name
|
55
|
-
# @return [Hash] API response
|
56
|
-
# @raise [ApiError] if the request fails
|
57
38
|
def delete_cache(name)
|
58
39
|
response = @conn.delete("/v1beta/#{name}") do |req|
|
59
40
|
req.params['key'] = api_key
|
@@ -64,16 +45,10 @@ module GeminiCache
|
|
64
45
|
|
65
46
|
private
|
66
47
|
|
67
|
-
# Gets the API key from configuration or environment
|
68
|
-
# @return [String] API key
|
69
48
|
def api_key
|
70
49
|
GeminiCache.configuration.api_key || ENV.fetch('GEMINI_API_KEY')
|
71
50
|
end
|
72
51
|
|
73
|
-
# Handles API responses
|
74
|
-
# @param response [Faraday::Response] HTTP response
|
75
|
-
# @return [Hash] parsed response body
|
76
|
-
# @raise [ApiError] if response status is not 200
|
77
52
|
def handle_response(response)
|
78
53
|
return JSON.parse(response.body) if response.status == 200
|
79
54
|
|
@@ -83,4 +58,4 @@ module GeminiCache
|
|
83
58
|
raise ApiError, "Network error: #{e.message}"
|
84
59
|
end
|
85
60
|
end
|
86
|
-
end
|
61
|
+
end
|
@@ -1,13 +1,7 @@
|
|
1
1
|
module GeminiCache
|
2
|
-
# Configuration class for GeminiCache settings
|
3
|
-
# @attr [String] api_key The API key for Gemini API
|
4
|
-
# @attr [String] api_base_url The base URL for the Gemini API
|
5
|
-
# @attr [String] default_model The default model to use
|
6
|
-
# @attr [Integer] default_ttl The default time-to-live in seconds
|
7
2
|
class Configuration
|
8
3
|
attr_accessor :api_key, :api_base_url, :default_model, :default_ttl
|
9
4
|
|
10
|
-
# Initializes a new Configuration with default values
|
11
5
|
def initialize
|
12
6
|
@api_base_url = 'https://generativelanguage.googleapis.com'
|
13
7
|
@default_model = 'gemini-1.5-flash-8b'
|
@@ -16,20 +10,12 @@ module GeminiCache
|
|
16
10
|
end
|
17
11
|
|
18
12
|
class << self
|
19
|
-
# @return [Configuration] current configuration
|
20
13
|
def configuration
|
21
14
|
@configuration ||= Configuration.new
|
22
15
|
end
|
23
16
|
|
24
|
-
# Configures GeminiCache
|
25
|
-
# @yield [Configuration] configuration object
|
26
|
-
# @example
|
27
|
-
# GeminiCache.configure do |config|
|
28
|
-
# config.api_key = 'your-api-key'
|
29
|
-
# config.default_ttl = 600
|
30
|
-
# end
|
31
17
|
def configure
|
32
18
|
yield(configuration)
|
33
19
|
end
|
34
20
|
end
|
35
|
-
end
|
21
|
+
end
|
@@ -5,24 +5,12 @@ module ItemExtender
|
|
5
5
|
DEFAULT_TIMEOUT = 300 # seconds
|
6
6
|
ACCURATE_MODE_CONFIG = { temperature: 0, topP: 0, topK: 1 }.freeze
|
7
7
|
|
8
|
-
|
9
|
-
# @return [void]
|
10
|
-
def delete
|
11
|
-
GeminiCache.delete(name: self['name'])
|
12
|
-
end
|
8
|
+
def delete = GeminiCache.delete(name: self['name'])
|
13
9
|
|
14
|
-
# Updates the TTL of the cached item
|
15
|
-
# @param new_ttl [Integer] new TTL value in seconds
|
16
|
-
# @return [void]
|
17
10
|
def ttl=(new_ttl)
|
18
11
|
GeminiCache.update(name: self['name'], content: { ttl: "#{new_ttl}s" }.to_json)
|
19
12
|
end
|
20
|
-
|
21
|
-
# Generates content using the Gemini API
|
22
|
-
# @param contents [Array<Hash>] array of content parts
|
23
|
-
# @param generation_config [Hash, nil] optional generation configuration
|
24
|
-
# @return [Hash] response with added #content method
|
25
|
-
# @raise [GeminiAPIError] when the API request fails
|
13
|
+
|
26
14
|
def generate_content(contents:, generation_config: nil)
|
27
15
|
response = api_client.post(generate_content_endpoint) do |req|
|
28
16
|
req.params['key'] = ENV.fetch('GEMINI_API_KEY')
|
@@ -33,11 +21,7 @@ module ItemExtender
|
|
33
21
|
rescue Faraday::Error => e
|
34
22
|
raise GeminiAPIError, "Request failed: #{e.message}"
|
35
23
|
end
|
36
|
-
|
37
|
-
# Generates content from a single prompt
|
38
|
-
# @param prompt [String] the input prompt
|
39
|
-
# @param generation_config [Hash, Symbol] generation configuration or :accurate_mode
|
40
|
-
# @return [String] generated content
|
24
|
+
|
41
25
|
def single_prompt(prompt:, generation_config: :accurate_mode)
|
42
26
|
config = generation_config.eql?(:accurate_mode) ? ACCURATE_MODE_CONFIG : generation_config
|
43
27
|
|
data/lib/gemini_cache.rb
CHANGED
@@ -8,56 +8,26 @@ require 'gemini_cache/configuration'
|
|
8
8
|
require 'gemini_cache/api_client'
|
9
9
|
require 'gemini_cache/item_extender'
|
10
10
|
|
11
|
-
# Module for interacting with Google's Gemini API cached contents
|
12
|
-
# @example Basic usage
|
13
|
-
# GeminiCache.configure do |config|
|
14
|
-
# config.api_key = 'your-api-key'
|
15
|
-
# end
|
16
|
-
#
|
17
|
-
# # Create a cache from text
|
18
|
-
# cache = GeminiCache.create_from_text(
|
19
|
-
# text: "Hello, world!",
|
20
|
-
# display_name: "my-cache"
|
21
|
-
# )
|
22
11
|
module GeminiCache
|
23
|
-
# Custom error class for GeminiCache-specific errors
|
24
12
|
class Error < StandardError; end
|
25
13
|
|
26
14
|
class << self
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
15
|
+
def parse_html(url:, default_remover: true)
|
16
|
+
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"))
|
17
|
+
%w[script style].each { |element| doc.css(element).each(&:remove) } if default_remover
|
18
|
+
doc
|
19
|
+
end
|
20
|
+
|
31
21
|
def read_local_file(path:, mime_type:)
|
32
22
|
{ inline_data: { mime_type:, data: Base64.strict_encode64(File.read(path)) } }
|
33
23
|
end
|
34
24
|
|
35
|
-
# Reads a remote file and prepares it for the Gemini API
|
36
|
-
# @param url [String] URL of the remote file
|
37
|
-
# @param mime_type [String] MIME type of the file
|
38
|
-
# @return [Hash] formatted data for the API
|
39
25
|
def read_remote_file(url:, mime_type:)
|
40
26
|
{ inline_data: { mime_type:, data: Base64.strict_encode64(URI.open(url).read) } }
|
41
27
|
end
|
42
28
|
|
43
|
-
|
44
|
-
# @param url [String] URL of the webpage
|
45
|
-
# @param default_remover [Boolean] whether to remove script and style tags
|
46
|
-
# @return [Nokogiri::HTML::Document] parsed HTML document
|
47
|
-
def read_html(url:, default_remover: true)
|
48
|
-
doc = Nokogiri::HTML(URI.open(url))
|
49
|
-
%w[script style].each { |element| doc.css(element).each(&:remove) } if default_remover
|
50
|
-
doc
|
51
|
-
end
|
29
|
+
def read_webpage_text(url:, default_remover: true) = { text: parse_html(url:, default_remover:).inner_text }
|
52
30
|
|
53
|
-
# Creates a new cache in the Gemini API
|
54
|
-
# @param parts [Array<Hash>] content parts to cache
|
55
|
-
# @param display_name [String] unique display name for the cache
|
56
|
-
# @param on_conflict [:raise_error, :get_existing] action to take if cache exists
|
57
|
-
# @param model [String, nil] Gemini model to use
|
58
|
-
# @param ttl [Integer, nil] time-to-live in seconds
|
59
|
-
# @return [Hash] created cache data
|
60
|
-
# @raise [Error] if cache already exists and on_conflict is :raise_error
|
61
31
|
def create(parts:, display_name:, on_conflict: :raise_error, model: nil, ttl: nil)
|
62
32
|
existing_cache = find_by_display_name(display_name:)
|
63
33
|
|
@@ -81,44 +51,18 @@ module GeminiCache
|
|
81
51
|
find_by_name(name: response['name'])
|
82
52
|
end
|
83
53
|
|
84
|
-
|
85
|
-
|
86
|
-
# @param options [Hash] additional options passed to #create
|
87
|
-
# @return [Hash] created cache data
|
88
|
-
def create_from_text(text:, **options)
|
89
|
-
create(parts: [{ text: }], **options)
|
90
|
-
end
|
91
|
-
|
92
|
-
# Creates a cache from a webpage's content
|
93
|
-
# @param url [String] URL of the webpage
|
94
|
-
# @param options [Hash] additional options passed to #create
|
95
|
-
# @return [Hash] created cache data
|
96
|
-
def create_from_webpage(url:, **options)
|
97
|
-
create_from_text(text: read_html(url:).inner_text, **options)
|
98
|
-
end
|
99
|
-
|
100
|
-
# Creates a cache from a local file
|
101
|
-
# @param path [String] path to the local file
|
102
|
-
# @param mime_type [String] MIME type of the file
|
103
|
-
# @param options [Hash] additional options passed to #create
|
104
|
-
# @return [Hash] created cache data
|
54
|
+
def create_from_text(text:, **options) = create(parts: [{ text: }], **options)
|
55
|
+
def create_from_webpage(url:, **options) = create_from_text(text: read_webpage_text(url:)[:text], **options)
|
105
56
|
def create_from_local_file(path:, mime_type:, **options)
|
106
57
|
file_data = read_local_file(path: path, mime_type: mime_type)
|
107
58
|
create(parts: [file_data], **options)
|
108
59
|
end
|
109
60
|
|
110
|
-
# Creates a cache from a remote file
|
111
|
-
# @param url [String] URL of the remote file
|
112
|
-
# @param mime_type [String] MIME type of the file
|
113
|
-
# @param options [Hash] additional options passed to #create
|
114
|
-
# @return [Hash] created cache data
|
115
61
|
def create_from_remote_file(url:, mime_type:, **options)
|
116
62
|
file_data = read_remote_file(url: url, mime_type: mime_type)
|
117
63
|
create(parts: [file_data], **options)
|
118
64
|
end
|
119
65
|
|
120
|
-
# Lists all available caches
|
121
|
-
# @return [Array<Hash>] list of caches with ItemExtender mixed in
|
122
66
|
def list
|
123
67
|
response = api_client.list_caches
|
124
68
|
return [] if response.empty?
|
@@ -126,41 +70,17 @@ module GeminiCache
|
|
126
70
|
response['cachedContents'].map { |item| item.extend(ItemExtender) }
|
127
71
|
end
|
128
72
|
|
129
|
-
|
130
|
-
|
131
|
-
# @return [Hash, nil] cache data if found, nil otherwise
|
132
|
-
def find_by_name(name:)
|
133
|
-
list.find { |item| item['name'].eql?(name) }
|
134
|
-
end
|
135
|
-
|
136
|
-
# Finds a cache by its display name
|
137
|
-
# @param display_name [String] display name of the cache
|
138
|
-
# @return [Hash, nil] cache data if found, nil otherwise
|
139
|
-
def find_by_display_name(display_name:)
|
140
|
-
list.find { |item| item['displayName'].eql?(display_name) }
|
141
|
-
end
|
73
|
+
def find_by_name(name:) = list.find { |item| item['name'].eql?(name) }
|
74
|
+
def find_by_display_name(display_name:) = list.find { |item| item['displayName'].eql?(display_name) }
|
142
75
|
|
143
|
-
|
144
|
-
# @param name [String] internal name of the cache
|
145
|
-
# @param content [Hash] new content for the cache
|
146
|
-
# @return [Hash] updated cache data
|
147
|
-
def update(name:, content:)
|
148
|
-
api_client.update_cache(name, content)
|
149
|
-
end
|
76
|
+
def update(name:, content:) = api_client.update_cache(name, content)
|
150
77
|
|
151
|
-
# Deletes a specific cache
|
152
|
-
# @param name [String] internal name of the cache to delete
|
153
|
-
# @return [Boolean] true if successful
|
154
78
|
def delete(name:)
|
155
79
|
api_client.delete_cache(name)
|
156
80
|
true
|
157
81
|
end
|
158
82
|
|
159
|
-
|
160
|
-
# @return [void]
|
161
|
-
def delete_all
|
162
|
-
list.each { |item| item.delete }
|
163
|
-
end
|
83
|
+
def delete_all() = list.each { |item| item.delete }
|
164
84
|
alias clear delete_all
|
165
85
|
|
166
86
|
private
|
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gemini_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gedean Dias
|
8
|
-
autorequire:
|
9
8
|
bindir: bin
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 2025-01-06 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: faraday
|
@@ -67,7 +66,6 @@ homepage: https://github.com/gedean/gemini_cache
|
|
67
66
|
licenses:
|
68
67
|
- MIT
|
69
68
|
metadata: {}
|
70
|
-
post_install_message:
|
71
69
|
rdoc_options: []
|
72
70
|
require_paths:
|
73
71
|
- lib
|
@@ -82,8 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
80
|
- !ruby/object:Gem::Version
|
83
81
|
version: '0'
|
84
82
|
requirements: []
|
85
|
-
rubygems_version: 3.
|
86
|
-
signing_key:
|
83
|
+
rubygems_version: 3.6.2
|
87
84
|
specification_version: 4
|
88
85
|
summary: Ruby Gemini Context Caching
|
89
86
|
test_files: []
|