Created
January 13, 2025 02:29
-
-
Save dpaluy/5ef9804e9f7ba1c2bc4e362ffb58e188 to your computer and use it in GitHub Desktop.
Get Logo from a domain
This file contains hidden or 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
class LogoService < BaseService | |
EXTERNAL_URL = 'https://logo.clearbit.com'.freeze | |
attr_reader :domain, :image_object | |
def initialize(domain, image_object) | |
@domain = domain | |
@image_object = image_object | |
end | |
def call | |
url = [EXTERNAL_URL, domain].join('/') | |
file = download_remote_file(url) | |
filename = [domain.split('.').first, 'png'].join('.') | |
image_object.attach(io: file, filename:, content_type: 'image/png') | |
end | |
private | |
def download_remote_file(url) | |
response = Net::HTTP.get_response(URI.parse(url)) | |
StringIO.new(response.body) | |
end | |
end | |
__END__ | |
domain = "google.com" | |
# resource has logo as attachment | |
LogoService.call(domain, resource.logo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment