这是一个 Ruby gem,可从指定的 URL 获取图像和元数据。它与流行的社交网站的链接预览功能非常相似。
Ruby gem generating image thumbnails from a given URL. Rank them and give you back an object containing images and website informations. Works like Facebook link previewer.
Demo Application is here ! The source code of the Demo Application is hosted here!
Add this line to your application's Gemfile:
gem 'link_thumbnailer'And then execute:
$ bundleOr install it yourself as:
$ gem install link_thumbnailerIf you are using Rails, you can generate the configuration file with:
$ rails g link_thumbnailer:installThis will add link_thumbnailer.rb to config/initializers/.
Run irb and require the gem:
require 'link_thumbnailer'The gem handle regular website but also website that use the Opengraph protocol.
object = LinkThumbnailer.generate('http://stackoverflow.com')
=> #
object.title
=> "Stack Overflow"
object.favicon
=> "//cdn.sstatic.net/stackoverflow/img/favicon.ico?v=038622610830"
object.description
=> "Q&A for professional and enthusiast programmers"
object.images.first.src.to_s
=> "http://cdn.sstatic.net/stackoverflow/img/[email protected]?v=fde65a5a78c6"LinkThumbnailer generate method return an instance of LinkThumbnailer::Models::Website that respond to to_json and as_json as you would expect:
object.to_json
=> "{\"url\":\"http://stackoverflow.com\",\"title\":\"Stack Overflow\",\"description\":\"Q&A for professional and enthusiast programmers\",\"images\":[{\"src\":\"http://cdn.sstatic.net/stackoverflow/img/[email protected]?v=fde65a5a78c6\",\"size\":[316,316],\"type\":\"png\"}]}"LinkThumbnailer comes with default configuration values. You can change default value by overriding them in a rails initializer:
In config/initializers/link_thumbnailer.rb
…Or at runtime:
object = LinkThumbnailer.generate('http://stackoverflow.com', redirect_limit: 5, user_agent: 'foo')Note that runtime options will override default global configuration.
See Configuration Options Explained for more details on each configuration options.
LinkThumbnailer defines a list of custom exceptions you may want to rescue in your code. All the following exceptions inherit from LinkThumbnailer::Exceptions:
RedirectLimit -- raised when redirection threshold defined in config is reachedBadUriFormat -- raised when url given is not a valid HTTP urlFormatNotSupported -- raised when the Content-Type of the HTTP request is not supported (not html)You can rescue from any LinkThumbnailer exceptions using the following code:
begin
LinkThumbnailer.generate('http://foo.com')
rescue LinkThumbnailer::Exceptions => e
# do something
endgit checkout -b my-new-feature)bundle exec rspec spec)git commit -am 'Added some feature')git push origin my-new-feature)暂无开放 Issues,或尚未同步最近议题。