-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #246 from loftwah/dl/og-image-fix
add og image fix
- Loading branch information
Showing
7 changed files
with
155 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,20 @@ | ||
# app/helpers/open_graph_helper.rb | ||
module OpenGraphHelper | ||
include Rails.application.routes.url_helpers | ||
|
||
def set_open_graph_tags(user) | ||
# Fallback values for Open Graph | ||
default_title = 'Linkarooie - Simplify Your Online Presence' | ||
default_description = 'Manage all your links in one place with Linkarooie. Create a central hub for your social and professional profiles.' | ||
default_description = 'Manage all your links in one place with Linkarooie.' | ||
default_image = image_url('default_og_image.png') | ||
default_image_alt = 'Linkarooie logo' | ||
default_url = root_url | ||
twitter_handle = user.username&.downcase || '@loftwah' | ||
|
||
# Open Graph tags with fallback values | ||
content_for :og_title, user.full_name || default_title | ||
content_for :og_description, (user.description || default_description).truncate(160) | ||
content_for :og_image, user.username.present? ? url_for("/uploads/og_images/#{user.username}_og.png") : default_image | ||
content_for :og_image_alt, user.full_name.present? ? "#{user.full_name}'s profile image" : default_image_alt | ||
content_for :og_url, user_links_url(user.username || default_url) | ||
|
||
# Twitter Card tags with fallback values | ||
|
||
content_for :og_title, user.full_name.presence || default_title | ||
content_for :og_description, user.description.presence&.truncate(160) || default_description | ||
content_for :og_image, user.og_image_url.presence || default_image | ||
content_for :og_url, user_links_url(username: user.username) | ||
|
||
# Twitter Card specific tags | ||
content_for :twitter_card, 'summary_large_image' | ||
content_for :twitter_site, "@#{twitter_handle}" | ||
content_for :twitter_creator, "@#{twitter_handle}" | ||
content_for :twitter_site, "@#{user.username.downcase}" | ||
content_for :twitter_creator, "@#{user.username.downcase}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddOgImageUrlToUsers < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :users, :og_image_url, :string | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe OpenGraphImageGenerator do | ||
let(:user) { create(:user, username: 'testuser', full_name: 'Test User', tags: ['ruby', 'rails'].to_json) } | ||
let(:spaces_service) { instance_double(DigitalOceanSpacesService) } | ||
let(:spaces_url) { 'https://linkarooie.syd1.digitaloceanspaces.com/og_images/testuser_og.png' } | ||
|
||
before do | ||
allow(DigitalOceanSpacesService).to receive(:new).and_return(spaces_service) | ||
allow(spaces_service).to receive(:upload_file_from_path) | ||
.with("og_images/#{user.username}_og.png", anything) | ||
.and_return(spaces_url) | ||
end | ||
|
||
describe '#generate' do | ||
it 'generates and uploads the image to Spaces' do | ||
result = described_class.new(user).generate | ||
expect(result).to eq(spaces_url) | ||
expect(user.reload.og_image_url).to eq(spaces_url) | ||
end | ||
|
||
context 'with different avatar sources' do | ||
it 'handles Spaces avatars' do | ||
user.update(avatar: 'https://linkarooie.syd1.digitaloceanspaces.com/avatars/test.png') | ||
expect(spaces_service).to receive(:upload_file_from_path) | ||
.with("og_images/#{user.username}_og.png", anything) | ||
.and_return(spaces_url) | ||
|
||
result = described_class.new(user).generate | ||
expect(result).to eq(spaces_url) | ||
end | ||
|
||
it 'uses default avatar for invalid URLs' do | ||
user.update(avatar: 'invalid-url') | ||
expect(spaces_service).to receive(:upload_file_from_path) | ||
.with("og_images/#{user.username}_og.png", anything) | ||
.and_return(spaces_url) | ||
|
||
result = described_class.new(user).generate | ||
expect(result).to eq(spaces_url) | ||
end | ||
end | ||
|
||
context 'when errors occur' do | ||
it 'handles upload failures' do | ||
allow(spaces_service).to receive(:upload_file_from_path).and_return(nil) | ||
result = described_class.new(user).generate | ||
expect(result).to be_nil | ||
expect(user.reload.og_image_url).to be_nil | ||
end | ||
|
||
it 'logs errors and returns nil' do | ||
allow(spaces_service).to receive(:upload_file_from_path) | ||
.and_raise(StandardError.new("Upload failed")) | ||
|
||
expect(Rails.logger).to receive(:error) | ||
.with("Failed to generate OG image for user #{user.id}: Upload failed") | ||
|
||
result = described_class.new(user).generate | ||
expect(result).to be_nil | ||
end | ||
end | ||
end | ||
end |