embeddable 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 18f2ab466c6cadbd615d9773926ea34341000a99
4
- data.tar.gz: 9d213d00bafe3d515137494967076727b85236c6
3
+ metadata.gz: 729da179e882d3556c3e2d47fab535508ddc72a8
4
+ data.tar.gz: 923d598b514adbe8b52d0e49594d618b579729ef
5
5
  SHA512:
6
- metadata.gz: 78354feb478b54c646977720ec78feb9090afc5b0aac146a619c753bf526c6770f693a93442899506dcf6a9d53810c3566ede6ed82ab5a1b7427d9aee5f83fe6
7
- data.tar.gz: 6da0f5c692782ad05a7c7e55dd0795b5f410401f913aace349d2f77380293ff0da9fa6c1dbdabaddfadb162305e811ea4576c79256b944478a57fd1ef89c0e42
6
+ metadata.gz: d80487840799dfe6dd9b005398769137777a70e18da8dc0e98e5be2af1ebbf004fac05f2c03b7632fc031a6358a0b2949c13bd8ac349ab0734e20845e5fbd938
7
+ data.tar.gz: c536bdf7cd58acbca954f75db6eb025da177160c1036f2cb5d39db8630eab44fab82e298d62875b045c5d2112c51d8f6408c5af34db9926cb171297c8388adb3
data/README.md CHANGED
@@ -39,7 +39,38 @@ post.video_on_youtube? # => true
39
39
  post.video_id # => 'bEvNRmPzq9s'
40
40
  ```
41
41
 
42
- ### Brightcove
42
+ ### Views
43
+ You can use a simple view helper to render the respective video players. `embed_video(embeddable, width, height)`
44
+
45
+ ```erb
46
+ <div class="video-wrapper">
47
+ <%= embed_video(embeddable, '100%', '100%') %>
48
+ </div>
49
+ ```
50
+
51
+ #### Multiple embeddables in one model
52
+ If you have multiple columns using embeddable in one model, you will need to specify which one you want to embed unless it's the first one specified in the model. Add an extra parameter to the view helper: `embed_video(embeddable, width, height, name: :your_embeddable_name)`.
53
+
54
+ If you have this in your model:
55
+ ```ruby
56
+ embeddable :video, from: :video_url
57
+ embeddable :another_video, from: :another_url
58
+ ```
59
+
60
+ And you want to embed `:another_video` - you will use the helper like this:
61
+ ```erb
62
+ <%= embed_video(embeddable, '100%', '100%', name: :another_video) %>
63
+ ```
64
+
65
+ #### Overriding
66
+ The view helper uses partials to render the embed snippets for all of the video players. You can override them by creating a partial here: `app/views/embeddable/`. The file should be named after the player. See example below.
67
+
68
+ ```erb
69
+ <!-- app/views/embeddable/partials/_youtube.html.erb -->
70
+ <iframe width="<%= width %>" height="<%= height %>" src="//www.youtube.com/embed/<%= id %>" frameborder="0" allowfullscreen webkitallowfullscreen mozillowfullscreen></iframe>
71
+ ```
72
+
73
+ #### Brightcove
43
74
  If you want to support brightcove, you'll need to add
44
75
  your own brightcove player by overriding the brightcove partial.
45
76
 
@@ -22,5 +22,6 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
24
24
  spec.add_development_dependency "rake"
25
- spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency "rspec-its"
26
27
  end
@@ -31,7 +31,13 @@ module Embeddable
31
31
  ]
32
32
  }
33
33
 
34
+ included do
35
+ @embeddables = []
36
+ end
37
+
34
38
  module ClassMethods
39
+ attr_reader :embeddables
40
+
35
41
  def embeddable(name, options = {})
36
42
  source = options.fetch :from
37
43
 
@@ -53,6 +59,10 @@ module Embeddable
53
59
  end.flatten.compact.first
54
60
  end
55
61
 
62
+ define_method "#{name}?" do
63
+ send("#{name}_id") ? true : false
64
+ end
65
+
56
66
  SERVICES.each do |service, pattern|
57
67
 
58
68
  define_method "#{name}_on_#{service}?" do
@@ -60,6 +70,12 @@ module Embeddable
60
70
  end
61
71
 
62
72
  end
73
+
74
+ define_method "#{name}_source" do
75
+ source
76
+ end
77
+
78
+ @embeddables << name
63
79
  end
64
80
  end
65
81
  end
@@ -3,7 +3,7 @@ require 'embeddable/view_helpers'
3
3
  module Embeddable
4
4
  class Railtie < Rails::Railtie
5
5
  initializer "embeddable.view_helpers" do
6
- ActionView::Base.send :include, ViewHelpers
6
+ ActionView::Base.send :include, Embeddable::ViewHelpers
7
7
  end
8
8
 
9
9
  initializer 'embeddable.add_view_paths', :after => :add_view_paths do |app|
@@ -1,3 +1,3 @@
1
1
  module Embeddable
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -1,24 +1,32 @@
1
- module ViewHelpers
2
- def embed_video(embeddable, width, height)
3
- attributes = { id: embeddable.video_id, width: width, height: height }
1
+ module Embeddable::ViewHelpers
2
+ def embed_video(embeddable, width, height, options = {})
3
+ if options[:name] && !embeddable.respond_to?("#{options[:name]}_id")
4
+ raise "Can't find embeddable name. Did you mean: \"#{embeddable.class.embeddables.last.inspect}\"?"
5
+ end
6
+ name = options[:name] || embeddable.class.embeddables.first
7
+ source = embeddable.send("#{name}_source")
8
+ render_embeddable_partial(embeddable, name, source, width, height)
9
+ end
4
10
 
5
- if embeddable.video_on_youtube?
11
+ def render_embeddable_partial(embeddable, name, source, width, height)
12
+ attributes = { id: embeddable.send("#{name}_id"), width: width, height: height }
13
+ if embeddable.send("#{name}_on_youtube?")
6
14
  render 'embeddable/partials/youtube', attributes
7
- elsif embeddable.video_on_vimeo?
15
+ elsif embeddable.send("#{name}_on_vimeo?")
8
16
  render 'embeddable/partials/vimeo', attributes
9
- elsif embeddable.video_on_dailymotion?
17
+ elsif embeddable.send("#{name}_on_dailymotion?")
10
18
  render 'embeddable/partials/dailymotion', attributes
11
- elsif embeddable.video_on_veoh?
19
+ elsif embeddable.send("#{name}_on_veoh?")
12
20
  render 'embeddable/partials/veoh', attributes
13
- elsif embeddable.video_on_vippy?
21
+ elsif embeddable.send("#{name}_on_vippy?")
14
22
  render 'embeddable/partials/vippy', attributes
15
- elsif embeddable.video_on_liveleak?
23
+ elsif embeddable.send("#{name}_on_liveleak?")
16
24
  # not supported
17
- link_to embeddable.url, embeddable.url
18
- elsif embeddable.video_on_brightcove?
25
+ link_to embeddable.send(source), embeddable.send(source)
26
+ elsif embeddable.send("#{name}_on_brightcove?")
19
27
  render 'embeddable/partials/brightcove', attributes
20
28
  else
21
- link_to embeddable.url, embeddable.url
29
+ link_to embeddable.send(source), embeddable.send(source)
22
30
  end
23
31
  end
24
32
  end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+ require 'embeddable/view_helpers'
3
+
4
+ describe Embeddable::ViewHelpers do
5
+ subject { Dummy.new }
6
+
7
+ class HelperDummy
8
+ include Embeddable::ViewHelpers
9
+
10
+ def link_to(*args)
11
+ true
12
+ end
13
+ end
14
+
15
+ before do
16
+ @helper = HelperDummy.new
17
+ end
18
+
19
+ describe 'embed_video' do
20
+ context 'specifying embeddable name' do
21
+ it 'should use the name provided' do
22
+ expect(@helper).to receive(:link_to)
23
+ @helper.embed_video(subject, '100%', '100%', name: :super_video)
24
+ end
25
+
26
+ it 'should raise error if the provided name is wrong' do
27
+ expect do
28
+ @helper.embed_video(subject, '100%', '100%', name: :wrong_name)
29
+ end.to raise_error
30
+ end
31
+ end
32
+
33
+ context 'not specifying embeddable name' do
34
+ it 'should choose the first embeddable name' do
35
+ expect(@helper).to receive(:render_embeddable_partial).
36
+ with(subject, :video, :video_url, '100%', '100%')
37
+ @helper.embed_video(subject, '100%', '100%')
38
+ end
39
+ end
40
+ end
41
+ end
@@ -7,8 +7,9 @@ describe Embeddable do
7
7
  include Embeddable
8
8
 
9
9
  embeddable :video, from: :video_url
10
+ embeddable :super_video, from: :another_url
10
11
 
11
- attr_accessor :video_url
12
+ attr_accessor :video_url, :another_url
12
13
  end
13
14
 
14
15
  describe '.embeddable' do
@@ -19,10 +20,26 @@ describe Embeddable do
19
20
  end
20
21
  end
21
22
 
23
+ describe 'predicate' do
24
+ context ':video_url is a supported url' do
25
+ it 'should be true' do
26
+ subject.video_url = 'http://youtube.com/watch?v=1&feature=foo'
27
+ expect(subject.video?).to be_truthy
28
+ end
29
+ end
30
+
31
+ context ':video_url is an unsupported url' do
32
+ it 'should be false' do
33
+ subject.video_url = 'http://foo.bar'
34
+ expect(subject.video?).to be_falsey
35
+ end
36
+ end
37
+ end
38
+
22
39
  describe 'Liveleak' do
23
40
  context 'with http://liveleak.com/...' do
24
- before {
25
- subject.video_url = 'http://www.liveleak.com/view?i=290_1392029603'
41
+ before {
42
+ subject.video_url = 'http://www.liveleak.com/view?i=290_1392029603'
26
43
  }
27
44
 
28
45
  its(:video_type) { should eq :liveleak }
@@ -32,8 +49,8 @@ describe Embeddable do
32
49
 
33
50
  describe 'Dailymotion' do
34
51
  context 'with http://dailymotion.com/video/<id>...' do
35
- before {
36
- subject.video_url = 'http://www.dailymotion.com/video/xu4q8m'
52
+ before {
53
+ subject.video_url = 'http://www.dailymotion.com/video/xu4q8m'
37
54
  }
38
55
 
39
56
  its(:video_type) { should eq :dailymotion }
@@ -41,8 +58,8 @@ describe Embeddable do
41
58
  end
42
59
 
43
60
  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'
61
+ before {
62
+ subject.video_url = 'http://www.dailymotion.com/video/xu4q8m_apprendre-le-deltaplane-a-millau-hang-gliding-in-france-creative-motion_sport'
46
63
  }
47
64
 
48
65
  its(:video_type) { should eq :dailymotion }
@@ -52,8 +69,8 @@ describe Embeddable do
52
69
 
53
70
  describe 'Veoh' do
54
71
  context 'with http://veoh.com/watch/<id>...' do
55
- before {
56
- subject.video_url = 'http://www.veoh.com/watch/v36298453QmtnSAza'
72
+ before {
73
+ subject.video_url = 'http://www.veoh.com/watch/v36298453QmtnSAza'
57
74
  }
58
75
 
59
76
  its(:video_type) { should eq :veoh }
@@ -61,8 +78,8 @@ describe Embeddable do
61
78
  end
62
79
 
63
80
  context 'with http://veoh.com/watch/<id>/<blargablarga>...' do
64
- before {
65
- subject.video_url = 'http://www.veoh.com/watch/v36298453QmtnSAza/CBS-SciTech-News'
81
+ before {
82
+ subject.video_url = 'http://www.veoh.com/watch/v36298453QmtnSAza/CBS-SciTech-News'
66
83
  }
67
84
 
68
85
  its(:video_type) { should eq :veoh }
@@ -81,7 +98,7 @@ describe Embeddable do
81
98
 
82
99
  describe 'Vippy' do
83
100
  context 'with http://liveleak.com/...' do
84
- before {
101
+ before {
85
102
  subject.video_url = <<SUCH_EMBED_CODE
86
103
  <!-- Start Vippy video -->
87
104
  <div itemscope itemtype="http://schema.org/VideoObject" class="vippy-video" style="width: 640px; height: 360px; position: relative;">
@@ -181,6 +198,24 @@ SUCH_EMBED_CODE
181
198
 
182
199
  end
183
200
 
201
+ describe '.video_source' do
202
+ it 'should return the source of video' do
203
+ expect(subject.video_source).to eql(:video_url)
204
+ end
205
+ end
206
+
207
+ describe '.super_video_source' do
208
+ it 'should return the source of super_video' do
209
+ expect(subject.super_video_source).to eql(:another_url)
210
+ end
211
+ end
212
+
213
+ context 'multiple embeddable columns' do
214
+ it 'should store an array of names on the class' do
215
+ expect(Dummy.embeddables).to eql([:video, :super_video])
216
+ end
217
+ end
218
+
184
219
  describe 'unsupported scenarios' do
185
220
 
186
221
  context 'unknown service' do
@@ -1 +1,2 @@
1
1
  require 'embeddable'
2
+ require 'rspec/its'
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.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johannes Gorset
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-05-22 00:00:00.000000000 Z
12
+ date: 2014-06-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -55,6 +55,20 @@ dependencies:
55
55
  version: '0'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: '3.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '3.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec-its
58
72
  requirement: !ruby/object:Gem::Requirement
59
73
  requirements:
60
74
  - - '>='
@@ -86,6 +100,7 @@ files:
86
100
  - lib/embeddable/railtie.rb
87
101
  - lib/embeddable/version.rb
88
102
  - lib/embeddable/view_helpers.rb
103
+ - spec/lib/embeddable/view_helpers_spec.rb
89
104
  - spec/lib/embeddable_spec.rb
90
105
  - spec/spec_helper.rb
91
106
  - views/embeddable/partials/_brightcove.html.erb
@@ -119,5 +134,6 @@ signing_key:
119
134
  specification_version: 4
120
135
  summary: Embeddable makes it easier to embed videos.
121
136
  test_files:
137
+ - spec/lib/embeddable/view_helpers_spec.rb
122
138
  - spec/lib/embeddable_spec.rb
123
139
  - spec/spec_helper.rb