youtube_embed 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/.rspec +1 -0
- data/.travis.yml +2 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +2 -0
- data/README.md +22 -7
- data/Rakefile +5 -0
- data/lib/youtube_embed/engine.rb +7 -0
- data/lib/youtube_embed/model_additions.rb +10 -0
- data/lib/youtube_embed/railtie.rb +9 -0
- data/lib/youtube_embed/version.rb +1 -1
- data/lib/youtube_embed/video_details.rb +62 -0
- data/lib/youtube_embed/video_object.rb +22 -0
- data/lib/youtube_embed.rb +83 -3
- data/spec/spec_helper.rb +1 -0
- data/spec/youtube_embed/model_additions_spec.rb +0 -0
- data/spec/youtube_embed_spec.rb +5 -0
- data/vendor/assets/javascripts/youtube_embed.js +6 -0
- data/vendor/assets/stylesheets/youtube_embed.css +24 -0
- data/youtube_embed.gemspec +6 -3
- metadata +71 -12
- data/.idea/encodings.xml +0 -5
- data/.idea/misc.xml +0 -5
- data/.idea/modules.xml +0 -9
- data/.idea/scopes/scope_settings.xml +0 -5
- data/.idea/vcs.xml +0 -7
- data/.idea/youtube_embed.iml +0 -19
data/.gitignore
CHANGED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# YoutubeEmbed
|
2
2
|
|
3
|
-
|
3
|
+
Embed youtube videos in on site saving URL and get relevant output in view
|
4
|
+
|
5
|
+
1. Simple Embed
|
6
|
+
|
7
|
+
2. Embed with title, description & thumbnail
|
4
8
|
|
5
9
|
## Installation
|
6
10
|
|
@@ -18,12 +22,23 @@ Or install it yourself as:
|
|
18
22
|
|
19
23
|
## Usage
|
20
24
|
|
21
|
-
|
25
|
+
It can be used in model where we can specify simple iframe embed or embedding with thumbnail & description
|
26
|
+
|
27
|
+
Simple:
|
28
|
+
|
29
|
+
youtube_embed :field_name, {:with_description => false, :width => 450, :height => 300}
|
30
|
+
|
31
|
+
With Thumbnail & Description:
|
32
|
+
|
33
|
+
youtube_embed :field_name, {:with_description => false, :width => 450, :height => 300}
|
34
|
+
|
35
|
+
|
36
|
+
This will modify the youtube link into html required to embed, If you don't want to modify html or simple change the view you can call method in view like this:
|
37
|
+
|
38
|
+
<?= YoutubeEmbed::youtube_embed(attr_here) ?>
|
39
|
+
|
40
|
+
Working on further improvements
|
22
41
|
|
23
42
|
## Contributing
|
24
43
|
|
25
|
-
|
26
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
-
5. Create new Pull Request
|
44
|
+
youtube_embed
|
data/Rakefile
CHANGED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
module YoutubeEmbed
|
3
|
+
class VideoDetails < VideoObject
|
4
|
+
include HTTParty
|
5
|
+
format :xml
|
6
|
+
|
7
|
+
VALID_METHODS = [:comments, :id, :published, :updated, :category, :title, :content, :link,
|
8
|
+
:where, {:group => [:category, :content, :description, :keywords,
|
9
|
+
:player, :thumbnail, :title]}]
|
10
|
+
|
11
|
+
def initialize video_details
|
12
|
+
@dynamic_methods = {}
|
13
|
+
@id = video_details.respond_to?(:keys) ? video_details["id"].split("/").last : video_details
|
14
|
+
@entry = (video_details if video_details.respond_to?(:keys))
|
15
|
+
end
|
16
|
+
|
17
|
+
def id
|
18
|
+
@id
|
19
|
+
end
|
20
|
+
|
21
|
+
def dynamic_methods
|
22
|
+
h={};@dynamic_methods.each {|k,v| h[v.class].nil? ? h[v.class]=[k] : h[v.class] << k}
|
23
|
+
return h
|
24
|
+
end
|
25
|
+
|
26
|
+
def entry
|
27
|
+
@entry ||= self.class.get_youtube(@id)["entry"]
|
28
|
+
if @dynamic_methods.blank?
|
29
|
+
VALID_METHODS.each {|key| recursive_hash_access @dynamic_methods, @entry, key}
|
30
|
+
define_links
|
31
|
+
end
|
32
|
+
return @entry
|
33
|
+
rescue MultiXml::ParseError
|
34
|
+
raise InvalidVideoDetails
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def self.get_youtube top_route, nested_route=nil
|
40
|
+
base_uri "https://gdata.youtube.com"
|
41
|
+
data = get "/feeds/api/videos/#{top_route}"
|
42
|
+
end
|
43
|
+
|
44
|
+
def method_missing method
|
45
|
+
(entry;@dynamic_methods[method]) if @dynamic_methods.blank?
|
46
|
+
|
47
|
+
return self.class.get_youtube(@dynamic_methods[method].split("videos/").last)["feed"]["entry"] if [:raw_video_details_responses,:raw_related_videos].include? method
|
48
|
+
return send("raw_#{method}").map{|v| YoutubeEmbed::VideoDetails.new(v)} if [:video_details_responses,:related_videos].include? method
|
49
|
+
@dynamic_methods[method] || super
|
50
|
+
end
|
51
|
+
|
52
|
+
def define_links
|
53
|
+
(links = [:direct_link,:video_details_responses,:related_videos,:mobile_link,:api_link]).each do |key|
|
54
|
+
single_count = "#{key.to_s.singularize}_count".to_sym
|
55
|
+
current_link = link[links.index(key)]["href"] if link.count>links.index(key)
|
56
|
+
@dynamic_methods[key] = current_link
|
57
|
+
@dynamic_methods["raw_#{key}".to_sym] = current_link
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module YoutubeEmbed
|
2
|
+
class VideoObject
|
3
|
+
def recursive_hash_access dynamic_methods, base, key
|
4
|
+
if key.respond_to? :keys
|
5
|
+
key.each do |k,v|
|
6
|
+
recursive_hash_access(dynamic_methods, base, k)
|
7
|
+
v=[v] unless v.kind_of?(Array)
|
8
|
+
v.each do |v1|
|
9
|
+
recursive_hash_access(dynamic_methods, base[k.to_s.camelcase(:lower)], v1)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
else
|
13
|
+
value = base[key.to_s.camelcase(:lower)]
|
14
|
+
begin
|
15
|
+
value = value.number? ? value.to_i : Date.parse(value)
|
16
|
+
rescue
|
17
|
+
end
|
18
|
+
dynamic_methods[key] = value
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/youtube_embed.rb
CHANGED
@@ -1,7 +1,87 @@
|
|
1
1
|
require "youtube_embed/version"
|
2
|
-
|
2
|
+
require "youtube_embed/model_additions"
|
3
|
+
require "youtube_embed/video_object"
|
4
|
+
require "youtube_embed/video_details"
|
5
|
+
require "youtube_embed/railtie" if defined? Rails
|
6
|
+
require "youtube_embed/engine"
|
7
|
+
require 'httparty'
|
3
8
|
module YoutubeEmbed
|
4
|
-
|
5
|
-
|
9
|
+
|
10
|
+
VIDEO_FORMATS = [
|
11
|
+
%r(https?://www\.youtube\.com/embed/(.*?)(\?|$)),
|
12
|
+
%r(https?://youtu\.be/(.+)),
|
13
|
+
%r(https?://www\.youtube\.com/user/.*?#\w/\w/\w/\w/(.+)\b),
|
14
|
+
%r(https?://www\.youtube\.com/v/(.*?)(#|\?|$)),
|
15
|
+
%r(https?://www\.youtube\.com/watch\?v=(.*?)(&|#|$))
|
16
|
+
]
|
17
|
+
def self.get_video_id(url)
|
18
|
+
url.strip!
|
19
|
+
VIDEO_FORMATS.find { |video_format| url =~ video_format } and $1
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.youtube_embed(data, options = {:with_description => true, :height => 200, :width => 300})
|
23
|
+
data = data.gsub(/<a?[^<]+ href="[(https?:\/\/)?(www\.)?youtube.com[^<]+]+">([^<]+)<\/a>/i, '\1')
|
24
|
+
if options[:with_description]
|
25
|
+
data = data.gsub(/https?:\/\/?(?:www\.)?youtube\.com(?:\/v\/|\/watch\?v=)([A-Za-z0-9_-]{11})/, thumbnail_and_description("#{$1}", options[:width], options[:height]))
|
26
|
+
else
|
27
|
+
data = data.gsub(/https?:\/\/?(?:www\.)?youtube\.com(?:\/v\/|\/watch\?v=)([A-Za-z0-9_-]{11})/, simple("#{$1}", options[:width], options[:height]))
|
28
|
+
end
|
29
|
+
return data
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.thumbnail_and_description(video_url, width, height)
|
33
|
+
begin
|
34
|
+
if video_url.to_s != ' '
|
35
|
+
video_id = get_video_id(video_url)
|
36
|
+
if video_id.present?
|
37
|
+
video_details = YoutubeEmbed::VideoDetails.new(video_id)
|
38
|
+
thumbnails = video_details.thumbnail
|
39
|
+
return %Q{<div class="youtube_embed_video">
|
40
|
+
<div class="youtube_embed_partial_video">
|
41
|
+
<div class="youtube_embed_thumbnail">
|
42
|
+
<img src="#{thumbnails[1]["url"]}" />
|
43
|
+
</div>
|
44
|
+
<div class="youtube_embed_details">
|
45
|
+
<div class="youtube_embed_title">
|
46
|
+
<strong>
|
47
|
+
#{video_details.title["__content__"]}
|
48
|
+
</strong>
|
49
|
+
</div>
|
50
|
+
<div class="youtube_embed_description">
|
51
|
+
#{video_details.description["__content__"].truncate(185)}
|
52
|
+
</div>
|
53
|
+
</div>
|
54
|
+
</div>
|
55
|
+
<div class="youtube_embed_main_video" style="display:none;">
|
56
|
+
<iframe title="YouTube player" width="#{ width }" height="#{ height }" src="http://www.youtube.com/embed/#{ video_id }" frameborder="0" allowfullscreen></iframe>
|
57
|
+
</div>
|
58
|
+
</div>}
|
59
|
+
else
|
60
|
+
return video_url
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
rescue Exception => e
|
65
|
+
Rails.logger.debug e.message
|
66
|
+
end
|
6
67
|
end
|
68
|
+
|
69
|
+
def self.simple(video_url, width, height)
|
70
|
+
begin
|
71
|
+
if video_url.to_s != ' '
|
72
|
+
video_id = get_video_id(video_url)
|
73
|
+
if video_id.present?
|
74
|
+
return %Q{<div class="youtube_embed_video"><iframe title="YouTube player" width="#{ width }" height="#{ height }" src="http://www.youtube.com/embed/#{ video_id }" frameborder="0" allowfullscreen></iframe></div>}
|
75
|
+
else
|
76
|
+
return video_url
|
77
|
+
end
|
78
|
+
end
|
79
|
+
rescue Exception => e
|
80
|
+
Rails.logger.debug e.message
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
7
84
|
end
|
85
|
+
|
86
|
+
|
87
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'youtube_embed'
|
File without changes
|
@@ -0,0 +1,6 @@
|
|
1
|
+
$('.youtube_embed_partial_video').live('click', function() {
|
2
|
+
$(this).hide();
|
3
|
+
var parent = $(this).parent();
|
4
|
+
$(".youtube_embed_main_video iframe", parent).attr('src', $(".youtube_embed_main_video iframe", parent).attr('src') + '?autoplay=1');
|
5
|
+
$(".youtube_embed_main_video", parent).show();
|
6
|
+
});
|
@@ -0,0 +1,24 @@
|
|
1
|
+
.youtube_embed_video {
|
2
|
+
float:left;
|
3
|
+
width:100%;
|
4
|
+
}
|
5
|
+
.youtube_embed_partial_video {
|
6
|
+
cursor:pointer;
|
7
|
+
}
|
8
|
+
.youtube_embed_thumbnail {
|
9
|
+
float:left;
|
10
|
+
width:120px;
|
11
|
+
padding-right:10px;
|
12
|
+
}
|
13
|
+
.youtube_embed_details {
|
14
|
+
float:left;
|
15
|
+
width:300px;
|
16
|
+
}
|
17
|
+
.youtube_embed_details .youtube_embed_title {
|
18
|
+
float:left;
|
19
|
+
width:100%;
|
20
|
+
}
|
21
|
+
.youtube_embed_details .youtube_embed_description {
|
22
|
+
float:left;
|
23
|
+
width:100%;
|
24
|
+
}
|
data/youtube_embed.gemspec
CHANGED
@@ -8,12 +8,15 @@ Gem::Specification.new do |gem|
|
|
8
8
|
gem.version = YoutubeEmbed::VERSION
|
9
9
|
gem.authors = ["Mohit Sharma"]
|
10
10
|
gem.email = ["developer.pht@gmail.com"]
|
11
|
-
gem.description = %q{Embed youtube videos
|
12
|
-
gem.summary = %q{Embed youtube videos
|
11
|
+
gem.description = %q{Embed youtube videos by saving url}
|
12
|
+
gem.summary = %q{Embed youtube videos saving url and show simple iframe embedded or embedded like facebook with thumnail, title and description }
|
13
13
|
gem.homepage = ""
|
14
14
|
|
15
|
-
gem.files = `git ls-files`.split(
|
15
|
+
gem.files = `git ls-files`.split($\)
|
16
16
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
|
+
gem.add_runtime_dependency "httparty"
|
20
|
+
gem.add_development_dependency "rake"
|
21
|
+
gem.add_development_dependency "rspec"
|
19
22
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: youtube_embed
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,9 +9,57 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
13
|
-
dependencies:
|
14
|
-
|
12
|
+
date: 2013-05-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: httparty
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Embed youtube videos by saving url
|
15
63
|
email:
|
16
64
|
- developer.pht@gmail.com
|
17
65
|
executables: []
|
@@ -19,18 +67,25 @@ extensions: []
|
|
19
67
|
extra_rdoc_files: []
|
20
68
|
files:
|
21
69
|
- .gitignore
|
22
|
-
- .
|
23
|
-
- .
|
24
|
-
- .
|
25
|
-
- .idea/scopes/scope_settings.xml
|
26
|
-
- .idea/vcs.xml
|
27
|
-
- .idea/youtube_embed.iml
|
70
|
+
- .rspec
|
71
|
+
- .travis.yml
|
72
|
+
- CHANGELOG.md
|
28
73
|
- Gemfile
|
29
74
|
- LICENSE.txt
|
30
75
|
- README.md
|
31
76
|
- Rakefile
|
32
77
|
- lib/youtube_embed.rb
|
78
|
+
- lib/youtube_embed/engine.rb
|
79
|
+
- lib/youtube_embed/model_additions.rb
|
80
|
+
- lib/youtube_embed/railtie.rb
|
33
81
|
- lib/youtube_embed/version.rb
|
82
|
+
- lib/youtube_embed/video_details.rb
|
83
|
+
- lib/youtube_embed/video_object.rb
|
84
|
+
- spec/spec_helper.rb
|
85
|
+
- spec/youtube_embed/model_additions_spec.rb
|
86
|
+
- spec/youtube_embed_spec.rb
|
87
|
+
- vendor/assets/javascripts/youtube_embed.js
|
88
|
+
- vendor/assets/stylesheets/youtube_embed.css
|
34
89
|
- youtube_embed.gemspec
|
35
90
|
homepage: ''
|
36
91
|
licenses: []
|
@@ -55,5 +110,9 @@ rubyforge_project:
|
|
55
110
|
rubygems_version: 1.8.24
|
56
111
|
signing_key:
|
57
112
|
specification_version: 3
|
58
|
-
summary: Embed youtube videos
|
59
|
-
|
113
|
+
summary: Embed youtube videos saving url and show simple iframe embedded or embedded
|
114
|
+
like facebook with thumnail, title and description
|
115
|
+
test_files:
|
116
|
+
- spec/spec_helper.rb
|
117
|
+
- spec/youtube_embed/model_additions_spec.rb
|
118
|
+
- spec/youtube_embed_spec.rb
|
data/.idea/encodings.xml
DELETED
data/.idea/misc.xml
DELETED
data/.idea/modules.xml
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
-
<project version="4">
|
3
|
-
<component name="ProjectModuleManager">
|
4
|
-
<modules>
|
5
|
-
<module fileurl="file://$PROJECT_DIR$/.idea/youtube_embed.iml" filepath="$PROJECT_DIR$/.idea/youtube_embed.iml" />
|
6
|
-
</modules>
|
7
|
-
</component>
|
8
|
-
</project>
|
9
|
-
|
data/.idea/vcs.xml
DELETED
data/.idea/youtube_embed.iml
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
-
<module type="RUBY_MODULE" version="4">
|
3
|
-
<component name="FacetManager">
|
4
|
-
<facet type="gem" name="Gem">
|
5
|
-
<configuration>
|
6
|
-
<option name="GEM_APP_ROOT_PATH" value="$MODULE_DIR$" />
|
7
|
-
<option name="GEM_APP_TEST_PATH" value="" />
|
8
|
-
<option name="GEM_APP_LIB_PATH" value="$MODULE_DIR$/lib" />
|
9
|
-
</configuration>
|
10
|
-
</facet>
|
11
|
-
</component>
|
12
|
-
<component name="NewModuleRootManager">
|
13
|
-
<content url="file://$MODULE_DIR$" />
|
14
|
-
<orderEntry type="inheritedJdk" />
|
15
|
-
<orderEntry type="sourceFolder" forTests="false" />
|
16
|
-
<orderEntry type="library" scope="PROVIDED" name="bundler (v1.2.1, RVM: ruby-1.9.3-p327) [gem]" level="application" />
|
17
|
-
</component>
|
18
|
-
</module>
|
19
|
-
|