leoandruby 0.3.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +29 -17
- data/README.md +57 -41
- data/lib/generators/leoandruby/templates/leoandruby_initializer.rb +6 -0
- data/lib/generators/leoandruby/templates/leonardo_controller.rb +24 -0
- data/lib/generators/leoandruby/webhook_generator.rb +15 -0
- data/lib/leoandruby/version.rb +1 -1
- data/lib/leoandruby.rb +7 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b7bd276f1a286d1e2ed55f1acc4e4972bed391aeac243312f226aa6e97eb9251
|
4
|
+
data.tar.gz: e9c965c2033b6dca7d372954c3ca3bdca4a0ab405cbe6b044826627181bea8ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01110a5dccb1af703ebfa130a46b3827891e6c44772c19cfcc56298db557bf93e88bb84c3f2edd4b543b1a4dc71cbc7ac8c3adb74fc931f825c56ede1c21f48e
|
7
|
+
data.tar.gz: 0e867e7615037b11722102739ab2719c33da287f0c2581230f570a7777fc68e2d750efc909ce3d36e337fe7afe9455115cf77dcaada1fffe46058ed80a26c1ee
|
data/CHANGELOG.md
CHANGED
@@ -1,29 +1,41 @@
|
|
1
|
-
|
1
|
+
# Changelog
|
2
2
|
|
3
|
-
## [0.1
|
3
|
+
## [0.4.1] - 2024-11-26
|
4
4
|
|
5
|
-
-
|
5
|
+
- Improved: Compatibility with Ruby 3.3.0.
|
6
6
|
|
7
|
-
## [0.1.1] - 2024-11-21
|
8
7
|
|
9
|
-
-
|
8
|
+
## [0.4.0] - 2024-11-26
|
9
|
+
### Added
|
10
|
+
- **Webhook Support**: Integrated Leonardo.ai's webhook feature to handle asynchronous image generation results. Applications can now automatically process results via a webhook callback.
|
11
|
+
- **Rails Generator**: Added a generator to set up the webhook integration in Rails applications. This includes:
|
12
|
+
- A pre-configured controller (`leonardo_controller.rb`).
|
13
|
+
- A route for the webhook (`/leonardo_webhook`).
|
14
|
+
- An initializer for managing webhook token configuration.
|
15
|
+
- **Security Enhancements**: Introduced token-based verification for webhook requests, ensuring only authorized requests are processed.
|
10
16
|
|
11
|
-
|
17
|
+
### Improved
|
18
|
+
- Enhanced documentation and examples in the README to reflect the new webhook functionality and Rails generator.
|
12
19
|
|
13
|
-
|
20
|
+
---
|
14
21
|
|
15
|
-
## [0.
|
22
|
+
## [0.3.0] - 2024-11-15
|
23
|
+
- Added: Ability to list available models from the Leonardo.ai API.
|
24
|
+
- Improved: General API interaction consistency and better error handling.
|
16
25
|
|
17
|
-
|
26
|
+
---
|
18
27
|
|
19
|
-
## [0.2.
|
28
|
+
## [0.2.0] - 2024-11-10
|
29
|
+
- Added: Support for specifying the number of images in a single generation request.
|
30
|
+
- Improved: Compatibility with Ruby 3.3.0.
|
20
31
|
|
21
|
-
|
32
|
+
---
|
22
33
|
|
23
|
-
## [0.
|
34
|
+
## [0.1.0] - 2024-11-01
|
35
|
+
- Initial release of the LeoAndRuby gem.
|
36
|
+
- Features:
|
37
|
+
- Image generation using Leonardo.ai's API.
|
38
|
+
- Retrieve generation results by ID.
|
39
|
+
- Basic configuration using environment variables.
|
24
40
|
|
25
|
-
|
26
|
-
|
27
|
-
## [0.3.0] - 2024-11-25
|
28
|
-
|
29
|
-
- Added: New feature allowing user to display a list of all the available models.
|
41
|
+
---
|
data/README.md
CHANGED
@@ -1,17 +1,18 @@
|
|
1
|
-
|
2
1
|
# LeoAndRuby
|
3
2
|
|
4
|
-
**LeoAndRuby** is a Ruby gem for generating images using the [Leonardo.ai API](https://docs.leonardo.ai/docs/generate-your-first-images). With this gem, you can easily integrate Leonardo.ai's powerful image generation capabilities into your Ruby applications.
|
3
|
+
**LeoAndRuby** is a Ruby gem for generating images using the [Leonardo.ai API](https://docs.leonardo.ai/docs/generate-your-first-images). With this gem, you can easily integrate Leonardo.ai's powerful image generation capabilities into your Ruby and Rails applications.
|
5
4
|
|
6
5
|
[![Gem Version](https://badge.fury.io/rb/leoandruby.svg)](https://badge.fury.io/rb/leoandruby)
|
7
6
|
|
8
|
-
|
9
7
|
---
|
10
8
|
|
11
9
|
## Features
|
12
10
|
|
13
11
|
- Generate images using Leonardo.ai's models.
|
14
12
|
- Retrieve the status and result of a generated image.
|
13
|
+
- List available models on the Leonardo.ai platform.
|
14
|
+
- Webhook support to handle asynchronous image generation results.
|
15
|
+
- Rails generator for setting up webhook integration effortlessly.
|
15
16
|
- Simple and intuitive Ruby interface for interacting with the Leonardo.ai API.
|
16
17
|
|
17
18
|
---
|
@@ -42,6 +43,8 @@ gem install leoandruby
|
|
42
43
|
|
43
44
|
To use LeoAndRuby, you need an API key from Leonardo.ai. You can obtain it by signing up for an account and navigating to the API key section in your dashboard.
|
44
45
|
|
46
|
+
For Rails users, you can generate a webhook controller and route using the built-in generator.
|
47
|
+
|
45
48
|
---
|
46
49
|
|
47
50
|
## Usage
|
@@ -82,8 +85,6 @@ models_response = client.list_models
|
|
82
85
|
puts models_response
|
83
86
|
```
|
84
87
|
|
85
|
-
This will return a list of models available in the Leonardo.ai platform.
|
86
|
-
|
87
88
|
### 4. Retrieve the Generated Image
|
88
89
|
|
89
90
|
Wait a few seconds for the image to be generated, then retrieve it using the generation ID:
|
@@ -97,54 +98,42 @@ puts image_response
|
|
97
98
|
|
98
99
|
---
|
99
100
|
|
100
|
-
##
|
101
|
+
## Webhook Integration (Rails)
|
101
102
|
|
102
|
-
|
103
|
+
LeoAndRuby supports asynchronous image generation through Leonardo.ai’s webhook feature. This allows your application to automatically process results when the image generation is complete.
|
103
104
|
|
104
|
-
|
105
|
+
### Generate a Webhook Controller
|
106
|
+
|
107
|
+
Run the following command to generate a controller, route, and initializer for webhook integration:
|
105
108
|
|
106
109
|
```bash
|
107
|
-
|
110
|
+
rails generate leoandruby:webhook
|
108
111
|
```
|
109
112
|
|
110
|
-
|
113
|
+
This will:
|
114
|
+
- Create `app/controllers/leonardo_controller.rb`.
|
115
|
+
- Add a route to `config/routes.rb`:
|
116
|
+
```ruby
|
117
|
+
post '/leonardo_webhook', to: 'leonardo#webhook'
|
118
|
+
```
|
119
|
+
- Add a configuration file to `config/initializers/leoandruby.rb` for managing the webhook token:
|
120
|
+
```ruby
|
121
|
+
LeoAndRuby.config = {
|
122
|
+
webhook_token: ENV.fetch('LEONARDO_WEBHOOK_TOKEN', 'your_default_token_here')
|
123
|
+
}
|
124
|
+
```
|
111
125
|
|
112
|
-
|
113
|
-
api_key = ENV['LEOANDRUBY_API_KEY']
|
114
|
-
client = LeoAndRuby::Client.new(api_key)
|
115
|
-
```
|
126
|
+
### Webhook Security
|
116
127
|
|
117
|
-
|
128
|
+
The webhook controller verifies requests using an API token provided in the `Authorization` header. Configure your webhook token in an environment variable or directly in the initializer.
|
118
129
|
|
119
|
-
|
120
|
-
|
121
|
-
The `list_models` method will return a response in the following format:
|
122
|
-
|
123
|
-
```json
|
124
|
-
{
|
125
|
-
"custom_models": [
|
126
|
-
{
|
127
|
-
"id": "model_id_1",
|
128
|
-
"name": "Model Name 1",
|
129
|
-
"description": "Description of Model 1",
|
130
|
-
"nsfw": false,
|
131
|
-
},
|
132
|
-
{
|
133
|
-
"id": "model_id_2",
|
134
|
-
"name": "Model Name 2",
|
135
|
-
"description": "Description of Model 2",
|
136
|
-
"nsfw": false,
|
137
|
-
}
|
138
|
-
// More models...
|
139
|
-
]
|
140
|
-
}
|
141
|
-
```
|
130
|
+
Webhook requests without a valid token will be rejected with a `401 Unauthorized` status.
|
142
131
|
|
143
|
-
|
132
|
+
---
|
144
133
|
|
145
134
|
## Example Script
|
146
135
|
|
147
|
-
Here's a full example script:
|
136
|
+
Here's a full example script for generating an image and retrieving it:
|
148
137
|
|
149
138
|
```ruby
|
150
139
|
require 'leoandruby'
|
@@ -173,6 +162,33 @@ puts image_response
|
|
173
162
|
|
174
163
|
---
|
175
164
|
|
165
|
+
## Configuration
|
166
|
+
|
167
|
+
### API Key
|
168
|
+
|
169
|
+
You can store your API key in an environment variable for security:
|
170
|
+
|
171
|
+
```bash
|
172
|
+
export LEOANDRUBY_API_KEY=your_api_key
|
173
|
+
```
|
174
|
+
|
175
|
+
Then, retrieve it in your code:
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
api_key = ENV['LEOANDRUBY_API_KEY']
|
179
|
+
client = LeoAndRuby::Client.new(api_key)
|
180
|
+
```
|
181
|
+
|
182
|
+
### Webhook Token
|
183
|
+
|
184
|
+
To secure webhook requests, configure the `LEONARDO_WEBHOOK_TOKEN` environment variable:
|
185
|
+
|
186
|
+
```bash
|
187
|
+
export LEONARDO_WEBHOOK_TOKEN=your_webhook_token
|
188
|
+
```
|
189
|
+
|
190
|
+
---
|
191
|
+
|
176
192
|
## Supported Ruby Versions
|
177
193
|
|
178
194
|
LeoAndRuby is tested with the latest Ruby versions. Ensure your environment is up to date to avoid compatibility issues.
|
@@ -201,5 +217,5 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
201
217
|
## Acknowledgments
|
202
218
|
|
203
219
|
Special thanks to [Leonardo.ai](https://leonardo.ai/) for providing such an amazing image generation API.
|
204
|
-
```
|
205
220
|
|
221
|
+
---
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class LeonardoController < ApplicationController
|
2
|
+
skip_before_action :verify_authenticity_token
|
3
|
+
|
4
|
+
before_action :verify_webhook_token
|
5
|
+
|
6
|
+
def webhook
|
7
|
+
payload = JSON.parse(request.body.read)
|
8
|
+
# TODO: Process the incoming webhook payload
|
9
|
+
Rails.logger.info("Received webhook: #{payload}")
|
10
|
+
render json: {status: "success"}, status: :ok
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def verify_webhook_token
|
16
|
+
provided_token = request.headers["Authorization"]&.split(" ")&.last
|
17
|
+
expected_token = LeoAndRuby.config[:webhook_token]
|
18
|
+
|
19
|
+
unless ActiveSupport::SecurityUtils.secure_compare(provided_token.to_s, expected_token.to_s)
|
20
|
+
Rails.logger.warn("Unauthorized webhook attempt: #{provided_token}")
|
21
|
+
render json: {error: "Unauthorized"}, status: :unauthorized
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "rails/generators"
|
2
|
+
|
3
|
+
module LeoAndRuby
|
4
|
+
class WebhookGenerator < Rails::Generators::Base
|
5
|
+
source_root File.expand_path("templates", __dir__)
|
6
|
+
|
7
|
+
def create_controller_file
|
8
|
+
copy_file "leonardo_controller.rb", "app/controllers/leonardo_controller.rb"
|
9
|
+
end
|
10
|
+
|
11
|
+
def add_route
|
12
|
+
route "post '/leonardo_webhook', to: 'leonardo#webhook'"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/leoandruby/version.rb
CHANGED
data/lib/leoandruby.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: leoandruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard HW Baldwin
|
@@ -26,6 +26,9 @@ files:
|
|
26
26
|
- README.md
|
27
27
|
- Rakefile
|
28
28
|
- cloving.json
|
29
|
+
- lib/generators/leoandruby/templates/leoandruby_initializer.rb
|
30
|
+
- lib/generators/leoandruby/templates/leonardo_controller.rb
|
31
|
+
- lib/generators/leoandruby/webhook_generator.rb
|
29
32
|
- lib/leoandruby.rb
|
30
33
|
- lib/leoandruby/client.rb
|
31
34
|
- lib/leoandruby/version.rb
|