mux-rails 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9daf9dfa3ace722030cdfa1020cb3682771167ef24cbb91394fa59b6007a3243
4
+ data.tar.gz: d2f33afce8d1b13649bbee69e38912dc59cbbd82ccae9f7d926178e83b62180f
5
+ SHA512:
6
+ metadata.gz: cd03523ecd957b317f4f7cc5cd2ce8cf2e07d3068124347b283f2ed1432cd3aa2b026cfec68621d5c1569431aaf52c6c798c50e0361e6119ecccead2067c3da9
7
+ data.tar.gz: 6a725b3b197c054f75abc3e7114b243d6058b6d1b395925099d00e7e372b537c3eb9145ff9b7432503812266b28d49707258a989aac87147ba6d5f05c0096ebd
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019 asgerb
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # Mux::Rails
2
+
3
+ ![Ruby](https://github.com/asgerb/mux-rails/workflows/Ruby/badge.svg)
4
+
5
+ A Rails Engine for working with [Mux](https://mux.com/).
6
+
7
+ Use `Mux::Client` to create assets and `Mux::Notifications` (built on the
8
+ `ActiveSupport::Notifications` API) to handle incoming webhook requests.
9
+
10
+ ## Installation
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'mux-rails'
15
+ ```
16
+
17
+ ```ruby
18
+ # config/routes.rb
19
+ mount Mux::Engine, at: "/mux" # provide a custom path
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ### Setup Mux tokens
25
+
26
+ ```ruby
27
+ # config/initializers/mux.rb
28
+ MuxRuby.configure do |config|
29
+ config.username = ENV["MUX_TOKEN_ID"]
30
+ config.password = ENV["MUX_TOKEN_SECRET"]
31
+ end
32
+ ```
33
+
34
+ ### Creating assets
35
+
36
+ Create a Mux asset by using the `Mux::Client.create_asset(url)` method, passing
37
+ the url to the video. The method returns a Mux asset id.
38
+
39
+ ```ruby
40
+ mux_asset_id = Mux::Client.create_asset("http://foo.com/bar.mp4")
41
+ # You probably want to store mux_asset_id somewhere for future reference
42
+ ```
43
+
44
+ ### Deleting assets
45
+
46
+ Delete an asset using the `Mux::Client.destroy_asset(asset_id)` method, passing
47
+ the asset id:
48
+
49
+ ```ruby
50
+ Mux::Client.destroy_asset("mux_asset_id")
51
+ ```
52
+
53
+ ### Fetching asset
54
+
55
+ Fetch an asset using the `Mux::Client.get_asset(asset_id)` method, passing
56
+ the asset id. This returns a
57
+ [JSON object](https://docs.mux.com/docs/webhooks#section-example-response):
58
+
59
+ ```ruby
60
+ asset = Mux::Client.get_asset("mux_asset_id")
61
+ puts asset.data.status
62
+ ```
63
+
64
+ ### Handling webhook requests
65
+
66
+ Using a subscriber with a block we can listen to incoming events from Mux and do further work:
67
+
68
+ ```ruby
69
+ # config/initializers/mux.rb
70
+ # ...
71
+
72
+ Mux::Notifications.subscribe "video.asset.created" do |event|
73
+ # handle asset created
74
+ # event.object.id == mux_asset_id
75
+ # event.data.playback_ids.first.id
76
+ end
77
+
78
+ Mux::Notifications.subscribe "video.asset.ready" do |event|
79
+ # handle asset ready
80
+ # event.object.id == mux_asset_id
81
+ # event.data.playback_ids.first.id
82
+ end
83
+
84
+ Mux::Notifications.subscribe "video.asset.deleted" do |event|
85
+ # handle asset deleted
86
+ # event.object.id == mux_asset_id
87
+ # event.data.playback_ids.first.id
88
+ end
89
+ ```
90
+
91
+ The block is passed an event which is simply the incoming JSON (take a peek
92
+ in [fixtures](test/fixtures) for examples) wrapped in an OpenStruct for ease of access.
93
+
94
+ ## License
95
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Mux::Rails'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
@@ -0,0 +1,5 @@
1
+ module Mux
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery with: :exception
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ module Mux
2
+ class EventsController < ActionController::Base
3
+ # QUESTION: Is this the right way to handle it?
4
+ if ::Rails.application.config.action_controller.default_protect_from_forgery
5
+ skip_before_action :verify_authenticity_token
6
+ end
7
+
8
+ def create
9
+ Mux::Notifications.instrument(event)
10
+ head :ok
11
+ end
12
+
13
+ private
14
+
15
+ def event
16
+ event = JSON.parse(request.body.read, object_class: OpenStruct)
17
+ Mux::Event.new(event)
18
+ end
19
+ end
20
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Mux::Engine.routes.draw do
2
+ resources :events, only: :create
3
+ end
data/lib/mux/client.rb ADDED
@@ -0,0 +1,31 @@
1
+ require "mux_ruby"
2
+
3
+ module Mux
4
+ class Client
5
+ class << self
6
+ def create_asset(url, options = {})
7
+ playback_policy = options.fetch(:playback_policy, "public")
8
+ request = MuxRuby::CreateAssetRequest.new
9
+ request.input = url
10
+ request.playback_policy = MuxRuby::PlaybackPolicy.build_from_hash(playback_policy)
11
+ response = assets_api.create_asset(request)
12
+ return response.data.id
13
+ end
14
+
15
+ def destroy_asset(asset_id, options = {})
16
+ assets_api.delete_asset(asset_id, options)
17
+ end
18
+
19
+ # TODO: Figure out if and how to wrap the response
20
+ def get_asset(asset_id, options = {})
21
+ assets_api.get_asset(asset_id, options)
22
+ end
23
+
24
+ private
25
+
26
+ def assets_api
27
+ @assets_api ||= MuxRuby::AssetsApi.new
28
+ end
29
+ end
30
+ end
31
+ end
data/lib/mux/engine.rb ADDED
@@ -0,0 +1,5 @@
1
+ module Mux
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Mux
4
+ end
5
+ end
data/lib/mux/event.rb ADDED
@@ -0,0 +1,12 @@
1
+ module Mux
2
+ class Event
3
+ # QUESTION: Is there security concerns here? This implementation seems very naive ;-)
4
+ def initialize(payload)
5
+ @event = OpenStruct.new(payload)
6
+ end
7
+
8
+ def method_missing(method, *args, &block)
9
+ @event.send(method, *args, &block)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,27 @@
1
+ require "active_support/notifications"
2
+
3
+ module Mux
4
+ module Notifications
5
+ class << self
6
+ def instrument(event)
7
+ return unless event
8
+ ActiveSupport::Notifications.instrument "mux.#{event.type}", event
9
+ end
10
+
11
+ def subscribe(name, &block)
12
+ ActiveSupport::Notifications.subscribe %r{^mux\.#{name}}, SubscriberWrapper.call(block)
13
+ end
14
+ end
15
+
16
+ class SubscriberWrapper < Struct.new(:subscriber)
17
+ def self.call(subscriber)
18
+ new(subscriber)
19
+ end
20
+
21
+ def call(*args)
22
+ payload = args.last
23
+ subscriber.call(payload)
24
+ end
25
+ end
26
+ end
27
+ end
data/lib/mux/rails.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "mux/engine"
2
+
3
+ require "mux/notifications"
4
+ require "mux/event"
5
+ require "mux/client"
@@ -0,0 +1,5 @@
1
+ module Mux
2
+ module Rails
3
+ VERSION = "0.2.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mux-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Asger Behncke Jacobsen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mux_ruby
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.8.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A Rails Engine for integrating with Mux.
84
+ email:
85
+ - a@asgerbehnckejacobsen.dk
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - MIT-LICENSE
91
+ - README.md
92
+ - Rakefile
93
+ - app/controllers/mux/application_controller.rb
94
+ - app/controllers/mux/events_controller.rb
95
+ - config/routes.rb
96
+ - lib/mux/client.rb
97
+ - lib/mux/engine.rb
98
+ - lib/mux/event.rb
99
+ - lib/mux/notifications.rb
100
+ - lib/mux/rails.rb
101
+ - lib/mux/rails/version.rb
102
+ homepage: https://www.github.com/asgerb/mux-rails
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubygems_version: 3.1.2
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: A Rails Engine for integrating with Mux.
125
+ test_files: []