There are plenty of useful Ruby gems for all tastes and needs. As Ruby on Rails agency, we use most of them. And today we’ll add one more gem to the collection. How about Onebox Ruby gem for turning URLs into website previews? It’s great if you want to make your application convert URLs into rich previews for display.
Onebox is a library for turning media URLs into simple HTML previews of the resource.
It’s very simple in use. First, make sure the library is required. Then pass a link to the library’s interface. From here you either call
Onebox::Preview#to_s or just pass the object to a string. In addition, you can set your own options with the interface.
The Onebox library is supported on all supported versions of Ruby, this means you must be on Ruby 2.3 or above version.
To add support for a new URL, you need:
- Check if the site supports oEmbed or Open Graph. If it does, you can get away with just whitelisting the URL in
Onebox::Engine::WhitelistedGenericOneboxIf the site does not support open standards, you can create a new engine.
- Create a new engine
- Create new spec using FakeWeb
- Create new mustache template
- Create new fixture from HTML response for your FakeWeb request
- Require in Engine module]
That’s a short version of how it works. To know more please visit here.
And please take a look at a set of interesting Ruby gems that you can use to solve specific design problems in your code. Dry-rb is composed of over 18 Ruby gems that can work together or by themselves.