laserlemon-videoclip 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Steve Richert
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/Manifest ADDED
@@ -0,0 +1,13 @@
1
+ init.rb
2
+ lib/videoclip/video.rb
3
+ lib/videoclip/videos/youtube.rb
4
+ lib/videoclip.rb
5
+ Manifest
6
+ MIT-LICENSE
7
+ Rakefile
8
+ README.rdoc
9
+ tasks/videoclip_tasks.rake
10
+ test/test_helper.rb
11
+ test/videoclip_test.rb
12
+ VERSION.yml
13
+ videoclip.gemspec
data/README.rdoc ADDED
@@ -0,0 +1,13 @@
1
+ = videoclip
2
+
3
+ == Installation
4
+
5
+ script/plugin install git://github.com/laserlemon/videoclip.git
6
+
7
+ == Example
8
+
9
+ Coming soon...
10
+
11
+ == Tips
12
+
13
+ Coming soon...
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('videoclip', '0.1.0') do |g|
6
+ g.description = %(Save videos from popular sites alongside your ActiveRecord models)
7
+ g.url = 'http://github.com/laserlemon/videoclip'
8
+ g.author = 'Steve Richert'
9
+ g.email = 'steve@laserlemon.com'
10
+ g.ignore_pattern = %w(tmp/* script/*)
11
+ g.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each{|t| load t }
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'videoclip'
@@ -0,0 +1,129 @@
1
+ module LaserLemon
2
+ module Videoclip
3
+ class Video
4
+ cattr_reader :implementations
5
+ @@implementations = []
6
+
7
+ class << self
8
+ def inherited(subclass)
9
+ @@implementations << subclass
10
+ end
11
+
12
+ def match(uri, host = nil)
13
+ begin
14
+ klass = @@implementations.detect{|i| (i.host == host) || i.match?(uri) }
15
+ raise if klass.nil?
16
+ rescue
17
+ raise UnrecognizedVideoHost
18
+ else klass
19
+ end
20
+ end
21
+
22
+ def parse_url(url)
23
+ begin
24
+ uri = URI.parse(url)
25
+ raise unless uri.is_a?(URI::HTTP)
26
+ rescue
27
+ raise InvalidUrlFormat
28
+ else uri
29
+ end
30
+ end
31
+
32
+ def build(host, key, url)
33
+ uri = parse_url(url)
34
+ klass = match(uri, host)
35
+ klass.new(key, url)
36
+ end
37
+
38
+ def assign(url)
39
+ uri = parse_url(url)
40
+ klass = match(uri)
41
+ video = klass.new
42
+ video.assign(uri)
43
+ video
44
+ end
45
+
46
+ def host
47
+ self.name.demodulize.underscore
48
+ end
49
+ end
50
+
51
+ attr_reader :host, :key, :url
52
+ attr_accessor :options
53
+
54
+ def initialize(key = nil, url = nil)
55
+ @host = self.class.host
56
+ @key, @url = key, url
57
+ end
58
+
59
+ protected
60
+
61
+ def width(style)
62
+ s = get_style(style)
63
+ w = s[:width] || (s[:height].to_f * aspect_ratio).floor
64
+ w += chrome_width unless w.zero? || s[:include_chrome]
65
+ w
66
+ end
67
+
68
+ def height(style)
69
+ s = get_style(style)
70
+ h = s[:height] || (s[:width].to_f / aspect_ratio).ceil
71
+ h += chrome_height unless h.zero? || s[:include_chrome]
72
+ h
73
+ end
74
+
75
+ def aspect_ratio
76
+ 16.0 / 9
77
+ end
78
+
79
+ def chrome_width
80
+ 0
81
+ end
82
+
83
+ def chrome_height
84
+ 0
85
+ end
86
+
87
+ private
88
+
89
+ def get_style(style)
90
+ case style
91
+ when nil: get_style(default_style)
92
+ when Hash: style
93
+ when String: geometry_to_style(style)
94
+ else get_style(styles.fetch(style, {}))
95
+ end
96
+ end
97
+
98
+ def default_style
99
+ style_name = case
100
+ when @options.has_key?(:default_style): @options[:default_style]
101
+ when styles.size == 1: styles.keys.first
102
+ else :default
103
+ end
104
+ styles.fetch(style_name, {})
105
+ end
106
+
107
+ def styles
108
+ @options.fetch(:styles, {})
109
+ end
110
+
111
+ def geometry_to_style(geometry)
112
+ style = {}
113
+ style[:include_chrome] = !geometry.dup.sub!(/!$/, '').nil?
114
+ width, height = geometry.split('x')
115
+ style.merge!(:width => width.to_i, :height => height.to_i)
116
+ style.delete_if{|k,v| v == 0 }
117
+ end
118
+ end
119
+
120
+ class VideoclipError < StandardError
121
+ end
122
+
123
+ class InvalidUrlFormat < VideoclipError
124
+ end
125
+
126
+ class UnrecognizedVideoHost < VideoclipError
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,24 @@
1
+ module LaserLemon
2
+ module Videoclip
3
+ class Video::Youtube < Video
4
+ def self.match?(uri)
5
+ (uri.host =~ /^(?:www\.)?youtube\.com$/i) && (uri.path == '/watch') && !CGI.parse(uri.query)['v'].blank?
6
+ end
7
+
8
+ def assign(uri)
9
+ @key = CGI.parse(uri.query)['v'].first
10
+ @url = "http://www.youtube.com/watch?v=#{@key}"
11
+ end
12
+
13
+ def embed(style = nil)
14
+ %(<object width="#{width(style)}" height="#{height(style)}"><param name="movie" value="http://www.youtube.com/v/#{key}&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/#{key}&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="#{width(style)}" height="#{height(style)}"></embed></object>)
15
+ end
16
+
17
+ protected
18
+
19
+ def chrome_height
20
+ 25
21
+ end
22
+ end
23
+ end
24
+ end
data/lib/videoclip.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'videoclip/video'
2
+ Dir.glob(Rails.root + '/lib/videoclip/*.rb').each{|v| require v }
3
+ Dir.glob(File.dirname(__FILE__) + '/videoclip/videos/*.rb').each{|v| require v }
4
+
5
+ module LaserLemon
6
+ module Videoclip
7
+ def self.included(base)
8
+ base.extend ClassMethods
9
+ end
10
+
11
+ module ClassMethods
12
+ def has_video(name = :video, options = {})
13
+ class_inheritable_hash :videoclip_options
14
+ self.videoclip_options = {name.to_sym => options}
15
+
16
+ composed_of name,
17
+ :class_name => 'LaserLemon::Videoclip::Video',
18
+ :mapping => %w(host key url).map{|x| %W(#{name}_#{x} #{x}) },
19
+ :constructor => :build,
20
+ :converter => :assign,
21
+ :allow_nil => true
22
+
23
+ define_method "#{name}_with_options" do
24
+ v = send("#{name}_without_options")
25
+ v.options ||= self.class.videoclip_options[name.to_sym]
26
+ v
27
+ end
28
+
29
+ alias_method_chain name, :options
30
+
31
+ define_method "#{name}?" do
32
+ !! send(name)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ ActiveRecord::Base.send(:include, LaserLemon::Videoclip)
File without changes
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class VideoclipTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
data/videoclip.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{videoclip}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Steve Richert"]
9
+ s.date = %q{2009-05-01}
10
+ s.description = %q{Save videos from popular sites alongside your ActiveRecord models}
11
+ s.email = %q{steve@laserlemon.com}
12
+ s.extra_rdoc_files = ["lib/videoclip/video.rb", "lib/videoclip/videos/youtube.rb", "lib/videoclip.rb", "README.rdoc", "tasks/videoclip_tasks.rake"]
13
+ s.files = ["init.rb", "lib/videoclip/video.rb", "lib/videoclip/videos/youtube.rb", "lib/videoclip.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README.rdoc", "tasks/videoclip_tasks.rake", "test/test_helper.rb", "test/videoclip_test.rb", "VERSION.yml", "videoclip.gemspec"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/laserlemon/videoclip}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Videoclip", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{videoclip}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Save videos from popular sites alongside your ActiveRecord models}
21
+ s.test_files = ["test/test_helper.rb", "test/videoclip_test.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 2
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ else
29
+ end
30
+ else
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: laserlemon-videoclip
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Steve Richert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-01 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Save videos from popular sites alongside your ActiveRecord models
17
+ email: steve@laserlemon.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - lib/videoclip/video.rb
24
+ - lib/videoclip/videos/youtube.rb
25
+ - lib/videoclip.rb
26
+ - README.rdoc
27
+ - tasks/videoclip_tasks.rake
28
+ files:
29
+ - init.rb
30
+ - lib/videoclip/video.rb
31
+ - lib/videoclip/videos/youtube.rb
32
+ - lib/videoclip.rb
33
+ - Manifest
34
+ - MIT-LICENSE
35
+ - Rakefile
36
+ - README.rdoc
37
+ - tasks/videoclip_tasks.rake
38
+ - test/test_helper.rb
39
+ - test/videoclip_test.rb
40
+ - VERSION.yml
41
+ - videoclip.gemspec
42
+ has_rdoc: true
43
+ homepage: http://github.com/laserlemon/videoclip
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --line-numbers
47
+ - --inline-source
48
+ - --title
49
+ - Videoclip
50
+ - --main
51
+ - README.rdoc
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: "1.2"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project: videoclip
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: Save videos from popular sites alongside your ActiveRecord models
73
+ test_files:
74
+ - test/test_helper.rb
75
+ - test/videoclip_test.rb