Code Monkey home page Code Monkey logo

onebox's Introduction

onebox

  • Gem Version
  • Code Climate
  • Build Status
  • Dependency Status
  • Coverage Status

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

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

Onebox has its own caching system but you can also provide (or turn off) your own system:

require "onebox"

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

In addition you can set your own options with this handy interface:

require "onebox"

Onebox.options = {
  cache: Rails.cache
}

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

Setup

  1. Create new onebox engine
``` ruby
# 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
```
  1. Create new onebox spec
``` ruby
# 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.response"))
  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
```
  1. Create new handlebars template
``` html
# in templates/name.handlebars
<div class="onebox">
  <a href="{{url}}">
    <h1>{{name}}</h1>
    <h2 class="host">example.com</h2>
    <img src="{{image}}" />
    <p>{{description}}</p>
  </a>
</div>
```
  1. Create new fixture from HTML response
``` bash
curl --output spec/fixtures/oneboxname.response -L -X -GET http://example.com
```
  1. Require in Engine module
``` ruby
# in lib/onebox/engine/engine.rb
require_relative "engine/name_onebox"
```

Onebox currently has support for page, image, and video URLs from these sites:

  • Amazon
  • Android App Store
  • Apple Store
  • BlipTV
  • Clikthrough
  • College Humor
  • Dailymotion
  • Dotsub
  • Flickr
  • Funny or Die
  • GitHub
    • Blob
    • Commit
    • Gist
    • Pull Request
  • Hulu
  • Imgur
  • Kinomap
  • NFB
  • Open Graph
  • Qik
  • Revision
  • Rotten Tomatoes
  • Slideshare
  • SmugMug
  • SoundCloud
  • Stack Exchange
  • TED
  • Twitter
  • Wikipedia
  • yFrog

Installing

Add this line to your application's Gemfile:

gem "onebox". "~> 1.0"

And then execute:

$ bundle

Or install it yourself as:

$ gem install onebox

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

eviltrout avatar jzeta avatar markijbema avatar nirvdrum avatar peterhellberg avatar randomecho avatar samsaffron avatar vikhyat avatar vykster avatar

Watchers

 avatar  avatar

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.