video_embed 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in video_embed.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Lee Jones
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # VideoEmbed
2
+
3
+ Generate html to embed videos based on a url.
4
+
5
+ Currently supported services:
6
+
7
+ * YouTube
8
+ * Vimeo
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'video_embed'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install video_embed
23
+
24
+ ## Usage
25
+
26
+ Default settings:
27
+
28
+ VideoEmbed.embed('http://www.youtube.com/watch?v=NtgtMQwr3Ko')
29
+ => "<iframe source=\"...
30
+
31
+ Specify a width and height:
32
+
33
+ VideoEmbed.embed('http://www.youtube.com/watch?v=NtgtMQwr3Ko',
34
+ :width => 1280,
35
+ :height => 720
36
+ )
37
+ => "<iframe source=\"...
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,11 @@
1
+ class VideoEmbed
2
+ class NoService
3
+ def url?(url)
4
+ true
5
+ end
6
+
7
+ def embed(url, options = {})
8
+ '<p>Video embed not available.</p>'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ class VideoEmbed
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,33 @@
1
+ class VideoEmbed
2
+ class Vimeo
3
+ def url?(url)
4
+ url.host =~ /vimeo.com/
5
+ end
6
+
7
+ def embed(url, options = {})
8
+ vimeo_url = Vimeo::Url.new(url, options)
9
+ vimeo_url.embed
10
+ end
11
+
12
+ class Url
13
+ attr_reader :url, :width, :height
14
+
15
+ def initialize(url, options = {})
16
+ @url = url
17
+ @width = options.fetch(:width, 560)
18
+ @height = options.fetch(:height, 315)
19
+ end
20
+
21
+ def embed
22
+ %Q{<iframe src="http://player.vimeo.com/video/#{video_id}?title=0&amp;byline=0&amp;portrait=0" width="#{width}" height="#{height}" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>}
23
+ end
24
+
25
+ private
26
+
27
+ def video_id
28
+ url.to_s.match(/vimeo.com\/(\d*)\??/)[1]
29
+ end
30
+ end
31
+ end
32
+ end
33
+
@@ -0,0 +1,36 @@
1
+ require 'cgi'
2
+
3
+ class VideoEmbed
4
+ class YouTube
5
+ def url?(url)
6
+ url.host =~ /youtube\.com/
7
+ end
8
+
9
+ def embed(url, options = {})
10
+ youtube_url = YouTube::Url.new(url, options)
11
+ youtube_url.embed
12
+ end
13
+
14
+ class Url
15
+ attr_reader :url, :width, :height
16
+
17
+ def initialize(url, options = {})
18
+ @url = url
19
+ @width = options.fetch(:width, 560)
20
+ @height = options.fetch(:height, 315)
21
+ end
22
+
23
+ def embed
24
+ %Q{<iframe width="#{width}" height="#{height}" src="http://www.youtube.com/embed/#{video_id}?rel=0" frameborder="0" allowfullscreen></iframe>}
25
+ end
26
+
27
+ private
28
+
29
+ def video_id
30
+ params = CGI.parse(url.query)
31
+ params['v'].first
32
+ end
33
+ end
34
+ end
35
+ end
36
+
@@ -0,0 +1,42 @@
1
+ require 'video_embed/version'
2
+ require 'uri'
3
+
4
+ class VideoEmbed
5
+ attr_reader :services
6
+
7
+ autoload :YouTube, 'video_embed/youtube'
8
+ autoload :Vimeo, 'video_embed/vimeo'
9
+ autoload :NoService, 'video_embed/no_service'
10
+
11
+ def initialize(services = nil)
12
+ if services.nil?
13
+ @services = default_services
14
+ else
15
+ @services = services
16
+ end
17
+ end
18
+
19
+ def embed(url, options = {})
20
+ if url.is_a? String
21
+ url = URI.parse(url)
22
+ end
23
+ service = find_service_by_url(url)
24
+ service.embed(url, options)
25
+ end
26
+
27
+ def self.embed(url, options = {})
28
+ video_embed = new
29
+ video_embed.embed(url, options)
30
+ end
31
+
32
+ private
33
+
34
+ def find_service_by_url(url)
35
+ services.find { |s| s.url?(url) }
36
+ end
37
+
38
+ def default_services
39
+ [YouTube.new, Vimeo.new, NoService.new]
40
+ end
41
+ end
42
+
@@ -0,0 +1,45 @@
1
+ require File.expand_path('../../lib/video_embed.rb', __FILE__)
2
+
3
+ describe VideoEmbed do
4
+ describe 'YouTube' do
5
+ it 'returns embed html from a url' do
6
+ video_embed = VideoEmbed.embed('http://www.youtube.com/watch?v=4Z3r9X8OahA&feature=fvwp&NR=1')
7
+ video_embed.should eql(%Q{<iframe width="560" height="315" src="http://www.youtube.com/embed/4Z3r9X8OahA?rel=0" frameborder="0" allowfullscreen></iframe>})
8
+ end
9
+
10
+ it 'accepts a custom width' do
11
+ video_embed = VideoEmbed.embed('http://www.youtube.com/watch?v=NtgtMQwr3Ko', :width => 1280)
12
+ video_embed.should match(/width="1280"/)
13
+ end
14
+
15
+ it 'accepts a custom height' do
16
+ video_embed = VideoEmbed.embed('http://www.youtube.com/watch?v=NtgtMQwr3Ko', :height => 720)
17
+ video_embed.should match(/height="720"/)
18
+ end
19
+ end
20
+
21
+ describe 'Vimeo' do
22
+ it 'returns embed html from a url' do
23
+ video_embed = VideoEmbed.embed('http://vimeo.com/11040425')
24
+ video_embed.should eql(%Q{<iframe src="http://player.vimeo.com/video/11040425?title=0&amp;byline=0&amp;portrait=0" width="560" height="315" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>})
25
+ end
26
+
27
+ it 'accepts a custom width' do
28
+ video_embed = VideoEmbed.embed('http://vimeo.com/11040425', :width => 720)
29
+ video_embed.should match(/width="720"/)
30
+ end
31
+
32
+ it 'accepts a custom height' do
33
+ video_embed = VideoEmbed.embed('http://vimeo.com/11040425', :height => 480)
34
+ video_embed.should match(/height="480"/)
35
+ end
36
+ end
37
+
38
+ describe 'Fallback' do
39
+ it 'returns an empty string when not a supported service' do
40
+ video_embed = VideoEmbed.embed('http://example.com/')
41
+ video_embed.should eql(%Q{<p>Video embed not available.</p>})
42
+ end
43
+ end
44
+ end
45
+
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'video_embed/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "video_embed"
8
+ gem.version = VideoEmbed::VERSION
9
+ gem.authors = ["Lee Jones"]
10
+ gem.email = ["scribblethink@gmail.com"]
11
+ gem.description = %q{Generate HTML to embed videos based on a URL}
12
+ gem.summary = %q{Video embed HTML generator}
13
+ gem.homepage = "http://github.com/leejones/video_embed"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_development_dependency('rspec')
20
+ gem.add_development_dependency('pry')
21
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: video_embed
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Lee Jones
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2013-02-06 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :development
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: pry
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :development
36
+ version_requirements: *id002
37
+ description: Generate HTML to embed videos based on a URL
38
+ email:
39
+ - scribblethink@gmail.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - LICENSE.txt
50
+ - README.md
51
+ - Rakefile
52
+ - lib/video_embed.rb
53
+ - lib/video_embed/no_service.rb
54
+ - lib/video_embed/version.rb
55
+ - lib/video_embed/vimeo.rb
56
+ - lib/video_embed/youtube.rb
57
+ - spec/video_embed_spec.rb
58
+ - video_embed.gemspec
59
+ homepage: http://github.com/leejones/video_embed
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.24
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Video embed HTML generator
86
+ test_files:
87
+ - spec/video_embed_spec.rb