acts_as_video 0.1.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
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+ gem 'json', '1.4.6'
6
+
7
+ # Add dependencies to develop your gem here.
8
+ # Include everything needed to run rake, tests, features, etc.
9
+ group :development do
10
+ gem 'rails', '3.0.5'
11
+ gem "mocha"
12
+ gem "fakeweb"
13
+ gem "fakeweb-matcher"
14
+ gem 'sqlite3-ruby', :require => 'sqlite3'
15
+ gem 'mysql'
16
+ gem 'ruby-debug'
17
+ gem 'shoulda-matchers'
18
+ gem "rspec", "~> 2.3.0"
19
+ gem "bundler", "~> 1.0.0"
20
+ gem "jeweler", "~> 1.5.2"
21
+ gem "rcov", ">= 0"
22
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,117 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.5)
6
+ actionpack (= 3.0.5)
7
+ mail (~> 2.2.15)
8
+ actionpack (3.0.5)
9
+ activemodel (= 3.0.5)
10
+ activesupport (= 3.0.5)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.4)
14
+ rack (~> 1.2.1)
15
+ rack-mount (~> 0.6.13)
16
+ rack-test (~> 0.5.7)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.5)
19
+ activesupport (= 3.0.5)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.4)
22
+ activerecord (3.0.5)
23
+ activemodel (= 3.0.5)
24
+ activesupport (= 3.0.5)
25
+ arel (~> 2.0.2)
26
+ tzinfo (~> 0.3.23)
27
+ activeresource (3.0.5)
28
+ activemodel (= 3.0.5)
29
+ activesupport (= 3.0.5)
30
+ activesupport (3.0.5)
31
+ arel (2.0.9)
32
+ builder (2.1.2)
33
+ columnize (0.3.2)
34
+ diff-lcs (1.1.2)
35
+ erubis (2.6.6)
36
+ abstract (>= 1.0.0)
37
+ fakeweb (1.3.0)
38
+ fakeweb-matcher (1.2.2)
39
+ fakeweb (>= 1.2.5)
40
+ rspec (>= 1.2.0)
41
+ git (1.2.5)
42
+ i18n (0.5.0)
43
+ jeweler (1.5.2)
44
+ bundler (~> 1.0.0)
45
+ git (>= 1.2.5)
46
+ rake
47
+ json (1.4.6)
48
+ linecache (0.43)
49
+ mail (2.2.19)
50
+ activesupport (>= 2.3.6)
51
+ i18n (>= 0.4.0)
52
+ mime-types (~> 1.16)
53
+ treetop (~> 1.4.8)
54
+ mime-types (1.16)
55
+ mocha (0.9.10)
56
+ rake
57
+ mysql (2.8.1)
58
+ polyglot (0.3.1)
59
+ rack (1.2.2)
60
+ rack-mount (0.6.14)
61
+ rack (>= 1.0.0)
62
+ rack-test (0.5.7)
63
+ rack (>= 1.0)
64
+ rails (3.0.5)
65
+ actionmailer (= 3.0.5)
66
+ actionpack (= 3.0.5)
67
+ activerecord (= 3.0.5)
68
+ activeresource (= 3.0.5)
69
+ activesupport (= 3.0.5)
70
+ bundler (~> 1.0)
71
+ railties (= 3.0.5)
72
+ railties (3.0.5)
73
+ actionpack (= 3.0.5)
74
+ activesupport (= 3.0.5)
75
+ rake (>= 0.8.7)
76
+ thor (~> 0.14.4)
77
+ rake (0.8.7)
78
+ rcov (0.9.9)
79
+ rspec (2.3.0)
80
+ rspec-core (~> 2.3.0)
81
+ rspec-expectations (~> 2.3.0)
82
+ rspec-mocks (~> 2.3.0)
83
+ rspec-core (2.3.1)
84
+ rspec-expectations (2.3.0)
85
+ diff-lcs (~> 1.1.2)
86
+ rspec-mocks (2.3.0)
87
+ ruby-debug (0.10.4)
88
+ columnize (>= 0.1)
89
+ ruby-debug-base (~> 0.10.4.0)
90
+ ruby-debug-base (0.10.4)
91
+ linecache (>= 0.3)
92
+ shoulda-matchers (1.0.0.beta2)
93
+ sqlite3 (1.3.3)
94
+ sqlite3-ruby (1.3.3)
95
+ sqlite3 (>= 1.3.3)
96
+ thor (0.14.6)
97
+ treetop (1.4.9)
98
+ polyglot (>= 0.3.1)
99
+ tzinfo (0.3.27)
100
+
101
+ PLATFORMS
102
+ ruby
103
+
104
+ DEPENDENCIES
105
+ bundler (~> 1.0.0)
106
+ fakeweb
107
+ fakeweb-matcher
108
+ jeweler (~> 1.5.2)
109
+ json (= 1.4.6)
110
+ mocha
111
+ mysql
112
+ rails (= 3.0.5)
113
+ rcov
114
+ rspec (~> 2.3.0)
115
+ ruby-debug
116
+ shoulda-matchers
117
+ sqlite3-ruby
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 anark
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.markdown ADDED
@@ -0,0 +1,55 @@
1
+ acts as video
2
+ =============
3
+ Create, Store, and Embed videos from different Oembed host using their url
4
+
5
+ * Vimeo
6
+ * Youtube
7
+
8
+ Usage
9
+ ----
10
+ Create a model to use as a video and setup migration
11
+
12
+ create_table :videos, :force => true do |t|
13
+ t.column :title, :string
14
+ t.column :embed_id, :string
15
+ t.column :type, :string
16
+ t.column :thumbnail_url, :string
17
+ end
18
+
19
+ To allow both vimeo and youtube videos
20
+
21
+ class Video < ActiveRecord::Base
22
+ acts_as_video
23
+ end
24
+
25
+ To allow only vimeo videos
26
+
27
+ class Video < ActiveRecord::Base
28
+ acts_as_video :video_hosts => [:vimeo]
29
+ end
30
+
31
+ Then create a model
32
+
33
+ video = Video.new :url => 'http://vimeo.com/15556095'
34
+
35
+ Acts as video will store the title, and thumbnail id. It also makes it easy to get the current embed code
36
+
37
+ video.title #=> 'Globe - 2010 Imaginarium - Ungu [magenta]'
38
+ video.thumbnail_url #=> "http:\/\/b.vimeocdn.com\/ts\/939\/007\/93900786_640.jpg'
39
+ video.embed_code #=> '<iframe src="http:\/\/player.vimeo.com\/video\/15556095" width="720" height="405" frameborder="0"><\/iframe>'
40
+ #embed_code(maxwidth, maxheight)
41
+ video.embed_code(200, 200) #=> '<iframe src="http:\/\/player.vimeo.com\/video\/15556095" width="200" height="113" frameborder="0"><\/iframe>'
42
+ Contributing to acts as video
43
+ -----
44
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
45
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
46
+ * Fork the project
47
+ * Start a feature/bugfix branch
48
+ * Commit and push until you are happy with your contribution
49
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
50
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
51
+
52
+ Copyright
53
+
54
+ Copyright (c) 2011 anark.
55
+
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "acts_as_video"
16
+ gem.homepage = "http://github.com/anark/acts_as_video"
17
+ gem.license = "MIT"
18
+ gem.summary = "Simple Rails video management"
19
+ gem.description = "Create, Store, and Embed videos from different Oembed host using their url"
20
+ gem.email = "anark@comcast.net"
21
+ gem.authors = ["anark"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "acts_as_video #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.2
@@ -0,0 +1,104 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{acts_as_video}
8
+ s.version = "0.1.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["anark"]
12
+ s.date = %q{2011-04-28}
13
+ s.description = %q{Create, Store, and Embed videos from different Oembed host using their url}
14
+ s.email = %q{anark@comcast.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.markdown",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "acts_as_video.gemspec",
29
+ "lib/acts_as_video.rb",
30
+ "lib/acts_as_video/acts_as_video.rb",
31
+ "lib/acts_as_video/hosts/vimeo.rb",
32
+ "lib/acts_as_video/hosts/youtube.rb",
33
+ "spec/acts_as_video/acts_as_video_spec.rb",
34
+ "spec/acts_as_video/vimeo_spec.rb",
35
+ "spec/acts_as_video/youtube_spec.rb",
36
+ "spec/database.yml",
37
+ "spec/database.yml.sample",
38
+ "spec/models.rb",
39
+ "spec/schema.rb",
40
+ "spec/spec_helper.rb"
41
+ ]
42
+ s.homepage = %q{http://github.com/anark/acts_as_video}
43
+ s.licenses = ["MIT"]
44
+ s.require_paths = ["lib"]
45
+ s.rubygems_version = %q{1.4.1}
46
+ s.summary = %q{Simple Rails video management}
47
+ s.test_files = [
48
+ "spec/acts_as_video/acts_as_video_spec.rb",
49
+ "spec/acts_as_video/vimeo_spec.rb",
50
+ "spec/acts_as_video/youtube_spec.rb",
51
+ "spec/models.rb",
52
+ "spec/schema.rb",
53
+ "spec/spec_helper.rb"
54
+ ]
55
+
56
+ if s.respond_to? :specification_version then
57
+ s.specification_version = 3
58
+
59
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
60
+ s.add_runtime_dependency(%q<json>, ["= 1.4.6"])
61
+ s.add_development_dependency(%q<rails>, ["= 3.0.5"])
62
+ s.add_development_dependency(%q<mocha>, [">= 0"])
63
+ s.add_development_dependency(%q<fakeweb>, [">= 0"])
64
+ s.add_development_dependency(%q<fakeweb-matcher>, [">= 0"])
65
+ s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
66
+ s.add_development_dependency(%q<mysql>, [">= 0"])
67
+ s.add_development_dependency(%q<ruby-debug>, [">= 0"])
68
+ s.add_development_dependency(%q<shoulda-matchers>, [">= 0"])
69
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
70
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
71
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
72
+ s.add_development_dependency(%q<rcov>, [">= 0"])
73
+ else
74
+ s.add_dependency(%q<json>, ["= 1.4.6"])
75
+ s.add_dependency(%q<rails>, ["= 3.0.5"])
76
+ s.add_dependency(%q<mocha>, [">= 0"])
77
+ s.add_dependency(%q<fakeweb>, [">= 0"])
78
+ s.add_dependency(%q<fakeweb-matcher>, [">= 0"])
79
+ s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
80
+ s.add_dependency(%q<mysql>, [">= 0"])
81
+ s.add_dependency(%q<ruby-debug>, [">= 0"])
82
+ s.add_dependency(%q<shoulda-matchers>, [">= 0"])
83
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
84
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
85
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
86
+ s.add_dependency(%q<rcov>, [">= 0"])
87
+ end
88
+ else
89
+ s.add_dependency(%q<json>, ["= 1.4.6"])
90
+ s.add_dependency(%q<rails>, ["= 3.0.5"])
91
+ s.add_dependency(%q<mocha>, [">= 0"])
92
+ s.add_dependency(%q<fakeweb>, [">= 0"])
93
+ s.add_dependency(%q<fakeweb-matcher>, [">= 0"])
94
+ s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
95
+ s.add_dependency(%q<mysql>, [">= 0"])
96
+ s.add_dependency(%q<ruby-debug>, [">= 0"])
97
+ s.add_dependency(%q<shoulda-matchers>, [">= 0"])
98
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
99
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
100
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
101
+ s.add_dependency(%q<rcov>, [">= 0"])
102
+ end
103
+ end
104
+
@@ -0,0 +1,85 @@
1
+ require 'net/http'
2
+ module ActsAsVideo
3
+ def self.included(base)
4
+ base.send :extend, ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def acts_as_video(options = {})
9
+ cattr_accessor :video_hosts
10
+ attr :url
11
+ validates_presence_of :url, :on => :create
12
+
13
+ self.video_hosts = (options[:video_hosts] || [:vimeo, :youtube])
14
+
15
+ self.extend Youtube
16
+ self.extend Vimeo
17
+
18
+ send :include, InstanceMethods
19
+
20
+ private
21
+ def domain_from_url(url)
22
+ matches = /(http:\/\/)?(www.)?([A-Za-z0-9._%-]*)\.com/.match(url)
23
+ matches.nil? ? raise("Invalid Url") : domain = matches[3]
24
+ end
25
+
26
+ def class_from_url(url)
27
+ # try to convert the url to a class, if no class exisists, rescue error and raise error
28
+ domain = domain_from_url(url)
29
+ raise "Unsupported Domain" unless video_hosts.include?(domain.to_sym)
30
+ domain_from_url(url).capitalize.constantize rescue raise "Unsupported Domain"
31
+ end
32
+ end
33
+ end
34
+
35
+ module InstanceMethods
36
+ def url=(url)
37
+ begin
38
+ domain_class = self.class.send :class_from_url, url
39
+ self.type = domain_class.to_s
40
+ self.embed_id = embed_id_from_url(url, domain_class)
41
+ data = response
42
+ self.title = data['title']
43
+ self.thumbnail_url = data['thumbnail_url']
44
+ @url = url
45
+ rescue Exception => ex
46
+ case ex.message
47
+ when "Unsupported Domain"
48
+ self.errors.add :url, "Unsupported Domain, supported video hosts are #{self.class.video_hosts.join(', ')}"
49
+ when "Invalid Url"
50
+ self.errors.add :url, "Invalid Url"
51
+ when "Video doesnt exist"
52
+ self.errors.add :url, "Video not found"
53
+ else
54
+ raise ex
55
+ end
56
+ end
57
+ end
58
+
59
+ def url
60
+ @url
61
+ end
62
+
63
+ def embed_id_from_url(url, host)
64
+ match = url.match(host::EMBED_ID_REGEX)
65
+ match[1] ? match[1] : raise("Video doesnt exist")
66
+ end
67
+
68
+ def embed_code(width = 720, height = 480)
69
+ response({:maxwidth => width, :maxheight => height})['html']
70
+ end
71
+
72
+ def embed_url
73
+ raise NotImplementedError, "Can only call #embed_url on a subclass of acts_as_video class" unless type
74
+ type.constantize.send :embed_url, embed_id
75
+ end
76
+
77
+ def response(options={})
78
+ arguments = options.map{ |key, value| "&#{key}=#{value}"}.join
79
+ uri = URI.parse(self.embed_url + arguments)
80
+ res = Net::HTTP.get_response(uri)
81
+ raise "Video doesnt exist" unless res.code == '200'
82
+ JSON.parse(res.body)
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,7 @@
1
+ module Vimeo
2
+ EMBED_ID_REGEX = /(?:http:\/\/)?(?:www\.)?vimeo.com\/(\d+)(?:$|\/)/
3
+
4
+ def self.embed_url(id)
5
+ "http://vimeo.com/api/oembed.json?url=http%3A//vimeo.com/#{id}"
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Youtube
2
+ EMBED_ID_REGEX = /(?:http:\/\/)?(?:www.)?youtube\.com\/watch\?v=([A-Za-z0-9._%-]*)(?:\&\S+)?/
3
+
4
+ def self.embed_url(id)
5
+ "http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch%3Fv%3D#{id}&format=json"
6
+ end
7
+ end
@@ -0,0 +1,14 @@
1
+ require "active_record"
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+
5
+ require "acts_as_video/acts_as_video"
6
+
7
+ require "acts_as_video/hosts/vimeo"
8
+ require "acts_as_video/hosts/youtube"
9
+
10
+ $LOAD_PATH.shift
11
+
12
+ if defined?(ActiveRecord::Base)
13
+ ActiveRecord::Base.send :include, ActsAsVideo
14
+ end
@@ -0,0 +1,140 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Video do
4
+ it { should validate_presence_of :url }
5
+
6
+ context "when using default options" do
7
+ let(:video) { Video.new }
8
+ subject { video }
9
+ it { should respond_to :url }
10
+
11
+ it "should have default video host options" do
12
+ video.video_hosts.should == [:vimeo, :youtube]
13
+ end
14
+
15
+ it "should raise error when embed url is called" do
16
+ lambda {video.embed_url}.should raise_error(NotImplementedError)
17
+ end
18
+ end
19
+
20
+ context "when using custom options" do
21
+ let(:video) { VimeoVideo.new }
22
+ it "should have only vimeo hosts options" do
23
+ video.video_hosts.should == [:vimeo]
24
+ end
25
+ end
26
+
27
+ describe "#url=" do
28
+ context "when supported url" do
29
+ vid_id = 123
30
+ FakeWeb.register_uri(:get, Vimeo.embed_url(vid_id), :status => ["200", "OK"], :body => {:title => "Hi"}.to_json)
31
+ subject { Video.new :url => "vimeo.com/#{vid_id}" }
32
+ it { should be_valid }
33
+ its(:type) { should == 'Vimeo' }
34
+ its(:title) { should == 'Hi' }
35
+ end
36
+
37
+ context "when invalid url" do
38
+ let(:video) {Video.new :url => 'www.27bslash6'}
39
+ subject { video }
40
+ it { should_not be_valid }
41
+ it "should have 1 error on url" do
42
+ video.errors[:url].size.should == 1
43
+ video.errors[:url].should include("Invalid Url")
44
+ end
45
+ end
46
+
47
+ context "when unsupported url" do
48
+ let(:video) {Video.new :url => 'www.27bslash6.com/foggot.html'}
49
+ subject { video }
50
+ it { should_not be_valid }
51
+ it "should have 1 error on url" do
52
+ video.errors[:url].size.should == 1
53
+ video.errors[:url].should include("Unsupported Domain, supported video hosts are vimeo, youtube")
54
+ end
55
+ end
56
+
57
+ context "when supported but excuded domain" do
58
+ let(:video) {VimeoVideo.new :url => 'http://www.youtube.com/watch?v=qs0rqOo2Rdw'}
59
+ subject { video }
60
+ it { should_not be_valid }
61
+ it "should have 1 error on url" do
62
+ video.errors[:url].size.should == 1
63
+ video.errors[:url].should include("Unsupported Domain, supported video hosts are vimeo")
64
+ end
65
+ end
66
+
67
+ context "when supported url but invalid url" do
68
+ vid_id = 'qs0rqOo2Rdw'
69
+ FakeWeb.register_uri(:get, Youtube.embed_url(vid_id), :status => ["404", "Not Found"])
70
+ let(:video) {Video.new :url => "http://www.youtube.com/watch?v=#{vid_id}"}
71
+ subject { video }
72
+ it { should_not be_valid }
73
+ it "should have 1 error on url" do
74
+ video.errors[:url].size.should == 1
75
+ video.errors[:url].should include("Video not found")
76
+ end
77
+ end
78
+ end
79
+
80
+ describe '#embed_code' do
81
+ FakeWeb.register_uri(:get, /http:\/\/www.youtube.com(?:.*)/, :status => ["200", "OK"], :body => {:html => 'embed'}.to_json)
82
+ let(:video) {Video.new :url => "http://www.youtube.com/watch?v=1234"}
83
+ it "should request embed code with default height and width" do
84
+ video.embed_code.should == 'embed'
85
+ FakeWeb.should have_requested(:get, /.*&maxwidth=720&maxheight=480/)
86
+ end
87
+
88
+ it "should request embed code with custom height and width" do
89
+ video.embed_code(200, 200).should == 'embed'
90
+ FakeWeb.should have_requested(:get, /.*&maxwidth=200&maxheight=200/)
91
+ end
92
+ end
93
+
94
+ describe '#domain_from_url' do
95
+ context "when valid url" do
96
+ domains = [
97
+ {:url => 'http://www.youtube.com/watch?v=qs0rqOo2Rdw', :domain => 'youtube'},
98
+ {:url => 'vimeo.com/15556095', :domain => 'vimeo'},
99
+ {:url => 'www.27bslash6.com/foggot.html', :domain => '27bslash6'}
100
+ ]
101
+ domains.each do |url|
102
+ it "should get domain from #{url[:url]}" do
103
+ domain = Video.send :domain_from_url, url[:url]
104
+ domain.should == url[:domain]
105
+ end
106
+ end
107
+ end
108
+
109
+ context "when invalid url" do
110
+ domains = [
111
+ {:url => 'http://www.youtube/watch?v=qs0rqOo2Rdw', :domain => 'youtube'},
112
+ {:url => 'vimeo/15556095', :domain => 'vimeo'},
113
+ {:url => 'www.27bslash6.abcdef/foggot.html', :domain => '27bslash6'}
114
+ ]
115
+ domains.each do |url|
116
+ it "should raise invalid url error from #{url[:url]}" do
117
+ lambda { Video.send :domain_from_url, url[:url] }.should raise_error("Invalid Url")
118
+ end
119
+ end
120
+ end
121
+ end
122
+
123
+ describe "#class_from_url" do
124
+ context "when supported url" do
125
+ subject {Video.send :class_from_url, 'vimeo.com/15556095'}
126
+ it { should == Vimeo }
127
+ end
128
+
129
+ context "when unsupported url" do
130
+ it "should raise error for unsupported domain" do
131
+ lambda {Video.send :class_from_url, 'www.27bslash6.com/foggot.html'}.should raise_error("Unsupported Domain")
132
+ end
133
+
134
+ it "should raise error for domain that is excluded from hosts" do
135
+ lambda {VimeoVideo.send :class_from_url, 'http://www.youtube.com/watch?v=qs0rqOo2Rdw'}.should raise_error("Unsupported Domain")
136
+ end
137
+ end
138
+ end
139
+ end
140
+
@@ -0,0 +1,31 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Vimeo do
4
+ before(:all) do
5
+ FakeWeb.clean_registry
6
+ end
7
+ describe '#new' do
8
+ context 'when vimeo' do
9
+ let(:video) { Video.new(:url => 'http://www.vimeo.com/17147129') }
10
+ subject { video }
11
+ it { should be_valid }
12
+ its(:type) { should == "Vimeo"}
13
+ its(:title) { should == 'Tourism Whistler Commercial' }
14
+ its(:thumbnail_url) { should == 'http://b.vimeocdn.com/ts/106/104/106104409_640.jpg' }
15
+ its(:embed_code) { should == '<iframe src="http://player.vimeo.com/video/17147129" width="720" height="405" frameborder="0"></iframe>'}
16
+ its(:embed_id) { should == '17147129'}
17
+ it "should have a smaller embed code if passed arguments" do
18
+ video.embed_code(200, 200).should == '<iframe src="http://player.vimeo.com/video/17147129" width="200" height="113" frameborder="0"></iframe>'
19
+ end
20
+ end
21
+
22
+ context 'when the video doesn\'t exist' do
23
+ let(:video) { Video.new :url => 'http://www.vimeo.com/1234567890' }
24
+ subject { video }
25
+ it { should_not be_valid }
26
+ it "should have 1 error on url" do
27
+ video.errors[:url].size.should == 1
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Youtube do
4
+ before(:all) do
5
+ FakeWeb.clean_registry
6
+ end
7
+ describe '#new' do
8
+ context 'when valid' do
9
+ let(:video) { Video.new :url => 'http://www.youtube.com/watch?v=qs0rqOo2Rdw' }
10
+ subject { video }
11
+ it { should be_valid }
12
+ its(:type) { should == "Youtube"}
13
+ its(:title) { should == 'Opening Day Shred Madness - Whistler/Blackcomb Opening Day 2010/2011' }
14
+ its(:thumbnail_url) { should == 'http://i2.ytimg.com/vi/qs0rqOo2Rdw/hqdefault.jpg' }
15
+ its(:embed_code) { should == "<object width=\"720\" height=\"430\"><param name=\"movie\" value=\"http://www.youtube.com/v/qs0rqOo2Rdw?version=3\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/qs0rqOo2Rdw?version=3\" type=\"application/x-shockwave-flash\" width=\"720\" height=\"430\" allowscriptaccess=\"always\" allowfullscreen=\"true\"></embed></object>"}
16
+ its(:embed_id) { should == 'qs0rqOo2Rdw'}
17
+ it "should have a smaller embed code if passed arguments" do
18
+ video.embed_code(200, 200).should == "<object width=\"200\" height=\"138\"><param name=\"movie\" value=\"http://www.youtube.com/v/qs0rqOo2Rdw?version=3\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/qs0rqOo2Rdw?version=3\" type=\"application/x-shockwave-flash\" width=\"200\" height=\"138\" allowscriptaccess=\"always\" allowfullscreen=\"true\"></embed></object>"
19
+ end
20
+ end
21
+
22
+ context 'when the video doesn\'t exist' do
23
+ let(:video) { Video.new(:url => 'http://www.youtube.com/watch?v=1234567890') }
24
+ subject { video }
25
+ it { should_not be_valid }
26
+ it "should have 1 error on url" do
27
+ video.errors[:url].size.should == 1
28
+ end
29
+ end
30
+ end
31
+ end
data/spec/database.yml ADDED
@@ -0,0 +1,17 @@
1
+ sqlite3:
2
+ adapter: sqlite3
3
+ database: acts_as_video.sqlite3
4
+
5
+ mysql:
6
+ adapter: mysql
7
+ hostname: localhost
8
+ username: root
9
+ password:
10
+ database: acts_as_video
11
+
12
+ postgresql:
13
+ adapter: postgresql
14
+ hostname: localhost
15
+ username: postgres
16
+ password:
17
+ database: acts_as_video
@@ -0,0 +1,17 @@
1
+ sqlite3:
2
+ adapter: sqlite3
3
+ database: acts_as_video.sqlite3
4
+
5
+ mysql:
6
+ adapter: mysql
7
+ hostname: localhost
8
+ username: root
9
+ password:
10
+ database: acts_as_video
11
+
12
+ postgresql:
13
+ adapter: postgresql
14
+ hostname: localhost
15
+ username: postgres
16
+ password:
17
+ database: acts_as_video
data/spec/models.rb ADDED
@@ -0,0 +1,7 @@
1
+ class Video < ActiveRecord::Base
2
+ acts_as_video
3
+ end
4
+
5
+ class VimeoVideo < ActiveRecord::Base
6
+ acts_as_video :video_hosts => [:vimeo]
7
+ end
data/spec/schema.rb ADDED
@@ -0,0 +1,15 @@
1
+ ActiveRecord::Schema.define :version => 0 do
2
+ create_table :videos, :force => true do |t|
3
+ t.column :title, :string
4
+ t.column :embed_id, :string
5
+ t.column :type, :string
6
+ t.column :thumbnail_url, :string
7
+ end
8
+
9
+ create_table :vimeo_videos, :force => true do |t|
10
+ t.column :title, :string
11
+ t.column :embed_id, :string
12
+ t.column :type, :string
13
+ t.column :thumbnail_url, :string
14
+ end
15
+ end
@@ -0,0 +1,66 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'acts_as_video'
5
+ require 'shoulda-matchers'
6
+ require 'fakeweb'
7
+ require 'fakeweb_matcher'
8
+
9
+ # Requires supporting files with custom matchers and macros, etc,
10
+ # in ./support/ and its subdirectories.
11
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
12
+
13
+ RSpec.configure do |config|
14
+ config.mock_with :mocha
15
+ end
16
+
17
+
18
+ $LOAD_PATH << "." unless $LOAD_PATH.include?(".")
19
+
20
+ begin
21
+ require "rubygems"
22
+ require "bundler"
23
+
24
+ if Gem::Version.new(Bundler::VERSION) <= Gem::Version.new("0.9.5")
25
+ raise RuntimeError, "Your bundler version is too old." +
26
+ "Run `gem install bundler` to upgrade."
27
+ end
28
+
29
+ # Set up load paths for all bundled gems
30
+ Bundler.setup
31
+ rescue Bundler::GemNotFound
32
+ raise RuntimeError, "Bundler couldn't find some gems." +
33
+ "Did you run \`bundlee install\`?"
34
+ end
35
+
36
+ Bundler.require
37
+ require File.expand_path('../../lib/acts_as_video', __FILE__)
38
+
39
+ ENV['DB'] ||= 'sqlite3'
40
+
41
+ database_yml = File.expand_path('../database.yml', __FILE__)
42
+ if File.exists?(database_yml)
43
+ active_record_configuration = YAML.load_file(database_yml)[ENV['DB']]
44
+
45
+ ActiveRecord::Base.establish_connection(active_record_configuration)
46
+
47
+ ActiveRecord::Base.silence do
48
+ ActiveRecord::Migration.verbose = false
49
+
50
+ load(File.dirname(__FILE__) + '/schema.rb')
51
+ load(File.dirname(__FILE__) + '/models.rb')
52
+ end
53
+
54
+ else
55
+ raise "Please create #{database_yml} first to configure your database. Take a look at: #{database_yml}.sample"
56
+ end
57
+
58
+ def clean_database!
59
+ models = [Video, VimeoVideo]
60
+ models.each do |model|
61
+ ActiveRecord::Base.connection.execute "DELETE FROM #{model.table_name}"
62
+ end
63
+ end
64
+
65
+ clean_database!
66
+
metadata ADDED
@@ -0,0 +1,284 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_video
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 2
10
+ version: 0.1.2
11
+ platform: ruby
12
+ authors:
13
+ - anark
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-28 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :runtime
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - "="
27
+ - !ruby/object:Gem::Version
28
+ hash: 11
29
+ segments:
30
+ - 1
31
+ - 4
32
+ - 6
33
+ version: 1.4.6
34
+ requirement: *id001
35
+ prerelease: false
36
+ name: json
37
+ - !ruby/object:Gem::Dependency
38
+ type: :development
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - "="
43
+ - !ruby/object:Gem::Version
44
+ hash: 13
45
+ segments:
46
+ - 3
47
+ - 0
48
+ - 5
49
+ version: 3.0.5
50
+ requirement: *id002
51
+ prerelease: false
52
+ name: rails
53
+ - !ruby/object:Gem::Dependency
54
+ type: :development
55
+ version_requirements: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ requirement: *id003
65
+ prerelease: false
66
+ name: mocha
67
+ - !ruby/object:Gem::Dependency
68
+ type: :development
69
+ version_requirements: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirement: *id004
79
+ prerelease: false
80
+ name: fakeweb
81
+ - !ruby/object:Gem::Dependency
82
+ type: :development
83
+ version_requirements: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ requirement: *id005
93
+ prerelease: false
94
+ name: fakeweb-matcher
95
+ - !ruby/object:Gem::Dependency
96
+ type: :development
97
+ version_requirements: &id006 !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: 3
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ requirement: *id006
107
+ prerelease: false
108
+ name: sqlite3-ruby
109
+ - !ruby/object:Gem::Dependency
110
+ type: :development
111
+ version_requirements: &id007 !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ hash: 3
117
+ segments:
118
+ - 0
119
+ version: "0"
120
+ requirement: *id007
121
+ prerelease: false
122
+ name: mysql
123
+ - !ruby/object:Gem::Dependency
124
+ type: :development
125
+ version_requirements: &id008 !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ hash: 3
131
+ segments:
132
+ - 0
133
+ version: "0"
134
+ requirement: *id008
135
+ prerelease: false
136
+ name: ruby-debug
137
+ - !ruby/object:Gem::Dependency
138
+ type: :development
139
+ version_requirements: &id009 !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ hash: 3
145
+ segments:
146
+ - 0
147
+ version: "0"
148
+ requirement: *id009
149
+ prerelease: false
150
+ name: shoulda-matchers
151
+ - !ruby/object:Gem::Dependency
152
+ type: :development
153
+ version_requirements: &id010 !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ~>
157
+ - !ruby/object:Gem::Version
158
+ hash: 3
159
+ segments:
160
+ - 2
161
+ - 3
162
+ - 0
163
+ version: 2.3.0
164
+ requirement: *id010
165
+ prerelease: false
166
+ name: rspec
167
+ - !ruby/object:Gem::Dependency
168
+ type: :development
169
+ version_requirements: &id011 !ruby/object:Gem::Requirement
170
+ none: false
171
+ requirements:
172
+ - - ~>
173
+ - !ruby/object:Gem::Version
174
+ hash: 23
175
+ segments:
176
+ - 1
177
+ - 0
178
+ - 0
179
+ version: 1.0.0
180
+ requirement: *id011
181
+ prerelease: false
182
+ name: bundler
183
+ - !ruby/object:Gem::Dependency
184
+ type: :development
185
+ version_requirements: &id012 !ruby/object:Gem::Requirement
186
+ none: false
187
+ requirements:
188
+ - - ~>
189
+ - !ruby/object:Gem::Version
190
+ hash: 7
191
+ segments:
192
+ - 1
193
+ - 5
194
+ - 2
195
+ version: 1.5.2
196
+ requirement: *id012
197
+ prerelease: false
198
+ name: jeweler
199
+ - !ruby/object:Gem::Dependency
200
+ type: :development
201
+ version_requirements: &id013 !ruby/object:Gem::Requirement
202
+ none: false
203
+ requirements:
204
+ - - ">="
205
+ - !ruby/object:Gem::Version
206
+ hash: 3
207
+ segments:
208
+ - 0
209
+ version: "0"
210
+ requirement: *id013
211
+ prerelease: false
212
+ name: rcov
213
+ description: Create, Store, and Embed videos from different Oembed host using their url
214
+ email: anark@comcast.net
215
+ executables: []
216
+
217
+ extensions: []
218
+
219
+ extra_rdoc_files:
220
+ - LICENSE.txt
221
+ - README.markdown
222
+ files:
223
+ - .document
224
+ - .rspec
225
+ - Gemfile
226
+ - Gemfile.lock
227
+ - LICENSE.txt
228
+ - README.markdown
229
+ - Rakefile
230
+ - VERSION
231
+ - acts_as_video.gemspec
232
+ - lib/acts_as_video.rb
233
+ - lib/acts_as_video/acts_as_video.rb
234
+ - lib/acts_as_video/hosts/vimeo.rb
235
+ - lib/acts_as_video/hosts/youtube.rb
236
+ - spec/acts_as_video/acts_as_video_spec.rb
237
+ - spec/acts_as_video/vimeo_spec.rb
238
+ - spec/acts_as_video/youtube_spec.rb
239
+ - spec/database.yml
240
+ - spec/database.yml.sample
241
+ - spec/models.rb
242
+ - spec/schema.rb
243
+ - spec/spec_helper.rb
244
+ has_rdoc: true
245
+ homepage: http://github.com/anark/acts_as_video
246
+ licenses:
247
+ - MIT
248
+ post_install_message:
249
+ rdoc_options: []
250
+
251
+ require_paths:
252
+ - lib
253
+ required_ruby_version: !ruby/object:Gem::Requirement
254
+ none: false
255
+ requirements:
256
+ - - ">="
257
+ - !ruby/object:Gem::Version
258
+ hash: 3
259
+ segments:
260
+ - 0
261
+ version: "0"
262
+ required_rubygems_version: !ruby/object:Gem::Requirement
263
+ none: false
264
+ requirements:
265
+ - - ">="
266
+ - !ruby/object:Gem::Version
267
+ hash: 3
268
+ segments:
269
+ - 0
270
+ version: "0"
271
+ requirements: []
272
+
273
+ rubyforge_project:
274
+ rubygems_version: 1.4.1
275
+ signing_key:
276
+ specification_version: 3
277
+ summary: Simple Rails video management
278
+ test_files:
279
+ - spec/acts_as_video/acts_as_video_spec.rb
280
+ - spec/acts_as_video/vimeo_spec.rb
281
+ - spec/acts_as_video/youtube_spec.rb
282
+ - spec/models.rb
283
+ - spec/schema.rb
284
+ - spec/spec_helper.rb