diff --git a/README.md b/README.md index 5086009..fa1a6dd 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,37 @@ job = Coconut::Job.create( ) ``` +A job without config file can contain outputs with same key (like hls). Since the Hash instance can't have duplicate keys, you have to prefix them: + +```ruby +vid = 1234 +s3 = "s3://accesskey:secretkey@mybucket" + +job = Coconut::Job.create( + :api_key => "k-api-key", + :source => "http://yoursite.com/media/video.mp4", + :webhook => "http://mysite.com/webhook/coconut?videoId=#{vid}", + :outputs => { + "2@hls" => "#{s3}/videos/playlist.m3u8, variants=hls:360p, if=$source_height > 300", + "1@hls" => "#{s3}/videos/playlist.m3u8, variants=hls:240p, if=$source_height < 300", + } +) +``` + +The output config file will contain 2 hls outputs, sorted by prefix: + +```ini +var s3 = s3://accesskey:secretkey@mybucket + +set webhook = http://mysite.com/webhook/coconut?videoId=$vid + +-> hls = $s3/videos/playlist.m3u8, variants=hls:240p, if=$source_height < 300 +-> hls = $s3/videos/playlist.m3u8, variants=hls:360p, if=$source_height > 300 +``` + +The prefix can be any digital or a-zA-Z char. + + Note that you can use the environment variable `COCONUT_API_KEY` to set your API key. ## Contributing diff --git a/lib/coconutrb.rb b/lib/coconutrb.rb index c8f46fc..d945eb9 100644 --- a/lib/coconutrb.rb +++ b/lib/coconutrb.rb @@ -69,7 +69,7 @@ def self.config(options={}) new_conf << "" new_conf.concat conf.select{|l| l.start_with?("set")}.sort new_conf << "" - new_conf.concat conf.select{|l| l.start_with?("->")}.sort + new_conf.concat conf.select{|l| l.start_with?("->")}.sort.map{ |output| output.sub /^(->\s+)[\w\d]+?@/, '\1' } return new_conf.join("\n") end diff --git a/test/coconutrb_test.rb b/test/coconutrb_test.rb index b6b52c5..833ce84 100644 --- a/test/coconutrb_test.rb +++ b/test/coconutrb_test.rb @@ -83,6 +83,56 @@ def test_generate_full_config_with_no_file assert_equal generated, config end + def test_generate_config_with_no_file_and_multiple_same_typed_outputs + base_conf = { + :vars => { + :vid => 1234, + :user => 5098, + :s3 => "s3://a:s@bucket" + }, + :source => "https://s3-eu-west-1.amazonaws.com/files.coconut.co/test.mp4", + :webhook => "http://mysite.com/webhook?vid=$vid&user=$user", + } + + output_without_prefix = { :outputs => { + "hls" => "$s3/playlist.m3u8, variants=hls:240p,hls:360p", + } } + + outputs_with_num_prefix = { :outputs => { + "2@hls" => "$s3/playlist.m3u8, variants=hls:480p,hls:720p, if=$source_height > 300", + "1@hls" => "$s3/playlist.m3u8, variants=hls:240p,hls:360p, if=$source_height < 300", + } } + + outputs_with_az_prefix = { :outputs => { + "cd@hls" => "$s3/playlist.m3u8, variants=hls:480p,hls:720p, if=$source_height > 300", + "ad@hls" => "$s3/playlist.m3u8, variants=hls:240p,hls:360p, if=$source_height < 300", + } } + + config_no_prefix = Coconut.config(base_conf.merge(output_without_prefix)) + config_num = Coconut.config(base_conf.merge(outputs_with_num_prefix)) + config_az = Coconut.config(base_conf.merge(outputs_with_az_prefix)) + + generated_base = [ + "var s3 = s3://a:s@bucket", + "var user = 5098", + "var vid = 1234", + "", + "set source = https://s3-eu-west-1.amazonaws.com/files.coconut.co/test.mp4", + "set webhook = http://mysite.com/webhook?vid=$vid&user=$user", + "", + ] + + generated_no_prefix = generated_base.dup.push("-> hls = $s3/playlist.m3u8, variants=hls:240p,hls:360p").join("\n") + + generated_with_prefix = generated_base.dup.push("-> hls = $s3/playlist.m3u8, variants=hls:240p,hls:360p, if=$source_height < 300", + "-> hls = $s3/playlist.m3u8, variants=hls:480p,hls:720p, if=$source_height > 300").join("\n") + + assert_equal generated_no_prefix, config_no_prefix + assert_equal generated_with_prefix, config_num + assert_equal generated_with_prefix, config_az + + end + def test_generate_config_with_file File.open("coconut.conf", "w") {|f| f.write("var s3 = s3://a:s@bucket/video\nset webhook = http://mysite.com/webhook?vid=$vid&user=$user\n-> mp4 = $s3/$vid.mp4")}