youtube-embed 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eee7226c806fa32c594e95735e0c4c1f8bbb92ca
4
+ data.tar.gz: 816194cb5cc2c7e2efbf77d7a308a7b499fea5fe
5
+ SHA512:
6
+ metadata.gz: 8e52de25c74404c51d0bef967a398459416f355007c2a4611aabf3f4a66be73d279fe8a0fac01fa7e5d46498e4d809d0741ec5352c54cbf698ccf183a3ea80c6
7
+ data.tar.gz: fd5644fed4b9bee21a9d2e4fdeeda4179546c2168436580e56d2bea828f124f5c46bd96a5813d10ba17fcaea7e2644ddb56e204a067a945570542cd0e6e6f6da
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1 @@
1
+ ruby-2.1.9
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in youtube-embed.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 Ildar Shaynurov
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.
@@ -0,0 +1,57 @@
1
+ # YoutubeEmbed
2
+
3
+ This library allows to parse given YouTube video url and generate html code for embed video on your page
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'youtube-embed'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install youtube-embed
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require `youtube_embed`
25
+
26
+ video = YoutubeEmbed::Video.new('https://www.youtube.com/watch?v=XD_e7T5WCqw') # You may use short url too like 'https://youtu.be/XD_e7T5WCqw'
27
+ video.iframe
28
+ ```
29
+ or shorter
30
+ ```ruby
31
+ require `youtube_embed`
32
+
33
+ YoutubeEmbed::Video.iframe('https://www.youtube.com/watch?v=XD_e7T5WCqw')
34
+ ```
35
+
36
+ ### Options
37
+
38
+ You also may pass some options
39
+ ```ruby
40
+ YoutubeEmbed::Video.new('https://www.youtube.com/watch?v=XD_e7T5WCqw', { show_similar: true })
41
+ ```
42
+ Next options are available:
43
+
44
+ - `show_similar` - show similar video when video is finished. Default `false`.
45
+ - `show_title` - show video title. Default `true`.
46
+ - `show_controls` - show video controls. Default `true`.
47
+ - `allow_fullscreen` - allow user to switch video in fullscreen model. Default `true`.
48
+ - `width` - wdith of iframe. Default `640`.
49
+ - `height` - height of iframe. Default `360`.
50
+
51
+ ## Contributing
52
+
53
+ 1. Fork it ( https://github.com/shir/youtube-embed/fork )
54
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
55
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
56
+ 4. Push to the branch (`git push origin my-new-feature`)
57
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
@@ -0,0 +1,2 @@
1
+ require "youtube_embed/version"
2
+ require "youtube_embed/video"
@@ -0,0 +1,3 @@
1
+ module YoutubeEmbed
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,57 @@
1
+ module YoutubeEmbed
2
+ class Video
3
+ class << self
4
+ def iframe(video_url, options = {})
5
+ new(video_url, options).iframe
6
+ end
7
+ end
8
+
9
+ def initialize(video_url, options = {})
10
+ @video_url = video_url
11
+ @options = {
12
+ allow_fullscreen: true,
13
+ show_title: true,
14
+ show_similar: false,
15
+ show_controls: true,
16
+ width: 640,
17
+ height: 360
18
+ }.merge(options.inject({}){ |o,(k,v)| o[k.to_sym] = v; o })
19
+ end
20
+
21
+ def video_id
22
+ @video_id ||=
23
+ if @video_url[/youtu\.be\/([^\?]*)/]
24
+ $1
25
+ else
26
+ @video_url[/^.*((v\/)|(embed\/)|(watch\?))\??v?=?([^\&\?]*).*/]
27
+ $5
28
+ end
29
+ end
30
+
31
+ %w[allow_fullscreen show_title show_similar show_controls].each do |opt_name|
32
+ define_method "#{opt_name}?" do
33
+ @options[opt_name.to_sym]
34
+ end
35
+ end
36
+
37
+ %w[width height].each do |opt_name|
38
+ define_method "#{opt_name}" do
39
+ @options[opt_name.to_sym]
40
+ end
41
+ end
42
+
43
+ def embed_url
44
+ params = {}.tap do |p|
45
+ p['rel'] = 0 if !show_similar?
46
+ p['showinfo'] = 0 if !show_title?
47
+ p['controls'] = 0 if !show_controls?
48
+ end
49
+
50
+ "https://www.youtube.com/embed/#{video_id}#{params.size == 0 ? '' : '?' + params.map{ |k, v| "#{k}=#{v}" }.join('&amp;')}"
51
+ end
52
+
53
+ def iframe
54
+ %(<iframe width="#{width}" height="#{height}" src="#{embed_url}" frameborder="0"#{' allowfullscreen' if allow_fullscreen?}></iframe>)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,147 @@
1
+ require 'spec_helper'
2
+
3
+ describe YoutubeEmbed::Video do
4
+ describe "#initialize" do
5
+ subject { YoutubeEmbed::Video.new('', options) }
6
+
7
+ context "if default options are applied" do
8
+ let(:options) { {} }
9
+ it { is_expected.not_to be_show_similar }
10
+ it { is_expected.to be_show_title }
11
+ it { is_expected.to be_show_controls }
12
+ it { is_expected.to be_allow_fullscreen }
13
+ it { expect(subject.width).to eq 640 }
14
+ it { expect(subject.height).to eq 360 }
15
+ end
16
+
17
+ context "if `show_similar` option is set to true" do
18
+ let(:options) { {show_similar: true} }
19
+ it { is_expected.to be_show_similar }
20
+ end
21
+
22
+ context "if `show_title` option is set to false" do
23
+ let(:options) { {show_title: false} }
24
+ it { is_expected.not_to be_show_title }
25
+ end
26
+
27
+ context "if `show_controls` option is set to false" do
28
+ let(:options) { {show_controls: false} }
29
+ it { is_expected.not_to be_show_controls }
30
+ end
31
+
32
+ context "if `allow_fullscreen` option is set to false" do
33
+ let(:options) { {allow_fullscreen: false} }
34
+ it { is_expected.not_to be_allow_fullscreen }
35
+ end
36
+
37
+ context "if `height` options is set" do
38
+ let(:options) { {height: 532} }
39
+ it { expect(subject.height).to eq 532 }
40
+ end
41
+
42
+ context "if `width` options is set" do
43
+ let(:options) { {width: 123} }
44
+ it { expect(subject.width).to eq 123 }
45
+ end
46
+ end
47
+
48
+ describe "#video_id" do
49
+ subject { YoutubeEmbed::Video.new(video_url).video_id }
50
+
51
+ context "for short url" do
52
+ let(:video_url) { 'https://youtu.be/XD_e7T5WCqw' }
53
+ it "returns right video id" do
54
+ is_expected.to eq 'XD_e7T5WCqw'
55
+ end
56
+ end
57
+
58
+ context "for long url" do
59
+ let(:video_url) { 'https://www.youtube.com/watch?v=XD_e7T5WCqw' }
60
+ it "returns right video id" do
61
+ is_expected.to eq 'XD_e7T5WCqw'
62
+ end
63
+ end
64
+ end
65
+
66
+ describe "#embed_url" do
67
+ let(:video) { YoutubeEmbed::Video.new('https://www.youtube.com/watch?v=XD_e7T5WCqw') }
68
+ subject { video.embed_url }
69
+
70
+ context "if `show_similar` option is true" do
71
+ before{ allow(video).to receive(:show_title?).and_return(true) }
72
+ before{ allow(video).to receive(:show_similar?).and_return(true) }
73
+ it "doesn't add `rel` parameter" do
74
+ is_expected.to eq 'https://www.youtube.com/embed/XD_e7T5WCqw'
75
+ end
76
+ end
77
+ context "if `show_similar` option is false" do
78
+ before{ allow(video).to receive(:show_title?).and_return(true) }
79
+ before{ allow(video).to receive(:show_similar?).and_return(false) }
80
+ it "adds `rel` get parameter set to 0" do
81
+ is_expected.to eq 'https://www.youtube.com/embed/XD_e7T5WCqw?rel=0'
82
+ end
83
+ end
84
+ context "if `show_title` option is true" do
85
+ before{ allow(video).to receive(:show_similar?).and_return(true) }
86
+ before{ allow(video).to receive(:show_title?).and_return(true) }
87
+ it "doesn't add `showinfo` parameter" do
88
+ is_expected.to eq 'https://www.youtube.com/embed/XD_e7T5WCqw'
89
+ end
90
+ end
91
+ context "if `show_title` option is false" do
92
+ before{ allow(video).to receive(:show_similar?).and_return(true) }
93
+ before{ allow(video).to receive(:show_title?).and_return(false) }
94
+ it "adds `showinfo` get parameter set to 0" do
95
+ is_expected.to eq 'https://www.youtube.com/embed/XD_e7T5WCqw?showinfo=0'
96
+ end
97
+ end
98
+ context "if `show_controls` option is true" do
99
+ before{ allow(video).to receive(:show_similar?).and_return(true) }
100
+ before{ allow(video).to receive(:show_controls?).and_return(true) }
101
+ it "doesn't add `controls` parameter" do
102
+ is_expected.to eq 'https://www.youtube.com/embed/XD_e7T5WCqw'
103
+ end
104
+ end
105
+ context "if `show_controls` option is false" do
106
+ before{ allow(video).to receive(:show_similar?).and_return(true) }
107
+ before{ allow(video).to receive(:show_controls?).and_return(false) }
108
+ it "adds `controls` get parameter set to 0" do
109
+ is_expected.to eq 'https://www.youtube.com/embed/XD_e7T5WCqw?controls=0'
110
+ end
111
+ end
112
+ context "if both `show_title` and `show_similar` and `show_controls` options are false" do
113
+ before{ allow(video).to receive(:show_similar?).and_return(false) }
114
+ before{ allow(video).to receive(:show_title?).and_return(false) }
115
+ before{ allow(video).to receive(:show_controls?).and_return(false) }
116
+ it "adds `showinfo` and `rel` get parameters set to 0" do
117
+ is_expected.to eq 'https://www.youtube.com/embed/XD_e7T5WCqw?rel=0&amp;showinfo=0&amp;controls=0'
118
+ end
119
+ end
120
+ end
121
+
122
+ describe "#iframe" do
123
+ let(:video) { YoutubeEmbed::Video.new('https://www.youtube.com/watch?v=XD_e7T5WCqw') }
124
+ subject { video.iframe }
125
+
126
+ before{ allow(video).to receive(:embed_url).and_return('https://embed') }
127
+
128
+ it "generates html code for iframe" do
129
+ is_expected.to eq %(<iframe width="640" height="360" src="https://embed" frameborder="0" allowfullscreen></iframe>)
130
+ end
131
+
132
+ context 'if `allow_fullscreen` options is false' do
133
+ before{ allow(video).to receive(:allow_fullscreen?).and_return(false) }
134
+ it "generates html code for iframe with disabled fullscreen" do
135
+ is_expected.to eq %(<iframe width="640" height="360" src="https://embed" frameborder="0"></iframe>)
136
+ end
137
+ end
138
+
139
+ context 'if `height` and `width` options are set' do
140
+ before{ allow(video).to receive(:height).and_return(567) }
141
+ before{ allow(video).to receive(:width).and_return(981) }
142
+ it "generates html code for iframe with given width and height" do
143
+ is_expected.to eq %(<iframe width="981" height="567" src="https://embed" frameborder="0" allowfullscreen></iframe>)
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe YoutubeEmbed do
4
+ it 'has a version number' do
5
+ expect(YoutubeEmbed::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'youtube_embed'
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'youtube_embed/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "youtube-embed"
8
+ spec.version = YoutubeEmbed::VERSION
9
+ spec.authors = ["Ildar Shaynurov"]
10
+ spec.email = ["shaynurov@gmail.com"]
11
+ spec.summary = %q{Parse youtube video url and generate iframe html}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec", '~> 3.0'
23
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: youtube-embed
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ildar Shaynurov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - shaynurov@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".ruby-version"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/youtube_embed.rb
70
+ - lib/youtube_embed/version.rb
71
+ - lib/youtube_embed/video.rb
72
+ - spec/lib/youtube_embed/video_spec.rb
73
+ - spec/lib/youtube_embed_spec.rb
74
+ - spec/spec_helper.rb
75
+ - youtube-embed.gemspec
76
+ homepage: ''
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.8
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Parse youtube video url and generate iframe html
100
+ test_files:
101
+ - spec/lib/youtube_embed/video_spec.rb
102
+ - spec/lib/youtube_embed_spec.rb
103
+ - spec/spec_helper.rb