diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..085e803 Binary files /dev/null and b/.DS_Store differ diff --git a/Gemfile.lock b/Gemfile.lock index 12ecf3d..8136b78 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,6 +6,7 @@ PATH GEM specs: diff-lcs (1.1.2) + json (1.4.6) mocha (0.9.10) rake nokogiri (1.4.4) @@ -27,6 +28,7 @@ PLATFORMS DEPENDENCIES bundler (>= 1.0.0) flowplayer! + json mocha nokogiri rspec (~> 2.0.0) diff --git a/flowplayer.gemspec b/flowplayer.gemspec index 23dac1a..e7b37cb 100644 --- a/flowplayer.gemspec +++ b/flowplayer.gemspec @@ -23,5 +23,6 @@ Gem::Specification.new do |s| s.add_development_dependency "rspec", "~> 2.0.0" s.add_development_dependency "nokogiri" s.add_development_dependency "mocha" + s.add_development_dependency "json" end diff --git a/lib/.DS_Store b/lib/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/lib/.DS_Store differ diff --git a/lib/flowplayer.rb b/lib/flowplayer.rb new file mode 100644 index 0000000..0a1a395 --- /dev/null +++ b/lib/flowplayer.rb @@ -0,0 +1 @@ +require 'flowplayer/player' \ No newline at end of file diff --git a/lib/flowplayer/player.rb b/lib/flowplayer/player.rb new file mode 100644 index 0000000..ff10088 --- /dev/null +++ b/lib/flowplayer/player.rb @@ -0,0 +1,56 @@ +require 'json' +module Flowplayer + class Player + attr_accessor :options, :functions, :dom_id + def initialize(dom_id, &block) + @dom_id = dom_id + @options = {} + @functions = {} + block.call(self) + self + end + + def to_js + json = options_to_javascript + json += functions_to_javascript + "{#{json.join(', ')}}" + end + + def script_tags + <<-EOS + + EOS + end + + private + + def functions_to_javascript + functions.map {|option, function| "\"#{option}\":#{function}"} + end + + def options_to_javascript + options.map do |option, value| + if value.is_a?(String) + "\"#{option}\":\"#{value.to_json}\"" + else + "\"#{option}\":#{value.to_json}" + end + end + end + + def method_missing(method, *args, &block) + raise "Setters are not supported use method('whatever') to set configs" if /\=$/.match(method.to_s) + if block.nil? + options[method] = args.first + else + functions[method] = "function() { #{block.call.gsub(/\;$/, '')}; }" + end + return method + end + + end +end \ No newline at end of file diff --git a/lib/flowplayer/rails/helper.rb b/lib/flowplayer/rails/helper.rb new file mode 100644 index 0000000..78b91e9 --- /dev/null +++ b/lib/flowplayer/rails/helper.rb @@ -0,0 +1,19 @@ +module Flowplayer + module Helpers + + # flowplayer_for :hubble do |f| + # f.option 'foo' + # f.onLoad do + # 'this.unmute();' + # end + + + + + + def flowplayer_for(id, swf, &block) + FlowPlayer::Player.new(id, swf, &block).script_tags + end + + end +end \ No newline at end of file diff --git a/lib/flowplayer/railtie.rb b/lib/flowplayer/railtie.rb new file mode 100644 index 0000000..9715b33 --- /dev/null +++ b/lib/flowplayer/railtie.rb @@ -0,0 +1,8 @@ +module Flowplayer + require 'rails' + class Railtie < ::Rails::Railtie + generators do + require File.join(File.expand_path('../', __FILE__), 'railties', 'generators', 'compass_theme', 'compass_theme_generator.rb') + end + end +end \ No newline at end of file diff --git a/spec/flowplayer_spec.rb b/spec/flowplayer_spec.rb new file mode 100644 index 0000000..a10b42b --- /dev/null +++ b/spec/flowplayer_spec.rb @@ -0,0 +1,60 @@ +require 'spec_helper' + +describe Flowplayer::Player do + it "should set options from block" do + flow_player = Flowplayer::Player.new('my_video') do |player| + player.fullscreen true + player.logo(:url => nil, :opacity => 0, :fullscreenOnly => true) + player.onLoad do + 'this.unmute();' + end + end + + flow_player.options[:fullscreen].should == true + flow_player.functions[:onLoad].should == 'function() { this.unmute(); }' + flow_player.options[:logo].should be_an(Hash) + + end + + it "should set the dom_id to 'my_video'" do + flow_player = Flowplayer::Player.new('my_video') do |player| + player.fullscreen true + end + flow_player.dom_id.should == 'my_video' + end + + it "should generate valid options" do + flow_player = Flowplayer::Player.new('my_video') do |player| + player.fullscreen true + player.logo({:opacity => 0, :fullscreenOnly => true}) + player.onLoad do + 'this.unmute();' + end + end + flow_player.to_js.should include 'fullscreen' + flow_player.to_js.should match /^\{.+\}$/ + flow_player.to_js.should include({:opacity => 0, :fullscreenOnly => true}.to_json) + end + + it "should create script tags with options" do + flow_player = Flowplayer::Player.new('my_video') do |player| + player.fullscreen true + player.logo(:url => nil, :opacity => 0, :fullscreenOnly => true) + player.onLoad do + 'this.unmute();' + end + end + ["", "flowplayer(\"my_video\", #{flow_player.to_js}"].each do |part| + flow_player.script_tags.should include part + end + end + + it "should raise exception if passed a setter" do + lambda do + flow_player = Flowplayer::Player.new('my_video') do |player| + player.fullscreen = true + end + end.should raise_error + end + +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..79f7869 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1 @@ +require File.expand_path('../../lib/flowplayer', __FILE__) \ No newline at end of file