FaviconGem

FaviconGem is a Ruby gem for finding and retrieving website favicons (icons). It's a port of the popular favicon Python library.

Installation

Add this line to your application's Gemfile:

gem 'favicon_gem'

And then execute:

$ bundle install

Or install it manually:

$ gem install favicon_gem

Usage

Get all icons

require 'favicon_gem'

icons = FaviconGem.get('https://www.ruby-lang.org/')
# => [Icon, Icon, Icon, ...]

# The first icon is usually the largest
icon = icons.first
puts "URL: #{icon.url}"
puts "Size: #{icon.width}x#{icon.height}"
puts "Format: #{icon.format}"

Download an icon

require 'favicon_gem'
require 'open-uri'

icons = FaviconGem.get('https://www.ruby-lang.org/')
icon = icons.first

URI.open(icon.url) do |image|
  File.open("/tmp/ruby-favicon.#{icon.format}", "wb") do |file|
    file.write(image.read)
  end
end

# => /tmp/ruby-favicon.png

Additional parameters

require 'favicon_gem'

# Custom headers
headers = {
  'User-Agent' => 'My custom User-Agent'
}

# Timeout and other parameters
FaviconGem.get('https://www.ruby-lang.org/',
                headers: headers,
                timeout: 5)

Requirements

License

This gem is available as open source under the terms of the MIT License.