embeddable 0.0.2 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE.md +22 -0
- data/README.md +7 -2
- data/lib/embeddable.rb +56 -2
- data/lib/embeddable/version.rb +1 -1
- data/spec/lib/embeddable_spec.rb +204 -0
- metadata +18 -22
- data/.rspec +0 -2
- data/LICENSE.txt +0 -22
- data/lib/embeddable/active_record.rb +0 -1
- data/lib/embeddable/concerns.rb +0 -3
- data/lib/embeddable/concerns/embeddable.rb +0 -49
- data/spec/lib/embeddable/concerns/embeddable_spec.rb +0 -121
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2e6a010ead8a00d7bfa9abe89edb88c156edfe44
|
4
|
+
data.tar.gz: 431d433dc5289ac8ad37726d085e7aa4ec1f99b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 848c0f5fc61d403109439fa58c3132ed3bf979d02a01b6fc29036c58e1da52a8fe92afe4ee8f71c45dfbf091eb873c79e6efa22b6cc7a4de7a12550b75fe311e
|
7
|
+
data.tar.gz: c89fe8f834ad7aa5db1bd3b5f4d5c1cf62a8b64a4b799e334464b41c707ad4117f4ef8069e046c108e43b4d54188818733a456f013c633216862f8eff86a6036
|
data/MIT-LICENSE.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Licensed under the **MIT** license
|
2
|
+
|
3
|
+
> Copyright (c) 2014 Hyper Interaktiv AS
|
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 NONINFRINGEMENT.
|
19
|
+
> IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
+
> CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
+
> TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
+
> SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
# Embeddable
|
2
2
|
|
3
|
-
[![
|
4
|
-
[![Build Status](https://
|
3
|
+
[![Gem Version](https://img.shields.io/gem/v/embeddable.svg)](https://rubygems.org/gems/embeddable)
|
4
|
+
[![Build Status](https://img.shields.io/travis/hyperoslo/embeddable.svg)](https://travis-ci.org/hyperoslo/embeddable)
|
5
|
+
[![Dependency Status](https://img.shields.io/gemnasium/hyperoslo/embeddable.svg)](https://gemnasium.com/hyperoslo/embeddable)
|
6
|
+
[![Code Climate](https://img.shields.io/codeclimate/github/hyperoslo/embeddable.svg)](https://codeclimate.com/github/hyperoslo/embeddable)
|
7
|
+
[![Coverage Status](https://img.shields.io/coveralls/hyperoslo/embeddable.svg)](https://coveralls.io/r/hyperoslo/embeddable)
|
5
8
|
|
6
9
|
Embeddable makes it easier to embed videos.
|
7
10
|
|
@@ -24,6 +27,8 @@ Or install it yourself as:
|
|
24
27
|
```ruby
|
25
28
|
# app/models/post.rb
|
26
29
|
class Post < ActiveRecord::Base
|
30
|
+
include Embeddable
|
31
|
+
|
27
32
|
embeddable :video, from: :video_url
|
28
33
|
end
|
29
34
|
|
data/lib/embeddable.rb
CHANGED
@@ -1,6 +1,60 @@
|
|
1
|
+
require 'active_support/all'
|
1
2
|
require 'embeddable/version'
|
2
|
-
require 'embeddable/concerns'
|
3
|
-
require 'embeddable/active_record'
|
4
3
|
|
5
4
|
module Embeddable
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
SERVICES = {
|
8
|
+
youtube: [
|
9
|
+
%r{^https?://(?:(?:www|m)\.)?youtube\.com/watch\?v=([^&]+)},
|
10
|
+
%r{^https?://(?:(?:www|m)\.)?youtu\.be/([^?]+)}
|
11
|
+
],
|
12
|
+
vimeo: [
|
13
|
+
%r{^https?://(?:www\.)?vimeo\.com/([^\?]+)},
|
14
|
+
],
|
15
|
+
dailymotion: [
|
16
|
+
%r{^https?://(?:www\.)?dailymotion\.com/video/([^\?]+)},
|
17
|
+
],
|
18
|
+
veoh: [
|
19
|
+
%r{^https?://(?:www\.)?veoh\.com/watch/([^\?]+)},
|
20
|
+
],
|
21
|
+
vippy: [
|
22
|
+
%r{https:\/\/vippy.co\/play\/.+\/([^\?\s]+)"}
|
23
|
+
],
|
24
|
+
liveleak: [
|
25
|
+
%r{^https?://(?:www\.)?liveleak\.com/view\?i=([^\?]+)},
|
26
|
+
]
|
27
|
+
}
|
28
|
+
|
29
|
+
module ClassMethods
|
30
|
+
def embeddable(name, options = {})
|
31
|
+
source = options.fetch :from
|
32
|
+
|
33
|
+
define_method "#{name}_type" do
|
34
|
+
url = send(source)
|
35
|
+
return if url.blank?
|
36
|
+
|
37
|
+
SERVICES.map do |service, patterns|
|
38
|
+
service if patterns.any? { |pattern| url[pattern] }
|
39
|
+
end.compact.first
|
40
|
+
end
|
41
|
+
|
42
|
+
define_method "#{name}_id" do
|
43
|
+
url = send(source)
|
44
|
+
return if url.blank?
|
45
|
+
|
46
|
+
SERVICES.map do |service, patterns|
|
47
|
+
patterns.map { |pattern| url[pattern, 1] }
|
48
|
+
end.flatten.compact.first
|
49
|
+
end
|
50
|
+
|
51
|
+
SERVICES.each do |service, pattern|
|
52
|
+
|
53
|
+
define_method "#{name}_on_#{service}?" do
|
54
|
+
send("#{name}_type") == service
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
6
60
|
end
|
data/lib/embeddable/version.rb
CHANGED
@@ -0,0 +1,204 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Embeddable do
|
4
|
+
subject { Dummy.new }
|
5
|
+
|
6
|
+
class Dummy
|
7
|
+
include Embeddable
|
8
|
+
|
9
|
+
embeddable :video, from: :video_url
|
10
|
+
|
11
|
+
attr_accessor :video_url
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '.embeddable' do
|
15
|
+
it 'requires a source property' do
|
16
|
+
expect do
|
17
|
+
Dummy.embeddable :video
|
18
|
+
end.to raise_error KeyError
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'Liveleak' do
|
23
|
+
context 'with http://liveleak.com/...' do
|
24
|
+
before {
|
25
|
+
subject.video_url = 'http://www.liveleak.com/view?i=290_1392029603'
|
26
|
+
}
|
27
|
+
|
28
|
+
its(:video_type) { should eq :liveleak }
|
29
|
+
its(:video_id) { should eq '290_1392029603' }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe 'Dailymotion' do
|
34
|
+
context 'with http://dailymotion.com/video/<id>...' do
|
35
|
+
before {
|
36
|
+
subject.video_url = 'http://www.dailymotion.com/video/xu4q8m'
|
37
|
+
}
|
38
|
+
|
39
|
+
its(:video_type) { should eq :dailymotion }
|
40
|
+
its(:video_id) { should eq 'xu4q8m' }
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'with http://dailymotion.com/video/<id>_<blargablarga>...' do
|
44
|
+
before {
|
45
|
+
subject.video_url = 'http://www.dailymotion.com/video/xu4q8m_apprendre-le-deltaplane-a-millau-hang-gliding-in-france-creative-motion_sport'
|
46
|
+
}
|
47
|
+
|
48
|
+
its(:video_type) { should eq :dailymotion }
|
49
|
+
its(:video_id) { should eq 'xu4q8m_apprendre-le-deltaplane-a-millau-hang-gliding-in-france-creative-motion_sport' }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe 'Veoh' do
|
54
|
+
context 'with http://veoh.com/watch/<id>...' do
|
55
|
+
before {
|
56
|
+
subject.video_url = 'http://www.veoh.com/watch/v36298453QmtnSAza'
|
57
|
+
}
|
58
|
+
|
59
|
+
its(:video_type) { should eq :veoh }
|
60
|
+
its(:video_id) { should eq 'v36298453QmtnSAza' }
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'with http://veoh.com/watch/<id>/<blargablarga>...' do
|
64
|
+
before {
|
65
|
+
subject.video_url = 'http://www.veoh.com/watch/v36298453QmtnSAza/CBS-SciTech-News'
|
66
|
+
}
|
67
|
+
|
68
|
+
its(:video_type) { should eq :veoh }
|
69
|
+
its(:video_id) { should eq 'v36298453QmtnSAza/CBS-SciTech-News' }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe 'Vimeo' do
|
74
|
+
context 'with http://vimeo.com/...' do
|
75
|
+
before { subject.video_url = 'https://vimeo.com/77949044' }
|
76
|
+
|
77
|
+
its(:video_type) { should eq :vimeo }
|
78
|
+
its(:video_id) { should eq '77949044' }
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe 'Vippy' do
|
83
|
+
context 'with http://liveleak.com/...' do
|
84
|
+
before {
|
85
|
+
subject.video_url = <<SUCH_EMBED_CODE
|
86
|
+
<!-- Start Vippy video -->
|
87
|
+
<div itemscope itemtype="http://schema.org/VideoObject" class="vippy-video" style="width: 640px; height: 360px; position: relative;">
|
88
|
+
<meta itemprop="name" content="bademiljo_hurum_v1.mp4" />
|
89
|
+
<meta itemprop="duration" content="PT9M38S" />
|
90
|
+
<meta itemprop="thumbnailURL" content="https://vippy.co/play/image/pretty-cool-video" />
|
91
|
+
<meta itemprop="contentURL" content="http://cdn2.vippy.co/10455/video/out/nice-stuff.mp4" />
|
92
|
+
<meta itemprop="embedUrl" content="https://vippy.co/play/flash/watch/pretty-cool-video" />
|
93
|
+
<meta itemprop="uploadDate" content="2014-04-30" />
|
94
|
+
<meta itemprop="width" content="640" />
|
95
|
+
<meta itemprop="height" content="360" />
|
96
|
+
<object class="vippy-video-object" type="application/x-shockwave-flash" data="https://vippy.co/play/flash/watch/pretty-cool-video" id="some-id" width="640" height="360">
|
97
|
+
<param name="player" value="https://vippy.co/play/flash/watch/pretty-cool-video" />
|
98
|
+
<param name="allowScriptAccess" value="always" />
|
99
|
+
<param name="allowFullScreen" value="true" />
|
100
|
+
<param name="wmode" value="direct" />
|
101
|
+
<param name="movie" value="https://vippy.co/play/flash/watch/pretty-cool-video" />
|
102
|
+
<video style="position: absolute; top: 0px; left: 0px;" class="vippy-video-object" width="640" height="360" preload="none" controls="controls" poster="https://vippy.co/play/image/pretty-cool-video">
|
103
|
+
<source src="https://vippy.co/play/mobile/watch/pretty-cool-video" />
|
104
|
+
</video>
|
105
|
+
</object>
|
106
|
+
</div>
|
107
|
+
<!-- End Vippy video -->
|
108
|
+
SUCH_EMBED_CODE
|
109
|
+
}
|
110
|
+
|
111
|
+
its(:video_type) { should eq :vippy }
|
112
|
+
its(:video_id) { should eq 'pretty-cool-video' }
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
describe 'YouTube' do
|
117
|
+
|
118
|
+
context 'with http://youtube.com/watch?v=...' do
|
119
|
+
before { subject.video_url = 'http://youtube.com/watch?v=1&feature=foo' }
|
120
|
+
|
121
|
+
its(:video_type) { should eq :youtube }
|
122
|
+
its(:video_id) { should eq '1' }
|
123
|
+
it { should be_video_on_youtube }
|
124
|
+
end
|
125
|
+
|
126
|
+
context 'with https://youtube.com/watch?v=...' do
|
127
|
+
before { subject.video_url = 'https://youtube.com/watch?v=1&feature=foo' }
|
128
|
+
|
129
|
+
its(:video_type) { should eq :youtube }
|
130
|
+
its(:video_id) { should eq '1' }
|
131
|
+
it { should be_video_on_youtube }
|
132
|
+
end
|
133
|
+
|
134
|
+
context 'with http://www.youtube.com/watch?v=...' do
|
135
|
+
before { subject.video_url = 'http://www.youtube.com/watch?v=1&feature=foo' }
|
136
|
+
|
137
|
+
its(:video_type) { should eq :youtube }
|
138
|
+
its(:video_id) { should eq '1' }
|
139
|
+
it { should be_video_on_youtube }
|
140
|
+
end
|
141
|
+
|
142
|
+
context 'with http://m.youtube.com/watch?v=...' do
|
143
|
+
before { subject.video_url = 'http://m.youtube.com/watch?v=1&feature=foo' }
|
144
|
+
|
145
|
+
its(:video_type) { should eq :youtube }
|
146
|
+
its(:video_id) { should eq '1' }
|
147
|
+
it { should be_video_on_youtube }
|
148
|
+
end
|
149
|
+
|
150
|
+
context 'with http://youtu.be/...' do
|
151
|
+
before { subject.video_url = 'http://youtu.be/1' }
|
152
|
+
|
153
|
+
its(:video_type) { should eq :youtube }
|
154
|
+
its(:video_id) { should eq '1' }
|
155
|
+
it { should be_video_on_youtube }
|
156
|
+
end
|
157
|
+
|
158
|
+
context 'with https://youtu.be/...' do
|
159
|
+
before { subject.video_url = 'https://youtu.be/1' }
|
160
|
+
|
161
|
+
its(:video_type) { should eq :youtube }
|
162
|
+
its(:video_id) { should eq '1' }
|
163
|
+
it { should be_video_on_youtube }
|
164
|
+
end
|
165
|
+
|
166
|
+
context 'with http://www.youtu.be/...' do
|
167
|
+
before { subject.video_url = 'http://www.youtu.be/1' }
|
168
|
+
|
169
|
+
its(:video_type) { should eq :youtube }
|
170
|
+
its(:video_id) { should eq '1' }
|
171
|
+
it { should be_video_on_youtube }
|
172
|
+
end
|
173
|
+
|
174
|
+
context 'with http://m.youtu.be/...' do
|
175
|
+
before { subject.video_url = 'http://m.youtu.be/1' }
|
176
|
+
|
177
|
+
its(:video_type) { should eq :youtube }
|
178
|
+
its(:video_id) { should eq '1' }
|
179
|
+
it { should be_video_on_youtube }
|
180
|
+
end
|
181
|
+
|
182
|
+
end
|
183
|
+
|
184
|
+
describe 'unsupported scenarios' do
|
185
|
+
|
186
|
+
context 'unknown service' do
|
187
|
+
before { subject.video_url = 'http://foobar.com' }
|
188
|
+
|
189
|
+
its(:video_type) { should be_nil }
|
190
|
+
its(:video_id) { should be_nil }
|
191
|
+
it { should_not be_video_on_youtube }
|
192
|
+
end
|
193
|
+
|
194
|
+
context 'empty property value' do
|
195
|
+
before { subject.video_url = nil }
|
196
|
+
|
197
|
+
its(:video_type) { should be_nil }
|
198
|
+
its(:video_id) { should be_nil }
|
199
|
+
it { should_not be_video_on_youtube }
|
200
|
+
end
|
201
|
+
|
202
|
+
end
|
203
|
+
|
204
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: embeddable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Johannes Gorset
|
@@ -9,62 +9,62 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-05-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- - ~>
|
18
|
+
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
20
|
version: '4.0'
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- - ~>
|
25
|
+
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '4.0'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: bundler
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
|
-
- - ~>
|
32
|
+
- - "~>"
|
33
33
|
- !ruby/object:Gem::Version
|
34
34
|
version: '1.3'
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
|
-
- - ~>
|
39
|
+
- - "~>"
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: '1.3'
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: rake
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
|
-
- -
|
46
|
+
- - ">="
|
47
47
|
- !ruby/object:Gem::Version
|
48
48
|
version: '0'
|
49
49
|
type: :development
|
50
50
|
prerelease: false
|
51
51
|
version_requirements: !ruby/object:Gem::Requirement
|
52
52
|
requirements:
|
53
|
-
- -
|
53
|
+
- - ">="
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '0'
|
56
56
|
- !ruby/object:Gem::Dependency
|
57
57
|
name: rspec
|
58
58
|
requirement: !ruby/object:Gem::Requirement
|
59
59
|
requirements:
|
60
|
-
- -
|
60
|
+
- - ">="
|
61
61
|
- !ruby/object:Gem::Version
|
62
62
|
version: '0'
|
63
63
|
type: :development
|
64
64
|
prerelease: false
|
65
65
|
version_requirements: !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
|
-
- -
|
67
|
+
- - ">="
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '0'
|
70
70
|
description: Embeddable makes it easier to embed videos.
|
@@ -75,20 +75,16 @@ executables: []
|
|
75
75
|
extensions: []
|
76
76
|
extra_rdoc_files: []
|
77
77
|
files:
|
78
|
-
- .gitignore
|
79
|
-
- .
|
80
|
-
- .travis.yml
|
78
|
+
- ".gitignore"
|
79
|
+
- ".travis.yml"
|
81
80
|
- Gemfile
|
82
|
-
- LICENSE.
|
81
|
+
- MIT-LICENSE.md
|
83
82
|
- README.md
|
84
83
|
- Rakefile
|
85
84
|
- embeddable.gemspec
|
86
85
|
- lib/embeddable.rb
|
87
|
-
- lib/embeddable/active_record.rb
|
88
|
-
- lib/embeddable/concerns.rb
|
89
|
-
- lib/embeddable/concerns/embeddable.rb
|
90
86
|
- lib/embeddable/version.rb
|
91
|
-
- spec/lib/
|
87
|
+
- spec/lib/embeddable_spec.rb
|
92
88
|
- spec/spec_helper.rb
|
93
89
|
homepage: http://github.com/hyperoslo/embeddable
|
94
90
|
licenses:
|
@@ -100,20 +96,20 @@ require_paths:
|
|
100
96
|
- lib
|
101
97
|
required_ruby_version: !ruby/object:Gem::Requirement
|
102
98
|
requirements:
|
103
|
-
- -
|
99
|
+
- - ">="
|
104
100
|
- !ruby/object:Gem::Version
|
105
101
|
version: '0'
|
106
102
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
103
|
requirements:
|
108
|
-
- -
|
104
|
+
- - ">="
|
109
105
|
- !ruby/object:Gem::Version
|
110
106
|
version: '0'
|
111
107
|
requirements: []
|
112
108
|
rubyforge_project:
|
113
|
-
rubygems_version: 2.2.
|
109
|
+
rubygems_version: 2.2.0
|
114
110
|
signing_key:
|
115
111
|
specification_version: 4
|
116
112
|
summary: Embeddable makes it easier to embed videos.
|
117
113
|
test_files:
|
118
|
-
- spec/lib/
|
114
|
+
- spec/lib/embeddable_spec.rb
|
119
115
|
- spec/spec_helper.rb
|
data/.rspec
DELETED
data/LICENSE.txt
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
Copyright (c) 2014 Johannes Gorset
|
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.
|
@@ -1 +0,0 @@
|
|
1
|
-
::ActiveRecord::Base.send :include, Embeddable::Concerns::Embeddable if defined?(ActiveRecord)
|
data/lib/embeddable/concerns.rb
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'active_support/all'
|
2
|
-
|
3
|
-
module Embeddable::Concerns
|
4
|
-
module Embeddable
|
5
|
-
extend ActiveSupport::Concern
|
6
|
-
|
7
|
-
SERVICES = {
|
8
|
-
youtube: [
|
9
|
-
%r{^https?://(?:(?:www|m)\.)?youtube\.com/watch\?v=([^&]+)},
|
10
|
-
%r{^https?://(?:(?:www|m)\.)?youtu\.be/([^?]+)}
|
11
|
-
],
|
12
|
-
vimeo: [
|
13
|
-
%r{^https?://(?:www\.)?vimeo\.com/([^\?]+)},
|
14
|
-
]
|
15
|
-
}
|
16
|
-
|
17
|
-
module ClassMethods
|
18
|
-
def embeddable(name, options = {})
|
19
|
-
source = options.fetch :from
|
20
|
-
|
21
|
-
define_method "#{name}_type" do
|
22
|
-
url = send(source)
|
23
|
-
return if url.blank?
|
24
|
-
|
25
|
-
SERVICES.map do |service, patterns|
|
26
|
-
service if patterns.any? { |pattern| url[pattern] }
|
27
|
-
end.compact.first
|
28
|
-
end
|
29
|
-
|
30
|
-
define_method "#{name}_id" do
|
31
|
-
url = send(source)
|
32
|
-
return if url.blank?
|
33
|
-
|
34
|
-
SERVICES.map do |service, patterns|
|
35
|
-
patterns.map { |pattern| url[pattern, 1] }
|
36
|
-
end.flatten.compact.first
|
37
|
-
end
|
38
|
-
|
39
|
-
SERVICES.each do |service, pattern|
|
40
|
-
|
41
|
-
define_method "#{name}_on_#{service}?" do
|
42
|
-
send("#{name}_type") == service
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
@@ -1,121 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Embeddable
|
4
|
-
describe Concerns::Embeddable do
|
5
|
-
subject { Dummy.new }
|
6
|
-
|
7
|
-
class Dummy
|
8
|
-
include Concerns::Embeddable
|
9
|
-
|
10
|
-
embeddable :video, from: :video_url
|
11
|
-
|
12
|
-
attr_accessor :video_url
|
13
|
-
end
|
14
|
-
|
15
|
-
describe '.embeddable' do
|
16
|
-
it 'requires a source property' do
|
17
|
-
expect do
|
18
|
-
Dummy.embeddable :video
|
19
|
-
end.to raise_error KeyError
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
describe 'Vimeo' do
|
24
|
-
context 'with http://vimeo.com/...' do
|
25
|
-
before { subject.video_url = 'https://vimeo.com/77949044' }
|
26
|
-
|
27
|
-
its(:video_type) { should eq :vimeo }
|
28
|
-
its(:video_id) { should eq '77949044' }
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe 'YouTube' do
|
33
|
-
|
34
|
-
context 'with http://youtube.com/watch?v=...' do
|
35
|
-
before { subject.video_url = 'http://youtube.com/watch?v=1&feature=foo' }
|
36
|
-
|
37
|
-
its(:video_type) { should eq :youtube }
|
38
|
-
its(:video_id) { should eq '1' }
|
39
|
-
it { should be_video_on_youtube }
|
40
|
-
end
|
41
|
-
|
42
|
-
context 'with https://youtube.com/watch?v=...' do
|
43
|
-
before { subject.video_url = 'https://youtube.com/watch?v=1&feature=foo' }
|
44
|
-
|
45
|
-
its(:video_type) { should eq :youtube }
|
46
|
-
its(:video_id) { should eq '1' }
|
47
|
-
it { should be_video_on_youtube }
|
48
|
-
end
|
49
|
-
|
50
|
-
context 'with http://www.youtube.com/watch?v=...' do
|
51
|
-
before { subject.video_url = 'http://www.youtube.com/watch?v=1&feature=foo' }
|
52
|
-
|
53
|
-
its(:video_type) { should eq :youtube }
|
54
|
-
its(:video_id) { should eq '1' }
|
55
|
-
it { should be_video_on_youtube }
|
56
|
-
end
|
57
|
-
|
58
|
-
context 'with http://m.youtube.com/watch?v=...' do
|
59
|
-
before { subject.video_url = 'http://m.youtube.com/watch?v=1&feature=foo' }
|
60
|
-
|
61
|
-
its(:video_type) { should eq :youtube }
|
62
|
-
its(:video_id) { should eq '1' }
|
63
|
-
it { should be_video_on_youtube }
|
64
|
-
end
|
65
|
-
|
66
|
-
context 'with http://youtu.be/...' do
|
67
|
-
before { subject.video_url = 'http://youtu.be/1' }
|
68
|
-
|
69
|
-
its(:video_type) { should eq :youtube }
|
70
|
-
its(:video_id) { should eq '1' }
|
71
|
-
it { should be_video_on_youtube }
|
72
|
-
end
|
73
|
-
|
74
|
-
context 'with https://youtu.be/...' do
|
75
|
-
before { subject.video_url = 'https://youtu.be/1' }
|
76
|
-
|
77
|
-
its(:video_type) { should eq :youtube }
|
78
|
-
its(:video_id) { should eq '1' }
|
79
|
-
it { should be_video_on_youtube }
|
80
|
-
end
|
81
|
-
|
82
|
-
context 'with http://www.youtu.be/...' do
|
83
|
-
before { subject.video_url = 'http://www.youtu.be/1' }
|
84
|
-
|
85
|
-
its(:video_type) { should eq :youtube }
|
86
|
-
its(:video_id) { should eq '1' }
|
87
|
-
it { should be_video_on_youtube }
|
88
|
-
end
|
89
|
-
|
90
|
-
context 'with http://m.youtu.be/...' do
|
91
|
-
before { subject.video_url = 'http://m.youtu.be/1' }
|
92
|
-
|
93
|
-
its(:video_type) { should eq :youtube }
|
94
|
-
its(:video_id) { should eq '1' }
|
95
|
-
it { should be_video_on_youtube }
|
96
|
-
end
|
97
|
-
|
98
|
-
end
|
99
|
-
|
100
|
-
describe 'unsupported scenarios' do
|
101
|
-
|
102
|
-
context 'unknown service' do
|
103
|
-
before { subject.video_url = 'http://foobar.com' }
|
104
|
-
|
105
|
-
its(:video_type) { should be_nil }
|
106
|
-
its(:video_id) { should be_nil }
|
107
|
-
it { should_not be_video_on_youtube }
|
108
|
-
end
|
109
|
-
|
110
|
-
context 'empty property value' do
|
111
|
-
before { subject.video_url = nil }
|
112
|
-
|
113
|
-
its(:video_type) { should be_nil }
|
114
|
-
its(:video_id) { should be_nil }
|
115
|
-
it { should_not be_video_on_youtube }
|
116
|
-
end
|
117
|
-
|
118
|
-
end
|
119
|
-
|
120
|
-
end
|
121
|
-
end
|