Giter VIP home page Giter VIP logo

mux-rails's Introduction

Mux::Rails

Gem Version Ruby

A Rails Engine for working with Mux.

Use Mux::Client to create assets and Mux::Notifications (built on the ActiveSupport::Notifications API) to handle incoming webhook requests.

Installation

Run

bundle add mux-rails

or add this line to your application's Gemfile:

gem 'mux-rails'

and run bundle install.

Then mount the engine in your routes:

# config/routes.rb
mount Mux::Engine, at: "/mux" # provide a custom path

Remember to configure the webhooks at mux.com to point to https://yourwebsite.com/mux/events.

Usage

Setup Mux tokens

# config/initializers/mux.rb
MuxRuby.configure do |config|
  config.username = ENV["MUX_TOKEN_ID"]
  config.password = ENV["MUX_TOKEN_SECRET"]
end

Creating assets

Create a Mux asset by using the Mux::Client.create_asset(url) method, passing the url to the video. The method returns a Mux asset id.

mux_asset_id = Mux::Client.create_asset("http://foo.com/bar.mp4")
# You probably want to store mux_asset_id somewhere for future reference

Deleting assets

Delete an asset using the Mux::Client.destroy_asset(asset_id) method, passing the asset id:

Mux::Client.destroy_asset("mux_asset_id")

Fetching asset

Fetch an asset using the Mux::Client.get_asset(asset_id) method, passing the asset id. This returns a JSON object:

asset = Mux::Client.get_asset("mux_asset_id")
puts asset.data.status

Handling webhook requests

Using a subscriber with a block we can listen to incoming events from Mux and do further work:

# config/initializers/mux.rb
# ...

Mux::Notifications.subscribe "video.asset.created" do |event|
  # handle asset created
  # event.id == mux_asset_id
end

Mux::Notifications.subscribe "video.asset.ready" do |event|
  # handle asset ready
  # event.id == mux_asset_id
end

Mux::Notifications.subscribe "video.asset.deleted" do |event|
  # handle asset deleted
  # event.id == mux_asset_id
end

The block is passed an event which is simply the incoming JSON (take a peek in fixtures for examples) wrapped in Mux::Event class based on Dry::Struct.

License

The gem is available as open source under the terms of the MIT License.

mux-rails's People

Contributors

asgerb avatar tomasc avatar

Watchers

James Cloos 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.