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.
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
.
# config/initializers/mux.rb
MuxRuby.configure do |config|
config.username = ENV["MUX_TOKEN_ID"]
config.password = ENV["MUX_TOKEN_SECRET"]
end
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
Delete an asset using the Mux::Client.destroy_asset(asset_id)
method, passing
the asset id:
Mux::Client.destroy_asset("mux_asset_id")
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
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
.
The gem is available as open source under the terms of the MIT License.