embedda 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/README.md +8 -1
- data/embedda.gemspec +1 -1
- data/lib/embedda.rb +31 -21
- data/lib/embedda/core_ext.rb +7 -0
- data/spec/embedda_core_ext_spec.rb +18 -0
- data/spec/embedda_spec.rb +107 -48
- metadata +6 -2
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -38,6 +38,13 @@ This is the default behavoir:
|
|
38
38
|
```ruby
|
39
39
|
[2] pry(main)> "String heheh http://www.youtube.com/watch?v=BVtYSy83XXw yeah".embedda(:filters => [:youtube, :vimeo])
|
40
40
|
=> "String heheh <iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/BVtYSy83XXw\" frameborder=\"0\" allowfullscreen></iframe> yeah"
|
41
|
+
```
|
42
|
+
|
43
|
+
You can enforce https URLs (useful on SSL-secured sites):
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
[2] pry(main)> "String heheh http://www.youtube.com/watch?v=BVtYSy83XXw yeah".embedda(ssl: true)
|
47
|
+
=> "String heheh <iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/BVtYSy83XXw\" frameborder=\"0\" allowfullscreen></iframe> yeah"
|
41
48
|
```
|
42
49
|
|
43
50
|
## Supported embeds
|
@@ -50,4 +57,4 @@ Currently embedda can embed the following formats:
|
|
50
57
|
|
51
58
|
## Links
|
52
59
|
|
53
|
-
Rubygems: https://rubygems.org/gems/embedda
|
60
|
+
Rubygems: https://rubygems.org/gems/embedda
|
data/embedda.gemspec
CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
5
|
gem.name = "embedda"
|
6
|
-
gem.version = '0.0.
|
6
|
+
gem.version = '0.0.4'
|
7
7
|
gem.authors = ["Kasper Grubbe",'Christian Takle']
|
8
8
|
gem.email = ["kaspergrubbe@gmail.com"]
|
9
9
|
gem.homepage = "http://github.com/kaspergrubbe/embedda"
|
data/lib/embedda.rb
CHANGED
@@ -1,49 +1,59 @@
|
|
1
1
|
require 'cgi'
|
2
2
|
|
3
|
-
class
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
options[:filters] = Array(options[:filters])
|
3
|
+
class Embedda
|
4
|
+
class UnknownFilter < StandardError
|
5
|
+
def initialize(name)
|
6
|
+
super "Unknown filter #{name}"
|
7
|
+
end
|
8
|
+
end
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
def initialize(string, options = {})
|
11
|
+
options = {:filters => [:youtube, :vimeo, :soundcloud]}.merge(options)
|
12
|
+
@filters = Array(options[:filters])
|
13
|
+
@protocol = options[:ssl] ? 'https' : 'http'
|
14
|
+
@string = string
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
+
def embed
|
18
|
+
return "" if @string.to_s.empty?
|
19
|
+
@filters.each do |filter_name|
|
20
|
+
begin
|
21
|
+
@string = send("#{filter_name}_replace", @string)
|
22
|
+
rescue NoMethodError
|
23
|
+
raise UnknownFilter.new(filter_name)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
@string
|
17
27
|
end
|
18
28
|
|
19
|
-
|
29
|
+
private
|
30
|
+
|
20
31
|
def youtube_replace(compiled)
|
21
|
-
compiled.gsub!(
|
22
|
-
compiled.gsub!(/[http|https]+:\/\/(?:www\.)?youtube\.com\/watch\?v=([a-zA-Z0-9\-\_]+)\w
|
32
|
+
compiled.gsub!(/(<a[^>]*?youtube\.com\/watch\?v=([a-zA-Z0-9\-\_]+).*?<\/a>)/i) { |m| youtube_player($2) }
|
33
|
+
compiled.gsub!(/([http|https]+:\/\/(?:www\.)?youtube\.com\/watch\?v=([a-zA-Z0-9\-\_]+)\w*)/i) { |m| youtube_player($2) }
|
23
34
|
|
24
35
|
return compiled
|
25
36
|
end
|
26
37
|
def youtube_player(token)
|
27
|
-
"<iframe width=\"560\" height=\"315\" src=\"
|
38
|
+
"<iframe width=\"560\" height=\"315\" src=\"#{@protocol}://www.youtube.com/embed/#{token}\" frameborder=\"0\" allowfullscreen></iframe>"
|
28
39
|
end
|
29
40
|
|
30
41
|
def vimeo_replace(compiled)
|
31
|
-
compiled.gsub!(
|
32
|
-
compiled.gsub!(/[http|https]+:\/\/(?:www\.)?vimeo\.com\/(\d+)\w
|
42
|
+
compiled.gsub!(/(<a[^>]*?vimeo\.com\/(\d+).*?<\/a>)/i) { |m| vimeo_player($2) }
|
43
|
+
compiled.gsub!(/([http|https]+:\/\/(?:www\.)?vimeo\.com\/(\d+)\w*)/i) { |m| vimeo_player($2) }
|
33
44
|
|
34
45
|
return compiled
|
35
46
|
end
|
36
47
|
def vimeo_player(token)
|
37
|
-
"<iframe src=\"
|
48
|
+
"<iframe src=\"#{@protocol}://player.vimeo.com/video/#{token}\" width=\"500\" height=\"281\" frameborder=\"0\" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>"
|
38
49
|
end
|
39
50
|
|
40
51
|
def soundcloud_replace(compiled)
|
41
52
|
r = /(https?:\/\/(?:www.)?soundcloud.com\/[A-Za-z0-9]+(?:[-_][A-Za-z0-9]+)*(?!\/sets(?:\/|$))(?:\/[A-Za-z0-9]+(?:[-_][A-Za-z0-9]+)*){1,2}\/?)/i
|
42
|
-
compiled.gsub!(r) { |
|
53
|
+
compiled.gsub!(r) { |m| soundcloud_player(m) }
|
43
54
|
|
44
55
|
return compiled
|
45
56
|
end
|
46
|
-
|
47
57
|
def soundcloud_player(token)
|
48
58
|
url_encoded_string = CGI::escape(token)
|
49
59
|
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'embedda/core_ext'
|
3
|
+
|
4
|
+
describe String do
|
5
|
+
it 'should have #embedda method' do
|
6
|
+
expect(described_class.new).to respond_to(:embedda)
|
7
|
+
end
|
8
|
+
|
9
|
+
context '#embedda' do
|
10
|
+
let(:embed_string) { '<iframe width="560" height="315" src="http://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen></iframe>' }
|
11
|
+
let(:media_link) { 'http://www.youtube.com/watch?v=dQw4w9WgXcQ' }
|
12
|
+
|
13
|
+
it 'should embed media' do
|
14
|
+
expect(media_link.embedda).to eq(embed_string)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
data/spec/embedda_spec.rb
CHANGED
@@ -1,113 +1,172 @@
|
|
1
1
|
require "spec_helper"
|
2
|
-
require
|
2
|
+
require "embedda"
|
3
|
+
|
4
|
+
describe Embedda do
|
5
|
+
let(:embedda) { described_class.new(@story).embed }
|
3
6
|
|
4
|
-
describe "Embedda" do
|
5
7
|
context "Youtube-link" do
|
6
|
-
|
7
|
-
|
8
|
-
end
|
8
|
+
let(:embed_string) { '<iframe width="560" height="315" src="http://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen></iframe>' }
|
9
|
+
let(:https_embed_string) { '<iframe width="560" height="315" src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen></iframe>' }
|
9
10
|
|
10
11
|
it "should embed when text have a link" do
|
11
|
-
story = "http://www.youtube.com/watch?v=dQw4w9WgXcQ"
|
12
|
-
expect(
|
12
|
+
@story = "http://www.youtube.com/watch?v=dQw4w9WgXcQ"
|
13
|
+
expect(embedda).to eq(embed_string)
|
13
14
|
end
|
14
15
|
|
15
16
|
it "should embed when text have a link with feature_embed" do
|
16
|
-
story = "http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ"
|
17
|
-
|
17
|
+
@story = "http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ"
|
18
|
+
pending
|
19
|
+
expect(embedda).to eq(embed_string)
|
18
20
|
end
|
19
21
|
|
20
22
|
it "should embed when also other text is present around link" do
|
21
|
-
story =
|
22
|
-
expect(
|
23
|
+
@story = 'Hello, my name is Kasper: http://www.youtube.com/watch?v=dQw4w9WgXcQ<br/>And I am embedding links'
|
24
|
+
expect(embedda).to eq("Hello, my name is Kasper: #{embed_string}<br/>And I am embedding links")
|
23
25
|
end
|
24
26
|
|
25
27
|
it "should embed when text include anchor tag to Youtube" do
|
26
|
-
story =
|
27
|
-
expect(
|
28
|
+
@story = '<a href="http://www.youtube.com/watch?v=dQw4w9WgXcQ">Look here for HalfLife3!</a>'
|
29
|
+
expect(embedda).to eq(embed_string)
|
28
30
|
end
|
29
31
|
|
30
32
|
it "should embed when also other text is present around anchor tag" do
|
31
|
-
story =
|
32
|
-
expect(
|
33
|
+
@story = 'Hello, my name is Kasper: <b><a href="http://www.youtube.com/watch?v=dQw4w9WgXcQ">Look here for HalfLife3!</a><br/></b>And I am embedding links'
|
34
|
+
expect(embedda).to eq("Hello, my name is Kasper: <b>#{embed_string}<br/></b>And I am embedding links")
|
33
35
|
end
|
34
36
|
|
35
37
|
it "should embed when content is around the link" do
|
36
|
-
story = "\n\nMy suggestions for getting ready for the dreadful monday we all hate:\n\nhttp://www.youtube.com/watch?v=dQw4w9WgXcQ\n\n"
|
37
|
-
expect(
|
38
|
+
@story = "\n\nMy suggestions for getting ready for the dreadful monday we all hate:\n\nhttp://www.youtube.com/watch?v=dQw4w9WgXcQ\n\n"
|
39
|
+
expect(embedda).to eq("\n\nMy suggestions for getting ready for the dreadful monday we all hate:\n\n#{embed_string}\n\n")
|
38
40
|
end
|
39
41
|
|
40
42
|
it "should embed when enabled" do
|
41
|
-
story
|
42
|
-
|
43
|
+
@story = 'Hello, my name is Kasper: <b><a href="http://www.youtube.com/watch?v=dQw4w9WgXcQ">Look here for HalfLife3!</a><br/></b>And I am embedding links'
|
44
|
+
embedda = described_class.new(@story, :filters => :youtube).embed
|
45
|
+
expect(embedda).to eq("Hello, my name is Kasper: <b>#{embed_string}<br/></b>And I am embedding links")
|
43
46
|
end
|
44
47
|
|
45
48
|
it "should not embed when disabled" do
|
46
|
-
story =
|
47
|
-
|
49
|
+
@story = 'Hello, my name is Kasper: <b><a href="http://www.youtube.com/watch?v=dQw4w9WgXcQ">Look here for HalfLife3!</a><br/></b>And I am embedding links'
|
50
|
+
embedda = described_class.new(@story, :filters => :vimeo).embed
|
51
|
+
expect(embedda).to eq('Hello, my name is Kasper: <b><a href="http://www.youtube.com/watch?v=dQw4w9WgXcQ">Look here for HalfLife3!</a><br/></b>And I am embedding links')
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should output a https link if ssl option is given' do
|
55
|
+
@story = "http://www.youtube.com/watch?v=dQw4w9WgXcQ"
|
56
|
+
expect(described_class.new(@story, :ssl => true).embed).to eq(https_embed_string)
|
48
57
|
end
|
49
58
|
end
|
50
59
|
|
51
60
|
context "Vimeo-link" do
|
52
|
-
|
53
|
-
|
54
|
-
end
|
61
|
+
let(:embed_string) { '<iframe src="http://player.vimeo.com/video/20241459" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>' }
|
62
|
+
let(:https_embed_string) { '<iframe src="https://player.vimeo.com/video/20241459" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>' }
|
55
63
|
|
56
64
|
it "should embed when text have a link" do
|
57
|
-
story = "http://vimeo.com/20241459"
|
58
|
-
expect(
|
65
|
+
@story = "http://vimeo.com/20241459"
|
66
|
+
expect(embedda).to eq(embed_string)
|
59
67
|
end
|
60
68
|
|
61
69
|
it "should embed when also other text is present around link" do
|
62
|
-
story =
|
63
|
-
expect(
|
70
|
+
@story = 'Hello, my name is Kasper: http://vimeo.com/20241459<br/>And I am embedding links'
|
71
|
+
expect(embedda).to eq("Hello, my name is Kasper: #{embed_string}<br/>And I am embedding links")
|
64
72
|
end
|
65
73
|
|
66
74
|
it "should embed when text include anchor tag to Vimeo" do
|
67
|
-
story =
|
68
|
-
expect(
|
75
|
+
@story = '<a href="http://vimeo.com/20241459">Look here for HalfLife3!</a>'
|
76
|
+
expect(embedda).to eq(embed_string)
|
69
77
|
end
|
70
78
|
|
71
79
|
it "should embed when also other text is present around anchor tag" do
|
72
|
-
story =
|
73
|
-
expect(
|
80
|
+
@story = 'Hello, my name is Kasper: <a href="http://vimeo.com/20241459">Look here for HalfLife3!</a><br/>And I am embedding links'
|
81
|
+
expect(embedda).to eq("Hello, my name is Kasper: #{embed_string}<br/>And I am embedding links")
|
74
82
|
end
|
75
83
|
|
76
84
|
it "should embed when enabled" do
|
77
|
-
story
|
78
|
-
|
85
|
+
@story = 'Hello, my name is Kasper: <a href="http://vimeo.com/20241459">Look here for HalfLife3!</a><br/>And I am embedding links'
|
86
|
+
embedda = described_class.new(@story, :filters => :vimeo).embed
|
87
|
+
expect(embedda).to eq("Hello, my name is Kasper: #{embed_string}<br/>And I am embedding links")
|
79
88
|
end
|
80
89
|
|
81
90
|
it "should not embed when disabled" do
|
82
|
-
story
|
83
|
-
|
91
|
+
@story = 'Hello, my name is Kasper: <a href="http://vimeo.com/20241459">Look here for HalfLife3!</a><br/>And I am embedding links'
|
92
|
+
embedda = described_class.new(@story, :filters => :youtube).embed
|
93
|
+
expect(embedda).to eq('Hello, my name is Kasper: <a href="http://vimeo.com/20241459">Look here for HalfLife3!</a><br/>And I am embedding links')
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'should output a https link if ssl option is given' do
|
97
|
+
@story = "http://vimeo.com/20241459"
|
98
|
+
expect(described_class.new(@story, :ssl => true).embed).to eq(https_embed_string)
|
84
99
|
end
|
85
100
|
end
|
86
101
|
|
87
102
|
context "Soundcloud-link" do
|
88
|
-
|
89
|
-
|
90
|
-
@link = "https://soundcloud.com/flume-1/flume-left-alone-bobby-tank"
|
91
|
-
end
|
103
|
+
let(:embed_string) { '<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A%2F%2Fsoundcloud.com%2Fflume-1%2Fflume-left-alone-bobby-tank&color=ff6600&auto_play=false&show_artwork=false"></iframe>' }
|
104
|
+
let(:link) { 'https://soundcloud.com/flume-1/flume-left-alone-bobby-tank' }
|
92
105
|
|
93
106
|
it "should embed when text have a link" do
|
94
|
-
story =
|
95
|
-
expect(
|
107
|
+
@story = link
|
108
|
+
expect(embedda).to eq(embed_string)
|
96
109
|
end
|
97
110
|
|
98
111
|
it "should embed when also other text is present around link" do
|
99
|
-
story = "Hello, my name is Takle: #{
|
100
|
-
expect(
|
112
|
+
@story = "Hello, my name is Takle: #{link}<br/>And I am embedding links"
|
113
|
+
expect(embedda).to eq("Hello, my name is Takle: #{embed_string}<br/>And I am embedding links")
|
101
114
|
end
|
102
115
|
|
103
116
|
it "should embed when enabled" do
|
104
|
-
story
|
105
|
-
|
117
|
+
@story = "Hello, my name is Takle: #{link}<br/>And I am embedding links"
|
118
|
+
embedda = described_class.new(@story, :filters => :soundcloud).embed
|
119
|
+
expect(embedda).to eq("Hello, my name is Takle: #{embed_string}<br/>And I am embedding links")
|
106
120
|
end
|
107
121
|
|
108
122
|
it "should not embed when disabled" do
|
109
|
-
story
|
110
|
-
|
123
|
+
@story = "Hello, my name is Takle: #{link}<br/>And I am not embedding links"
|
124
|
+
embedda = described_class.new(@story, :filters => :youtube).embed
|
125
|
+
expect(embedda).to eq("Hello, my name is Takle: #{link}<br/>And I am not embedding links")
|
111
126
|
end
|
112
127
|
end
|
128
|
+
|
129
|
+
context "All embeds in one string" do
|
130
|
+
let(:youtube) { '<iframe width="560" height="315" src="http://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen></iframe>' }
|
131
|
+
let(:youtube_link) { 'http://www.youtube.com/watch?v=dQw4w9WgXcQ' }
|
132
|
+
let(:vimeo) { '<iframe src="http://player.vimeo.com/video/20241459" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>' }
|
133
|
+
let(:vimeo_link) { 'http://vimeo.com/20241459' }
|
134
|
+
let(:soundcloud) { '<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A%2F%2Fsoundcloud.com%2Fflume-1%2Fflume-left-alone-bobby-tank&color=ff6600&auto_play=false&show_artwork=false"></iframe>' }
|
135
|
+
let(:soundcloud_link) { 'https://soundcloud.com/flume-1/flume-left-alone-bobby-tank' }
|
136
|
+
|
137
|
+
it "should show two youtube embeds" do
|
138
|
+
@story = "#{youtube_link} #{youtube_link}"
|
139
|
+
expect(embedda).to eq("#{youtube} #{youtube}")
|
140
|
+
end
|
141
|
+
|
142
|
+
it "should show two vimeo embeds" do
|
143
|
+
@story = "#{vimeo_link} #{vimeo_link}"
|
144
|
+
expect(embedda).to eq("#{vimeo} #{vimeo}")
|
145
|
+
end
|
146
|
+
|
147
|
+
it "should show two soundcloud embeds" do
|
148
|
+
@story = "#{soundcloud_link} #{soundcloud_link}"
|
149
|
+
expect(embedda).to eq("#{soundcloud} #{soundcloud}")
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should show all the embeds in the @story" do
|
153
|
+
@story = "#{youtube_link} #{vimeo_link} #{soundcloud_link}"
|
154
|
+
expect(embedda).to eq("#{youtube} #{vimeo} #{soundcloud}")
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
context "Unkown filter" do
|
159
|
+
it "should refuse to embed when unknown filter passed" do
|
160
|
+
story = "http://www.youtube.com/watch?v=dQw4w9WgXcQ"
|
161
|
+
expect { described_class.new(story, :filters => [:dummy]).embed }.to raise_error(Embedda::UnknownFilter)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
context "Empty string" do
|
166
|
+
it "should return empty string" do
|
167
|
+
@story = ""
|
168
|
+
expect(embedda).to eq("")
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
113
172
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: embedda
|
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:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-
|
13
|
+
date: 2013-08-06 00:00:00.000000000 Z
|
14
14
|
dependencies: []
|
15
15
|
description: Embed content strings in your strings, and turn it to embed HTML!
|
16
16
|
email:
|
@@ -28,6 +28,8 @@ files:
|
|
28
28
|
- Rakefile.rb
|
29
29
|
- embedda.gemspec
|
30
30
|
- lib/embedda.rb
|
31
|
+
- lib/embedda/core_ext.rb
|
32
|
+
- spec/embedda_core_ext_spec.rb
|
31
33
|
- spec/embedda_spec.rb
|
32
34
|
- spec/spec_helper.rb
|
33
35
|
homepage: http://github.com/kaspergrubbe/embedda
|
@@ -55,5 +57,7 @@ signing_key:
|
|
55
57
|
specification_version: 3
|
56
58
|
summary: Embed content strings in your strings, and turn it to embed HTML!
|
57
59
|
test_files:
|
60
|
+
- spec/embedda_core_ext_spec.rb
|
58
61
|
- spec/embedda_spec.rb
|
59
62
|
- spec/spec_helper.rb
|
63
|
+
has_rdoc:
|