Generate an oEmbed response for any URL.
content = LinkPreview.fetch(url)
content.as_oembed
Serialize content sources:
content.sources
Load previous content via sources:
previous_content = LinkPreview.load_content(url, options, content.sources)
- Designed to make the minimal number of HTTP requests to generate a preview
- Configurable via Faraday middleware
- Battletested on wide variety of URLs and HTML in the wild
- Includes test helper for stubbing
LinkPreview::Content
gem install link_preview
LinkPreview is configured via Faraday
with some additional middleware:
# $RAILS_ROOT/config/initializer/link_preview.rb
# Cache responses in Rails.cache
class HTTPCache < Faraday::Middleware
CACHE_PREFIX = name
EXPIRES_IN = 10.minutes
def call(env)
url = env[:url].to_s
Rails.cache.fetch("#{CACHE_PREFIX}::#{url}", :expires_in => EXPIRES_IN) do
@app.call(env)
end
end
end
# Report unknown exceptions to Airbrake
module ErrorHandler
IGNORED_EXCEPTIONS = [
IOError,
SocketError,
Timeout::Error,
Errno::ECONNREFUSED,
Errno::ECONNRESET,
Errno::EHOSTUNREACH,
Errno::ENETUNREACH,
Errno::ETIMEDOUT,
Net::ProtocolError,
Net::NetworkTimeoutError,
OpenSSL::SSL::SSLError
]
class << self
def error_handler(e)
case e
when *IGNORED_EXCEPTIONS
# Ignore
else
Airbrake.notify_or_ignore(e)
end
end
end
end
LinkPreview.configure do |config|
config.http_adapter = Faraday::Adapter::NetHttp
config.max_requests = 10
config.follow_redirects = true
config.middleware = HTTPCache
config.error_handler = ErrorHandler.method(:error_handler)
end
- Fork the project
- Fix the issue
- Add unit tests
- Submit pull request on github
See CONTRIBUTORS.txt for list of project contributors
Copyright (c) 2014-2016, VMware, Inc. All Rights Reserved. See LICENSE.txt for further details.