Giter Site home page Giter Site logo

link_preview's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.