Giter Club home page Giter Club logo

onebox's Introduction

โš ๏ธ This project isn't maintained anymore as the onebox library has moved into core repository. If you wish to help by maintaining the project please contact [email protected].

onebox

Gem Version Code Climate Build Status

Onebox is a library for turning media URLs into simple HTML previews of the resource.

Onebox currently has support for page, image, and video URLs for many popular sites.

It's great if you want users to input URLs and have your application convert them into rich previews for display. For example, a link to a YouTube video would be automatically converted into a video player.

It was originally created for Discourse but has since been extracted into this convenient gem for all to use!

Usage

Using onebox is fairly simple! First, make sure the library is required:

require "onebox"

Then pass a link to the library's interface:

require "onebox"

url = "http://www.amazon.com/gp/product/B005T3GRNW/ref=s9_simh_gw_p147_d0_i2"
preview = Onebox.preview(url)

This will contain a simple Onebox::Preview object that handles all the transformation. From here you either call Onebox::Preview#to_s or just pass the object to a string:

require "onebox"

url = "http://www.amazon.com/gp/product/B005T3GRNW/ref=s9_simh_gw_p147_d0_i2"
preview = Onebox.preview(url)
"#{preview}" == preview.to_s #=> true

Twitch Onebox

To be able to embed Twitch video and clips, pass hostname in the options to Onebox.preview

preview = Onebox.preview(url, hostname: 'www.example.com')

Ruby Support

The onebox library is supported on all "officially" supported versions of Ruby.

This means you must be on Ruby 2.4 or above for it to work.

Development Preview Interface

The onebox gem comes with a development server for previewing the results of your changes. You can run it by running bundle exec rake server after checking out the project. You can then try out URLs.

The server doesn't reload code changes automatically (PRs accepted!) so make sure to hit CTRL-C and restart the server to try a code change out.

Adding Support for a new URL

  1. Check if the site supports oEmbed or Open Graph. If it does, you can probably get away with just allowing the URL in Onebox::Engine::AllowlistedGenericOnebox (see: Allowlisted Generic Onebox caveats). If the site does not support open standards, you can create a new engine.

  2. Create new onebox engine

    # in lib/onebox/engine/name_onebox.rb
    
    module Onebox
      module Engine
        class NameOnebox
          include LayoutSupport
          include HTML
    
          private
    
          def data
            {
              url: @url,
              name: raw.css("h1").inner_text,
              image: raw.css("#main-image").first["src"],
              description: raw.css("#postBodyPS").inner_text
            }
          end
        end
      end
    end
  3. Create new onebox spec using FakeWeb

    # in spec/lib/onebox/engine/name_spec.rb
    require "spec_helper"
    
    describe Onebox::Engine::NameOnebox do
      let(:link) { "http://example.com" }
      let(:html) { described_class.new(link).to_html }
    
      before do
        fake(link, response("name"))
      end
    
      it "has the video's title" do
        expect(html).to include("title")
      end
    
      it "has the video's still shot" do
        expect(html).to include("photo.jpg")
      end
    
      it "has the video's description" do
        expect(html).to include("description")
      end
    
      it "has the URL to the resource" do
        expect(html).to include(link)
      end
    end
  4. Create new mustache template

    # in templates/name.mustache
    <div class="onebox">
      <a href="{{url}}">
        <h1>{{name}}</h1>
        <h2 class="host">example.com</h2>
        <img src="{{image}}" />
        <p>{{description}}</p>
      </a>
    </div>
  5. Create new fixture from HTML response for your FakeWeb request(s)

    curl --output spec/fixtures/oneboxname.response -L -X GET http://example.com
  6. Require in Engine module

    # in lib/onebox/engine.rb
    require_relative "engine/name_onebox"

Allowlisted Generic Onebox caveats

The Allowlisted Generic Onebox has some caveats for its use, beyond simply allowlisting the domain.

  1. The domain must be allowlisted
  2. The URL you're oneboxing cannot be a root url (e.g. http://example.com won't work, but http://example.com/page will)
  3. If the oneboxed URL responds with oEmbed and has a rich type: the html content must contain an <iframe>. Responses without an iframe will not be oneboxed.

Ignoring Canonical URLs

Onebox prefers to use canonical URLs instead of the raw inputted URL when searching for Open Graph metadata. If your site's canonical URL does not have opengraph metadata, use the og:ignore_canonical property to have Onebox ignore the canonical URL.

<meta property="og:ignore_canonical" content="true" />

Installing

Add this line to your application's Gemfile:

gem "onebox"

And then execute:

$ bundle

Or install it yourself as:

$ gem install onebox

Issues / Discussion

Discussion of the Onebox gem, its development and features should be done on Discourse Meta.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

onebox's People

Contributors

arpitjalan avatar cvx avatar davidtaylorhq avatar elberet avatar erickguan avatar eviltrout avatar glebm avatar gschlager avatar jbrw avatar jjaffeux avatar jzeta avatar kohenkatz avatar lidlanca avatar majakomel avatar markijbema avatar martin-brennan avatar nbianca avatar nlalonde avatar pmusaraj avatar rcanand avatar riking avatar romanrizzi avatar rriemann avatar samsaffron avatar tgxworld avatar vikhyat avatar vinothkannans avatar vykster avatar xfalcox avatar zogstrip avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

onebox's Issues

Why move matching url away from the onebox?

Though I think it was rather sloppy implemented in the previous version, I think it's a good idea to keep the code which matches the url close to the onebox conversion code. This way, you define in your onebox what urls you support, which is sort of a precondition for your code.

So I was wondering why the choice was made to move this away?

We need a way to sanitize any URLs or text we render

While our current architecture, grabing data from the response of a resource and injecting that data into a handlebars template, avoids many types of attack there's a few known problem areas we don't cover:

  1. @markijbema pointed out that we encourage XSS by grabbing image src values
  2. There may be ways to inject running JS in any parts of the resource we grab
  3. The URL we are given might contain an XSS attack

Better handle determining how to match urls to oneboxes

We want to have an easier time handling the matching of urls to oneboxes. We need to determine how to equate a onebox to a regular expression through a nice class level DSL.

In addition we want to use verbexpressions to write those regular expressions in the match setup.

Write up a How-To for new architecture

Since we're introducing a new architecture for each OneBox it'll be nice to have a quick introduction on all the parts required for a new onebox.

  1. Creating the Engine object
  2. Creating the template (inline or file)
  3. (optional) Presenters for data
  4. (optional) Sanitizing/checking setup
  5. (optional) Setting up OpenGraph

Turn URLs into Oneboxes

We need a simple maintainable way to turn urls given to Onebox into their coorsponding onebox objects. This object should know what to do if the request fails (404, 500, etc), needs to be authenticated (401), or redirected (302). It should also know what a valid URL looks like and handle that correctly.

Support for OpenGraph.

We want to handle open graph if possible as it avoids a lot of HTML parsing problems. Most websites support opengraph so we'll need a module that shares open graph behavior with Engine objects.

We should probably use intridea/opengraph.

Create yFrog onebox

matcher /^https?:\/\/(?:www\.)?yfrog\.(com|ru|com\.tr|it|fr|co\.il|co\.uk|com\.pl|pl|eu|us)\/[a-zA-Z0-9]+/

Allow the option to cache returns from websites

We want to be able to cache the return responses for certain oneboxes. This cache should pretty much be any key/value system that responds to fetch/write. Default should be an in-memory hash.

Usage of tabs and spaces

Just noticed this file while browsing around:

https://github.com/dysania/onebox/blob/master/lib/onebox/preview/amazon.rb

In ruby it's convention to use 2 spaces for indenting, but it looks like in this file (maybe others?) also tabs were used. I suspect this is due to editor configuration of one of the committers. Most editors can easily be configured to use spaces, for instance, for sublimetext config you could use this one:

https://gist.github.com/markijbema/5668813

(sorry if this comes of as pedantic, but in my experience having non-standard spacing leads to lots of merge conflicts later on, so better to fix it quickly)

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.