swf_ditty 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Zeke Sikelianos
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.rdoc ADDED
@@ -0,0 +1,44 @@
1
+ = SWF Ditty (swiff ditty)
2
+
3
+ A simple and flexible Sinatra helper for embedding SWFs in your views.
4
+ Requires {jQuery}[http://jquery.com/] and {jQuery SWFObject}[http://jquery.thewikies.com/swfobject/].
5
+
6
+ == Installation
7
+
8
+ Install {gemcutter}[http://gemcutter.org] if you haven't already:
9
+
10
+ sudo gem install gemcutter
11
+ gem tumble
12
+
13
+ Install the ratpack gem:
14
+
15
+ sudo gem install swf_ditty
16
+
17
+ Drop this line in your app:
18
+
19
+ gem 'swf_ditty'
20
+ require 'sinatra/swf_ditty'
21
+
22
+ == Usage
23
+
24
+ The following example assumes you're using {haml}[http://haml-lang.com/] and my {ratpack}[http://github.com/zeke/ratpack] helpers.
25
+
26
+ %html
27
+ %head
28
+ =javascript_include_tag %w(jquery-1.3.2.min.js jquery.swfobject.1-0-7.min)
29
+ %body
30
+ = swf 'my_swf.swf'
31
+ = swf 'subdirectory/of/public/another.swf', :width => 500, :height => 300, :flashvars => {:foo => 'chacha', :bar => 250}
32
+ = swf 'http://example.com/yet_another.swf', :wmode => 'opaque'
33
+
34
+ See full auto-generated documentation at {rdoc.info/projects/zeke/swf_ditty}[http://rdoc.info/projects/zeke/swf_ditty]
35
+
36
+ == Colophon
37
+
38
+ * Thanks to the Sinatra page on {Writing Extensions}[http://www.sinatrarb.com/extensions.html]
39
+ * Tests modified from {http://github.com/wbzyl/sinatra-static-assets/}[wbzyl/sinatra-static-assets]
40
+ * Gem built with {http://github.com/technicalpickles/jeweler/}[Jeweler]
41
+
42
+ == Copyright
43
+
44
+ Copyright (c) 2009 Zeke Sikelianos. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "swf_ditty"
8
+ gem.summary = %Q{A simple and flexible Sinatra helper for embedding SWFs in your views. }
9
+ gem.description = %Q{A simple and flexible Sinatra helper for embedding SWFs in your views. Depends on jQuery and jQuery SWFObject}
10
+ gem.email = "zeke@sikelianos.com"
11
+ gem.homepage = "http://github.com/zeke/swf_ditty"
12
+ gem.authors = ["Zeke Sikelianos"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "swf_ditty #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
@@ -0,0 +1,87 @@
1
+ require 'sinatra/base'
2
+
3
+ module Sinatra
4
+ module SwfDitty
5
+
6
+ def render_swf(swf, *args)
7
+ options = {
8
+ :swf => swf,
9
+ :dom_id => filename_to_dom_id(swf),
10
+ :name => filename_to_dom_id(swf),
11
+ :width => "100%",
12
+ :height => "100%",
13
+ :wmode => "opaque",
14
+ :allowScriptAccess => "sameDomain",
15
+ :create_dom_container => true,
16
+ }.merge(args.extract_options!)
17
+
18
+ out = []
19
+
20
+ # Yank dom_id out of the options hash
21
+ dom_id = options.delete(:dom_id)
22
+
23
+ # Yank create_dom_container option out of the hash, if it exists
24
+ # Create DOM container if option is set to something other than false or nil
25
+ out << content_tag(:div, "", :id => dom_id) if options.delete(:create_dom_container)
26
+
27
+ # Turn flashvars hash into a flashvars-style formatted string
28
+ options[:flashvars] = "{" + hash_to_key_value_string(options[:flashvars]) + "}" if options[:flashvars]
29
+
30
+ # Format options hash (excluding flashvars) into a key:value string..
31
+ embed = hash_to_key_value_string(options)
32
+
33
+ # Spit it out!
34
+ out << content_tag(:script, "$(document).ready(function(){$('##{dom_id}').flash({#{embed}});});", :type => "text/javascript", :charset => "utf-8")
35
+ out.reverse.join("\n")
36
+ end
37
+
38
+ # Convert a string to dom-friendly format.
39
+ # Adapted from permalink_fu
40
+ def filename_to_dom_id(str)
41
+ result = str.split("/").last.gsub(".", "_") # e.g. 'foo.swf' -> 'foo_swf'
42
+ result.gsub!(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics).
43
+ result.gsub!(/[^\w \-]+/i, '') # Remove unwanted chars.
44
+ result.gsub!(/[ \-]+/i, '-') # No more than one of the separator in a row.
45
+ result.gsub!(/^\-|\-$/i, '') # Remove leading/trailing separator.
46
+ result.downcase
47
+ end
48
+
49
+ # Convert a hash to a string of k:v pairs, delimited by commas
50
+ # Wrap in quotes unless numeric or flashvars hash
51
+ def hash_to_key_value_string(hash)
52
+ hash.each_pair.map do |k,v|
53
+ v = "'#{v}'" unless k.to_s=='flashvars' || !v.to_s.match(/^[0-9]*\.[0-9]+|[0-9]+$/).nil?
54
+ "#{k}:#{v}"
55
+ end.sort.join(", ")
56
+ end
57
+
58
+ # Sinatra doesn't have this method, but Rails does..
59
+ unless method_defined?(:content_tag)
60
+ def content_tag(name,content,options={})
61
+ options = options.map{ |k,v| "#{k}='#{v}'" }.join(" ")
62
+ "<#{name} #{options}>#{content}</#{name}>"
63
+ end
64
+ end
65
+
66
+ # Add these methods to Sinatra's helper methods..
67
+ helpers SwfDitty
68
+ end
69
+ end
70
+
71
+ class Hash
72
+ # Sinatra doesn't have this method, but Rails does..
73
+ unless method_defined?(:symbolize_keys)
74
+ def symbolize_keys
75
+ self.inject({}) { |h,(k,v)| h[k.to_sym] = v; h }
76
+ end
77
+ end
78
+ end
79
+
80
+ class Array
81
+ # Sinatra doesn't have this method, but Rails does..
82
+ unless method_defined?('extract_options!')
83
+ def extract_options!
84
+ last.is_a?(::Hash) ? pop : {}
85
+ end
86
+ end
87
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'sinatra/swf_ditty'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,44 @@
1
+ path = File.expand_path("../lib" + File.dirname(__FILE__))
2
+ $:.unshift(path) unless $:.include?(path)
3
+
4
+ require 'rubygems'
5
+ require 'sinatra'
6
+ require 'sinatra/swf_ditty'
7
+
8
+ get "/render_swf_plain" do
9
+ content_type "text/plain"
10
+ <<"EOD"
11
+ #{render_swf("swf/foo.swf")}
12
+ EOD
13
+ end
14
+
15
+ get "/render_swf_with_custom_dom_id" do
16
+ content_type "text/plain"
17
+ <<"EOD"
18
+ #{render_swf("swf/foo.swf", :dom_id => "dombo", :create_dom_container => false)}
19
+ EOD
20
+ end
21
+
22
+ get "/render_swf_with_flashvars" do
23
+ content_type "text/plain"
24
+ <<"EOD"
25
+ #{render_swf("foo.swf", :height => 50, :flashvars => {:a => 1, :b => 'two'})}
26
+ EOD
27
+ end
28
+
29
+ get "/filename_to_dom_id" do
30
+ content_type "text/plain"
31
+ <<"EOD"
32
+ #{filename_to_dom_id("alpha.swf")}
33
+ #{filename_to_dom_id("substandard/bravo.swf")}
34
+ #{filename_to_dom_id("http://bar.org/xyz/charlie.swf")}
35
+ EOD
36
+ end
37
+
38
+ get "/hash_to_key_value_string" do
39
+ content_type "text/plain"
40
+ <<"EOD"
41
+ #{hash_to_key_value_string({:a => 1, :b => 2})}
42
+ #{hash_to_key_value_string({:alpha => 'male', :bravo => 'hamster'})}
43
+ EOD
44
+ end
@@ -0,0 +1,60 @@
1
+ require 'sinatra_app'
2
+ require 'test/unit'
3
+ require 'rack/test'
4
+ require 'helper'
5
+
6
+ set :environment, :test
7
+
8
+ class TestSwfDitty < Test::Unit::TestCase
9
+ include Rack::Test::Methods
10
+
11
+ def app
12
+ Sinatra::Application
13
+ end
14
+
15
+ def test_render_swf_plain
16
+ get '/render_swf_plain'
17
+ assert last_response.ok?
18
+ assert_equal last_response.body, <<EOD
19
+ <script type='text/javascript' charset='utf-8'>$(document).ready(function(){$('#foo_swf').flash({allowScriptAccess:'sameDomain', height:'100%', name:'foo_swf', swf:'swf/foo.swf', width:'100%', wmode:'opaque'});});</script>
20
+ <div id='foo_swf'></div>
21
+ EOD
22
+ end
23
+
24
+ def test_render_swf_with_custom_dom_id
25
+ get '/render_swf_with_custom_dom_id'
26
+ assert last_response.ok?
27
+ assert_equal last_response.body, <<EOD
28
+ <script type='text/javascript' charset='utf-8'>$(document).ready(function(){$('#dombo').flash({allowScriptAccess:'sameDomain', height:'100%', name:'foo_swf', swf:'swf/foo.swf', width:'100%', wmode:'opaque'});});</script>
29
+ EOD
30
+ end
31
+
32
+ def test_render_swf_with_flashvars
33
+ get '/render_swf_with_flashvars'
34
+ assert last_response.ok?
35
+ assert_equal last_response.body, <<EOD
36
+ <script type='text/javascript' charset='utf-8'>$(document).ready(function(){$('#foo_swf').flash({allowScriptAccess:'sameDomain', flashvars:{a:1, b:'two'}, height:50, name:'foo_swf', swf:'foo.swf', width:'100%', wmode:'opaque'});});</script>
37
+ <div id='foo_swf'></div>
38
+ EOD
39
+ end
40
+
41
+ def test_filename_to_dom_id
42
+ get '/filename_to_dom_id'
43
+ assert last_response.ok?
44
+ assert_equal last_response.body, <<EOD
45
+ alpha_swf
46
+ bravo_swf
47
+ charlie_swf
48
+ EOD
49
+ end
50
+
51
+ def test_hash_to_key_value_string
52
+ get '/hash_to_key_value_string'
53
+ assert last_response.ok?
54
+ assert_equal last_response.body, <<EOD
55
+ a:1, b:2
56
+ alpha:'male', bravo:'hamster'
57
+ EOD
58
+ end
59
+
60
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: swf_ditty
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Zeke Sikelianos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-02 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: A simple and flexible Sinatra helper for embedding SWFs in your views. Depends on jQuery and jQuery SWFObject
26
+ email: zeke@sikelianos.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/sinatra/swf_ditty.rb
42
+ - test/helper.rb
43
+ - test/sinatra_app.rb
44
+ - test/test_swf_ditty.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/zeke/swf_ditty
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.5
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: A simple and flexible Sinatra helper for embedding SWFs in your views.
73
+ test_files:
74
+ - test/helper.rb
75
+ - test/sinatra_app.rb
76
+ - test/test_swf_ditty.rb