embed 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -2
- data/embed.gemspec +1 -0
- data/lib/embed.rb +14 -13
- data/lib/embed/version.rb +1 -1
- data/lib/embed_helper.rb +54 -34
- data/lib/spec/embed_helper_spec.rb +62 -30
- data/lib/spec/embed_spec.rb +18 -16
- metadata +21 -5
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
An easy way to embed media to your Rails app. Don't worry about messy embedding HTML anymore, just use the media URL.
|
4
4
|
|
5
|
-
As of now, it only supports YouTube, Vimeo and SoundCloud.
|
5
|
+
As of now, it only supports YouTube, Vimeo, Wistia and SoundCloud.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -20,11 +20,16 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
On your view
|
23
|
+
On your view
|
24
|
+
|
24
25
|
```ruby
|
25
26
|
<%= embed("http://www.youtube.com/watch?v=fwncgZ15RVQ") %>
|
26
27
|
```
|
27
28
|
|
29
|
+
```ruby
|
30
|
+
<%= embed('http://www.youtube.com/watch?v=fwncgZ15RVQ', {:width => 540, :height => 290}) %>
|
31
|
+
```
|
32
|
+
|
28
33
|
## Contributing
|
29
34
|
|
30
35
|
1. Fork it
|
data/embed.gemspec
CHANGED
@@ -14,6 +14,7 @@ Gem::Specification.new do |gem|
|
|
14
14
|
|
15
15
|
gem.add_development_dependency 'rspec'
|
16
16
|
gem.add_development_dependency 'rake'
|
17
|
+
gem.add_development_dependency 'fakeweb'
|
17
18
|
|
18
19
|
gem.files = `git ls-files`.split($/)
|
19
20
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
data/lib/embed.rb
CHANGED
@@ -2,20 +2,21 @@ require "embed/version"
|
|
2
2
|
require 'embed_helper'
|
3
3
|
|
4
4
|
module Embed
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
5
|
+
def self.youtube_video_id(url)
|
6
|
+
if url[/^.*((v\/)|(embed\/)|(watch\?))\??v?=?([^\&\?]*).*/]
|
7
|
+
return $5
|
8
|
+
elsif url[/youtu\.be\/([^\?]*)/]
|
9
|
+
return $1
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.vimeo_video_id(url)
|
14
|
+
if url[/vimeo.com\/([^\?]*)/]
|
15
|
+
return $1
|
16
|
+
end
|
17
|
+
end
|
17
18
|
end
|
18
19
|
|
19
20
|
if defined?(ActionView::Base)
|
20
|
-
|
21
|
+
ActionView::Base.send :include, Embed::EmbedHelper
|
21
22
|
end
|
data/lib/embed/version.rb
CHANGED
data/lib/embed_helper.rb
CHANGED
@@ -5,37 +5,57 @@ require 'uri'
|
|
5
5
|
require 'cgi'
|
6
6
|
|
7
7
|
module Embed
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
end
|
8
|
+
module EmbedHelper
|
9
|
+
def _youtube_embed(url, width, height)
|
10
|
+
video_id = Embed.youtube_video_id(url)
|
11
|
+
html = %Q{<iframe id="#{video_id}" type="text/html" width="#{width}" height="#{height}" src="http://www.youtube.com/embed/#{video_id}?autoplay=0 frameborder="0"/>}
|
12
|
+
html.respond_to?(:html_safe) ? html.html_safe : html
|
13
|
+
end
|
14
|
+
|
15
|
+
def _vimeo_embed(url, width, height)
|
16
|
+
video_id = Embed.vimeo_video_id(url)
|
17
|
+
html = %Q{<iframe src="http://player.vimeo.com/video/#{video_id}" width="#{width}" height="#{height}" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>}
|
18
|
+
html.respond_to?(:html_safe) ? html.html_safe : html
|
19
|
+
end
|
20
|
+
|
21
|
+
def _soundcloud_embed(url)
|
22
|
+
params = {:format => 'json', :url => url}
|
23
|
+
_oembed('http://soundcloud.com/oembed', url, params)
|
24
|
+
end
|
25
|
+
|
26
|
+
def _wistia_embed(url, width, height)
|
27
|
+
params = { :format => 'json', :url => url, :width => width, :height => height }
|
28
|
+
_oembed('http://fast.wistia.com/oembed', url, params)
|
29
|
+
end
|
30
|
+
|
31
|
+
def _oembed(oembed_url, url, params)
|
32
|
+
uri = URI(oembed_url)
|
33
|
+
uri.query = params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&')
|
34
|
+
response = Net::HTTP.get(uri)
|
35
|
+
if response
|
36
|
+
begin
|
37
|
+
html = JSON.parse(response)["html"]
|
38
|
+
html.respond_to?(:html_safe) ? html.html_safe : html
|
39
|
+
rescue JSON::ParserError
|
40
|
+
defined?(link_to) ? link_to(url) : url
|
41
|
+
end
|
42
|
+
else
|
43
|
+
defined?(link_to) ? link_to(url) : url
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def embed(url, params = {})
|
48
|
+
params[:width] ||= 640
|
49
|
+
params[:height] ||= 390
|
50
|
+
if url[/(youtube.com|youtu.be)/]
|
51
|
+
return _youtube_embed(url, params[:width], params[:height])
|
52
|
+
elsif url[/vimeo.com/]
|
53
|
+
return _vimeo_embed(url, params[:width], params[:height])
|
54
|
+
elsif url[/soundcloud.com/]
|
55
|
+
return _soundcloud_embed(url)
|
56
|
+
elsif url[/wistia.com/]
|
57
|
+
return _wistia_embed(url, params[:width], params[:height])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -5,33 +5,65 @@ require 'rspec'
|
|
5
5
|
include Embed::EmbedHelper
|
6
6
|
|
7
7
|
describe Embed::EmbedHelper do
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
8
|
+
let(:youtube_url) { 'http://www.youtube.com/watch?v=u1zgFlCw8Aw' }
|
9
|
+
let(:vimeo_url) { 'http://vimeo.com/49760839' }
|
10
|
+
let(:soundcloud_url) { 'http://soundcloud.com/forss/flickermood' }
|
11
|
+
let(:wistia_url) { 'http://fast.wistia.com/embed/iframe/2cf8fbb2c0' }
|
12
|
+
|
13
|
+
describe '::_youtube_embed(url, 640, 390)' do
|
14
|
+
it 'returns the embedding html for a YouTube URL' do
|
15
|
+
_youtube_embed(youtube_url, 640, 390).should == %Q{<iframe id="u1zgFlCw8Aw" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/u1zgFlCw8Aw?autoplay=0 frameborder="0"/>}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '::_vimeo_embed(url, 640, 390)' do
|
20
|
+
it 'returns the embedding html for a Vimeo URL' do
|
21
|
+
_vimeo_embed(vimeo_url, 640, 390).should == %Q{<iframe src="http://player.vimeo.com/video/49760839" width="640" height="390" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '::_soundcloud_embed(url)' do
|
26
|
+
it 'returns the embedding html for a SoundCloud URL' do
|
27
|
+
_soundcloud_embed(soundcloud_url).should == %Q{<iframe width=\"100%\" height=\"166\" scrolling=\"no\" frameborder=\"no\" src=\"http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F293&show_artwork=true\"></iframe>}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '::_wistia_embed(url, 640, 390)' do
|
32
|
+
it 'returns the embedding html for a Wisita URL' do
|
33
|
+
_wistia_embed(wistia_url, 640, 390).should == %Q{<iframe src="http://fast.wistia.com/embed/iframe/2cf8fbb2c0?version=v1&videoHeight=360&videoWidth=640" allowtransparency="true" frameborder="0" scrolling="no" class="wistia_embed" name="wistia_embed" width="640" height="390"></iframe>}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '::embed(url)' do
|
38
|
+
it 'embeds an YouTube video' do
|
39
|
+
embed(youtube_url).should == %Q{<iframe id="u1zgFlCw8Aw" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/u1zgFlCw8Aw?autoplay=0 frameborder="0"/>}
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'embeds a Vimeo video' do
|
43
|
+
embed(vimeo_url).should == %Q{<iframe src="http://player.vimeo.com/video/49760839" width="640" height="390" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>}
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'embeds a SoundCloud audio' do
|
47
|
+
embed(soundcloud_url).should == %Q{<iframe width=\"100%\" height=\"166\" scrolling=\"no\" frameborder=\"no\" src=\"http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F293&show_artwork=true\"></iframe>}
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '::embed(url, 540, 290)' do
|
52
|
+
it 'embeds an YouTube video with custom sizes' do
|
53
|
+
embed(youtube_url, {:width => 540, :height => 290}).should == %Q{<iframe id="u1zgFlCw8Aw" type="text/html" width="540" height="290" src="http://www.youtube.com/embed/u1zgFlCw8Aw?autoplay=0 frameborder="0"/>}
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'embeds a Vimeo video with custom sizes' do
|
57
|
+
embed(vimeo_url, {:width => 540, :height => 290}).should == %Q{<iframe src="http://player.vimeo.com/video/49760839" width="540" height="290" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>}
|
58
|
+
end
|
59
|
+
|
60
|
+
#FIXME: Wistia always return videoHeight=360&videoWidth=640 ??
|
61
|
+
it 'embeds a Wistia video with custom sizes' do
|
62
|
+
embed(wistia_url, {:width => 540, :height => 290}).should == %Q{<iframe src="http://fast.wistia.com/embed/iframe/2cf8fbb2c0?version=v1&videoHeight=360&videoWidth=640" allowtransparency="true" frameborder="0" scrolling="no" class="wistia_embed" name="wistia_embed" width="540" height="290"></iframe>}
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'embeds a SoundCloud audio' do
|
66
|
+
embed(soundcloud_url).should == %Q{<iframe width=\"100%\" height=\"166\" scrolling=\"no\" frameborder=\"no\" src=\"http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F293&show_artwork=true\"></iframe>}
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/spec/embed_spec.rb
CHANGED
@@ -5,20 +5,22 @@ require 'rspec'
|
|
5
5
|
include Embed
|
6
6
|
|
7
7
|
describe Embed do
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
8
|
+
describe '::youtube_video_id(url)' do
|
9
|
+
it 'returns the YouTube video id from a full URL' do
|
10
|
+
url = 'http://www.youtube.com/watch?v=dMH0bHeiRNg'
|
11
|
+
Embed.youtube_video_id(url).should == 'dMH0bHeiRNg'
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'returns the YouTube video id from a short URL' do
|
15
|
+
url = 'http://youtu.be/dMH0bHeiRNg'
|
16
|
+
Embed.youtube_video_id(url).should == 'dMH0bHeiRNg'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '::vimeo_video_id(url)' do
|
21
|
+
it 'returns the Vimeo video id from its URL' do
|
22
|
+
url = 'http://vimeo.com/49760839'
|
23
|
+
Embed.vimeo_video_id(url).should == '49760839'
|
24
|
+
end
|
25
|
+
end
|
24
26
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: embed
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -43,6 +43,22 @@ dependencies:
|
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: fakeweb
|
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'
|
46
62
|
description: An easy way to embed media to your Rails app
|
47
63
|
email:
|
48
64
|
- me@neil.pro
|
@@ -75,7 +91,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
75
91
|
version: '0'
|
76
92
|
segments:
|
77
93
|
- 0
|
78
|
-
hash:
|
94
|
+
hash: 2927421669210399932
|
79
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
96
|
none: false
|
81
97
|
requirements:
|
@@ -84,10 +100,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
100
|
version: '0'
|
85
101
|
segments:
|
86
102
|
- 0
|
87
|
-
hash:
|
103
|
+
hash: 2927421669210399932
|
88
104
|
requirements: []
|
89
105
|
rubyforge_project:
|
90
|
-
rubygems_version: 1.8.
|
106
|
+
rubygems_version: 1.8.23
|
91
107
|
signing_key:
|
92
108
|
specification_version: 3
|
93
109
|
summary: An easy way to embed media to your Rails app
|