rails_ai_tag 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +8 -0
- data/Gemfile +14 -0
- data/README.md +60 -0
- data/Rakefile +12 -0
- data/bin/console +11 -0
- data/bin/setup +8 -0
- data/doc/image_ai_cat.png +0 -0
- data/doc/image_ai_tag.png +0 -0
- data/doc/image_ai_tag_2.png +0 -0
- data/doc/image_ai_tag_3.png +0 -0
- data/lib/rails_ai_tag/configuration.rb +11 -0
- data/lib/rails_ai_tag/helpers/ai_image_helper.rb +40 -0
- data/lib/rails_ai_tag/version.rb +5 -0
- data/lib/rails_ai_tag.rb +24 -0
- data/sig/rails_ai_tag.rbs +4 -0
- data/spec/helpers/ai_image_helper_spec.rb +77 -0
- data/spec/rails_ai_tag_spec.rb +11 -0
- data/spec/spec_helper.rb +19 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: '081c2a789ce7fc4edc77b8fe9ff24445953f021588106460191094e23dcc66a5'
|
4
|
+
data.tar.gz: 945f655bc60334fa115b54aa2b577eedc59601117b1e813d03a48b019b885860
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a6d04bcc98305761e95064e3b69e52799a11c8f70e5d31141c8effe577510992859fc3877c8a2674d66ecd4a9f83d54f119664d6c5816a6b33ead504a23b52bc
|
7
|
+
data.tar.gz: 9780e180b6df6801348c825b5fbe4aebec240d92117c15c79eb67c404525a853c2dd1806ccafb8df712383a998d12f22b5faa5b55239c003b3fdba303f66cf34
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# RailsAiTag
|
2
|
+
|
3
|
+
```erb
|
4
|
+
<%= ai_image_tag("A cute cat with a barretina that likes ruby on rails") %>
|
5
|
+
```
|
6
|
+

|
7
|
+
|
8
|
+
RailsAiTag is a Ruby on Rails gem that provides custom HTML helpers for AI-powered image generation. Currently, it supports generating `<img>` tags with images created by OpenAI's image generation API, based on a given description. This gem is designed to be extendable, with plans for supporting other AI services.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your Rails application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'rails_ai_tag'
|
16
|
+
```
|
17
|
+
Then, run:
|
18
|
+
|
19
|
+
```bash
|
20
|
+
bundle install
|
21
|
+
```
|
22
|
+
|
23
|
+
## Configuration
|
24
|
+
|
25
|
+
Configure the gem by specifying your AI service provider and API key. Currently, only OpenAI is supported as the provider.
|
26
|
+
|
27
|
+
Add the configuration in an initializer (e.g., config/initializers/rails_ai_tag.rb):
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
RailsAiTag.configure do |config|
|
31
|
+
config.provider = :openai # Currently, only :openai is supported
|
32
|
+
config.api_key = "YOUR_OPENAI_API_KEY" # Replace with your actual OpenAI API key
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
Use the ai_image_tag helper in your Rails views.
|
38
|
+
|
39
|
+
Provide a description, and the helper will generate an image based on the description using OpenAI's API.
|
40
|
+
|
41
|
+
```erb
|
42
|
+
<%= ai_image_tag("A futuristic cityscape at sunset") %>
|
43
|
+
```
|
44
|
+
|
45
|
+
This helper method generates an <img> tag with the AI-generated image URL and sets the alt attribute to the description.
|
46
|
+

|
47
|
+

|
48
|
+
## Example
|
49
|
+
```erb
|
50
|
+
<%= ai_image_tag("A lush green forest with misty mountains in the background") %>
|
51
|
+
```
|
52
|
+
|
53
|
+
The above code will generate an image tag for an AI-generated image that matches the description provided.
|
54
|
+

|
55
|
+
## Testing
|
56
|
+
RailsAiTag uses RSpec and WebMock for testing. You can run the tests with:
|
57
|
+
|
58
|
+
```bash
|
59
|
+
rspec
|
60
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "rails_ai_tag"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
require "irb"
|
11
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
Binary file
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# lib/rails_ai_tag/configuration.rb
|
2
|
+
module RailsAiTag
|
3
|
+
class Configuration
|
4
|
+
attr_accessor :provider, :api_key
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@provider = :openai # Default to OpenAI
|
8
|
+
@api_key = ENV["OPENAI_API_KEY"] # Can be set via environment variable or manually
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# lib/rails_ai_tag/helpers/ai_image_helper.rb
|
2
|
+
require "httparty"
|
3
|
+
|
4
|
+
module RailsAiTag
|
5
|
+
module Helpers
|
6
|
+
module AiImageHelper
|
7
|
+
def ai_image_tag(description, **options)
|
8
|
+
image_url = generate_image(description)
|
9
|
+
image_tag(image_url, alt: description, **options)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def generate_image(description)
|
15
|
+
case RailsAiTag.configuration.provider
|
16
|
+
when :openai
|
17
|
+
generate_image_openai(description)
|
18
|
+
else
|
19
|
+
raise NotImplementedError, "Provider #{RailsAiTag.configuration.provider} is not supported"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def generate_image_openai(description)
|
24
|
+
response = HTTParty.post("https://api.openai.com/v1/images/generations", {
|
25
|
+
body: { prompt: description, n: 1, size: "512x512" }.to_json,
|
26
|
+
headers: {
|
27
|
+
'Content-Type' => 'application/json',
|
28
|
+
'Authorization' => "Bearer #{RailsAiTag.configuration.api_key}"
|
29
|
+
}
|
30
|
+
})
|
31
|
+
|
32
|
+
if response.code == 200
|
33
|
+
response.parsed_response["data"].first["url"]
|
34
|
+
else
|
35
|
+
"/assets/default_image.png" # Fallback image
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/rails_ai_tag.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# lib/rails_ai_tag.rb
|
4
|
+
require "rails_ai_tag/version"
|
5
|
+
require "rails_ai_tag/configuration"
|
6
|
+
require "rails_ai_tag/helpers/ai_image_helper"
|
7
|
+
|
8
|
+
module RailsAiTag
|
9
|
+
class Railtie < ::Rails::Railtie
|
10
|
+
initializer "rails_ai_tag.helpers" do
|
11
|
+
ActiveSupport.on_load(:action_view) do
|
12
|
+
include RailsAiTag::Helpers::AiImageHelper
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class << self
|
18
|
+
attr_accessor :configuration
|
19
|
+
def configure
|
20
|
+
self.configuration ||= Configuration.new
|
21
|
+
yield(configuration)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# spec/helpers/ai_image_helper_spec.rb
|
2
|
+
require "rails_helper"
|
3
|
+
require "rails_ai_tag/helpers/ai_image_helper"
|
4
|
+
|
5
|
+
RSpec.describe RailsAiTag::Helpers::AiImageHelper do
|
6
|
+
include RailsAiTag::Helpers::AiImageHelper
|
7
|
+
|
8
|
+
before do
|
9
|
+
RailsAiTag.configure do |config|
|
10
|
+
config.provider = :openai
|
11
|
+
config.api_key = "test_api_key"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#ai_image_tag" do
|
16
|
+
let(:description) { "A futuristic cityscape at sunset" }
|
17
|
+
|
18
|
+
context "when OpenAI is the provider" do
|
19
|
+
before do
|
20
|
+
RailsAiTag.configuration.provider = :openai
|
21
|
+
end
|
22
|
+
|
23
|
+
it "returns an image tag with the generated image URL" do
|
24
|
+
allow(self).to receive(:generate_image_openai).and_return("https://example.com/generated_image.png")
|
25
|
+
|
26
|
+
result = ai_image_tag(description)
|
27
|
+
expect(result).to include("https://example.com/generated_image.png")
|
28
|
+
expect(result).to include("alt=\"A futuristic cityscape at sunset\"")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# spec/helpers/ai_image_helper_spec.rb
|
35
|
+
require "rails_helper"
|
36
|
+
require "rails_ai_tag/helpers/ai_image_helper"
|
37
|
+
|
38
|
+
RSpec.describe RailsAiTag::Helpers::AiImageHelper do
|
39
|
+
include RailsAiTag::Helpers::AiImageHelper
|
40
|
+
|
41
|
+
before do
|
42
|
+
RailsAiTag.configure do |config|
|
43
|
+
config.provider = :openai
|
44
|
+
config.api_key = "test_api_key" # Mocked API key
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "#ai_image_tag" do
|
49
|
+
let(:description) { "A futuristic cityscape at sunset" }
|
50
|
+
|
51
|
+
context "when OpenAI is the provider" do
|
52
|
+
before do
|
53
|
+
RailsAiTag.configuration.provider = :openai
|
54
|
+
|
55
|
+
# Mock the OpenAI API response
|
56
|
+
stub_request(:post, "https://api.openai.com/v1/images/generations")
|
57
|
+
.with(
|
58
|
+
body: { prompt: description, n: 1, size: "512x512" }.to_json,
|
59
|
+
headers: {
|
60
|
+
'Content-Type' => 'application/json',
|
61
|
+
'Authorization' => "Bearer test_api_key"
|
62
|
+
}
|
63
|
+
).to_return(
|
64
|
+
status: 200,
|
65
|
+
body: { "data" => [{ "url" => "https://example.com/generated_image.png" }] }.to_json,
|
66
|
+
headers: { 'Content-Type' => 'application/json' }
|
67
|
+
)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "returns an image tag with the generated image URL" do
|
71
|
+
result = ai_image_tag(description)
|
72
|
+
expect(result).to include("https://example.com/generated_image.png")
|
73
|
+
expect(result).to include("alt=\"A futuristic cityscape at sunset\"")
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails_ai_tag"
|
4
|
+
require "webmock/rspec"
|
5
|
+
|
6
|
+
# Disable external requests in tests
|
7
|
+
WebMock.disable_net_connect!(allow_localhost: true)
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
# Enable flags like --only-failures and --next-failure
|
11
|
+
config.example_status_persistence_file_path = ".rspec_status"
|
12
|
+
|
13
|
+
# Disable RSpec exposing methods globally on `Module` and `main`
|
14
|
+
config.disable_monkey_patching!
|
15
|
+
|
16
|
+
config.expect_with :rspec do |c|
|
17
|
+
c.syntax = :expect
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_ai_tag
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jordi Bari Corberó
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-11-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: webmock
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: A gem that extends Rails helpers with AI-powered image generation. Current
|
56
|
+
functionality includes a custom image_tag helper that generates images based on
|
57
|
+
a description.
|
58
|
+
email:
|
59
|
+
- jordi.bari@gmail.com
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".rspec"
|
65
|
+
- ".rubocop.yml"
|
66
|
+
- Gemfile
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- doc/image_ai_cat.png
|
72
|
+
- doc/image_ai_tag.png
|
73
|
+
- doc/image_ai_tag_2.png
|
74
|
+
- doc/image_ai_tag_3.png
|
75
|
+
- lib/rails_ai_tag.rb
|
76
|
+
- lib/rails_ai_tag/configuration.rb
|
77
|
+
- lib/rails_ai_tag/helpers/ai_image_helper.rb
|
78
|
+
- lib/rails_ai_tag/version.rb
|
79
|
+
- sig/rails_ai_tag.rbs
|
80
|
+
- spec/helpers/ai_image_helper_spec.rb
|
81
|
+
- spec/rails_ai_tag_spec.rb
|
82
|
+
- spec/spec_helper.rb
|
83
|
+
homepage: https://github.com/rails-cat/rails_ai_tag
|
84
|
+
licenses: []
|
85
|
+
metadata:
|
86
|
+
allowed_push_host: https://rubygems.org
|
87
|
+
homepage_uri: https://github.com/rails-cat/rails_ai_tag
|
88
|
+
source_code_uri: https://github.com/rails-cat/rails_ai_tag
|
89
|
+
changelog_uri: https://github.com/rails-cat/rails_ai_tag
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: 3.0.0
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubygems_version: 3.5.9
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Custom Rails helper for AI-generated image tags
|
109
|
+
test_files: []
|