Skip to content

Instantly share code, notes, and snippets.

@dpaluy
Created January 13, 2025 02:29
Show Gist options
  • Save dpaluy/5ef9804e9f7ba1c2bc4e362ffb58e188 to your computer and use it in GitHub Desktop.
Save dpaluy/5ef9804e9f7ba1c2bc4e362ffb58e188 to your computer and use it in GitHub Desktop.
Get Logo from a domain
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