embeddable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +53 -0
- data/Rakefile +8 -0
- data/embeddable.gemspec +26 -0
- data/lib/embeddable.rb +6 -0
- data/lib/embeddable/active_record.rb +1 -0
- data/lib/embeddable/concerns.rb +3 -0
- data/lib/embeddable/concerns/embeddable.rb +58 -0
- data/lib/embeddable/version.rb +3 -0
- data/spec/lib/embeddable/concerns/embeddable_spec.rb +112 -0
- data/spec/spec_helper.rb +1 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d5ea8ddee330b32ebe578d29fcf6a50bd8b99ae4
|
4
|
+
data.tar.gz: 7c45262cb3fc0fc112417e8efab81cd936247e50
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 28eec77adfff43b90c150c98d7393ae190ceba1e231dabb24012a068e70b0bc235d339fd5cc2358f1e9f3e00d94ad6bfe6aa3c51ef35b2367d82a30130596e6e
|
7
|
+
data.tar.gz: f18f2439d651f66061b22b1ba83e3325f8673c6002163e7fd3de6c5e75e08c4c56a79b4093ed64a0752e117d1103e2a816573c598ea44f2f852d6e7962f8dcf1
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Embeddable
|
2
|
+
|
3
|
+
[![Code Climate](https://codeclimate.com/github/hyperoslo/embeddable.png)](https://codeclimate.com/github/hyperoslo/embeddable)
|
4
|
+
[![Build Status](https://travis-ci.org/hyperoslo/embeddable.png)](https://travis-ci.org/hyperoslo/embeddable)
|
5
|
+
|
6
|
+
Embeddable makes it easier to embed videos.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'embeddable'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install embeddable
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```
|
25
|
+
# app/models/post.rb
|
26
|
+
class Post
|
27
|
+
embeddable :video, from: :video_url
|
28
|
+
end
|
29
|
+
|
30
|
+
# ...
|
31
|
+
post = Post.new video_url: 'http://www.youtube.com/watch?v=bEvNRmPzq9s'
|
32
|
+
|
33
|
+
post.video_on_youtube? # => true
|
34
|
+
post.video_id # => 'bEvNRmPzq9s'
|
35
|
+
```
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
1. Fork it
|
40
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
41
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
42
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
43
|
+
5. Create new Pull Request
|
44
|
+
|
45
|
+
## Credits
|
46
|
+
|
47
|
+
Hyper made this. We're a digital communications agency with a passion for good code,
|
48
|
+
and if you're using this library we probably want to hire you.
|
49
|
+
|
50
|
+
|
51
|
+
## License
|
52
|
+
|
53
|
+
Embeddable is available under the MIT license. See the LICENSE file for more info.
|
data/Rakefile
ADDED
data/embeddable.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'embeddable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "embeddable"
|
8
|
+
spec.version = Embeddable::VERSION
|
9
|
+
spec.authors = ["Johannes Gorset", "Sindre Moen"]
|
10
|
+
spec.email = ["jgorset@gmail.com", "sindre@hyper.no"]
|
11
|
+
spec.description = "Embeddable makes it easier to embed videos."
|
12
|
+
spec.summary = "Embeddable makes it easier to embed videos."
|
13
|
+
spec.homepage = "http://github.com/hyperoslo/embeddable"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "activesupport", "~> 4.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
end
|
data/lib/embeddable.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
::ActiveRecord::Base.send :include, Embeddable::Concerns::Embeddable if defined?(ActiveRecord)
|
@@ -0,0 +1,58 @@
|
|
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
|
+
}
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
# Denotes an embeddable for given name
|
16
|
+
#
|
17
|
+
# Options:
|
18
|
+
# :from (source property for this embeddable, required)
|
19
|
+
#
|
20
|
+
# Example:
|
21
|
+
#
|
22
|
+
# class Post
|
23
|
+
# include Concerns::Embeddable
|
24
|
+
# embeddable :video, from: :video_url
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
def embeddable(name, options = {})
|
28
|
+
source = options.fetch :from
|
29
|
+
|
30
|
+
define_method "#{name}_type" do
|
31
|
+
url = send(source)
|
32
|
+
return if url.blank?
|
33
|
+
|
34
|
+
SERVICES.map do |service, patterns|
|
35
|
+
service if patterns.any? { |pattern| url[pattern] }
|
36
|
+
end.compact.first
|
37
|
+
end
|
38
|
+
|
39
|
+
define_method "#{name}_id" do
|
40
|
+
url = send(source)
|
41
|
+
return if url.blank?
|
42
|
+
|
43
|
+
SERVICES.map do |service, patterns|
|
44
|
+
patterns.map { |pattern| url[pattern, 1] }
|
45
|
+
end.flatten.compact.first
|
46
|
+
end
|
47
|
+
|
48
|
+
SERVICES.each do |service, pattern|
|
49
|
+
|
50
|
+
define_method "#{name}_on_#{service}?" do
|
51
|
+
send("#{name}_type") == service
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,112 @@
|
|
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 'YouTube' do
|
24
|
+
|
25
|
+
context 'with http://youtube.com/watch?v=...' do
|
26
|
+
before { subject.video_url = 'http://youtube.com/watch?v=1&feature=foo' }
|
27
|
+
|
28
|
+
its(:video_type) { should eq :youtube }
|
29
|
+
its(:video_id) { should eq '1' }
|
30
|
+
it { should be_video_on_youtube }
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'with https://youtube.com/watch?v=...' do
|
34
|
+
before { subject.video_url = 'https://youtube.com/watch?v=1&feature=foo' }
|
35
|
+
|
36
|
+
its(:video_type) { should eq :youtube }
|
37
|
+
its(:video_id) { should eq '1' }
|
38
|
+
it { should be_video_on_youtube }
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'with http://www.youtube.com/watch?v=...' do
|
42
|
+
before { subject.video_url = 'http://www.youtube.com/watch?v=1&feature=foo' }
|
43
|
+
|
44
|
+
its(:video_type) { should eq :youtube }
|
45
|
+
its(:video_id) { should eq '1' }
|
46
|
+
it { should be_video_on_youtube }
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'with http://m.youtube.com/watch?v=...' do
|
50
|
+
before { subject.video_url = 'http://m.youtube.com/watch?v=1&feature=foo' }
|
51
|
+
|
52
|
+
its(:video_type) { should eq :youtube }
|
53
|
+
its(:video_id) { should eq '1' }
|
54
|
+
it { should be_video_on_youtube }
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'with http://youtu.be/...' do
|
58
|
+
before { subject.video_url = 'http://youtu.be/1' }
|
59
|
+
|
60
|
+
its(:video_type) { should eq :youtube }
|
61
|
+
its(:video_id) { should eq '1' }
|
62
|
+
it { should be_video_on_youtube }
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'with https://youtu.be/...' do
|
66
|
+
before { subject.video_url = 'https://youtu.be/1' }
|
67
|
+
|
68
|
+
its(:video_type) { should eq :youtube }
|
69
|
+
its(:video_id) { should eq '1' }
|
70
|
+
it { should be_video_on_youtube }
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'with http://www.youtu.be/...' do
|
74
|
+
before { subject.video_url = 'http://www.youtu.be/1' }
|
75
|
+
|
76
|
+
its(:video_type) { should eq :youtube }
|
77
|
+
its(:video_id) { should eq '1' }
|
78
|
+
it { should be_video_on_youtube }
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'with http://m.youtu.be/...' do
|
82
|
+
before { subject.video_url = 'http://m.youtu.be/1' }
|
83
|
+
|
84
|
+
its(:video_type) { should eq :youtube }
|
85
|
+
its(:video_id) { should eq '1' }
|
86
|
+
it { should be_video_on_youtube }
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
describe 'unsupported scenarios' do
|
92
|
+
|
93
|
+
context 'unknown service' do
|
94
|
+
before { subject.video_url = 'http://foobar.com' }
|
95
|
+
|
96
|
+
its(:video_type) { should be_nil }
|
97
|
+
its(:video_id) { should be_nil }
|
98
|
+
it { should_not be_video_on_youtube }
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'empty property value' do
|
102
|
+
before { subject.video_url = nil }
|
103
|
+
|
104
|
+
its(:video_type) { should be_nil }
|
105
|
+
its(:video_id) { should be_nil }
|
106
|
+
it { should_not be_video_on_youtube }
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'embeddable'
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: embeddable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Johannes Gorset
|
8
|
+
- Sindre Moen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-03-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ~>
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '4.0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '4.0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: bundler
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '1.3'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ~>
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '1.3'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rake
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rspec
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
description: Embeddable makes it easier to embed videos.
|
71
|
+
email:
|
72
|
+
- jgorset@gmail.com
|
73
|
+
- sindre@hyper.no
|
74
|
+
executables: []
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- .gitignore
|
79
|
+
- .rspec
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- embeddable.gemspec
|
85
|
+
- lib/embeddable.rb
|
86
|
+
- lib/embeddable/active_record.rb
|
87
|
+
- lib/embeddable/concerns.rb
|
88
|
+
- lib/embeddable/concerns/embeddable.rb
|
89
|
+
- lib/embeddable/version.rb
|
90
|
+
- spec/lib/embeddable/concerns/embeddable_spec.rb
|
91
|
+
- spec/spec_helper.rb
|
92
|
+
homepage: http://github.com/hyperoslo/embeddable
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.2.2
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Embeddable makes it easier to embed videos.
|
116
|
+
test_files:
|
117
|
+
- spec/lib/embeddable/concerns/embeddable_spec.rb
|
118
|
+
- spec/spec_helper.rb
|