0.01
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Generate a link_preview for any URL
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0
>= 2.9
>= 0

Runtime

 Project Readme

Build Status Code Climate

link_preview

Generate an oEmbed response for any URL.

Usage

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)

Features

  • 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

Installation

gem install link_preview

Configuration

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

Contributing

  • Fork the project
  • Fix the issue
  • Add unit tests
  • Submit pull request on github

See CONTRIBUTORS.txt for list of project contributors

Copyright

Copyright (c) 2014-2016, VMware, Inc. All Rights Reserved. See LICENSE.txt for further details.