embedda 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -22,31 +22,38 @@ And remember to `bundle`
22
22
  This gem adds `String#embedda` to Ruby strings. You use it like this:
23
23
 
24
24
  ```ruby
25
- [2] pry(main)> "String heheh http://www.youtube.com/watch?v=BVtYSy83XXw yeah".embedda
25
+ "String heheh http://www.youtube.com/watch?v=BVtYSy83XXw yeah".embedda
26
26
  => "String heheh <iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/BVtYSy83XXw\" frameborder=\"0\" allowfullscreen></iframe> yeah"
27
27
  ```
28
28
 
29
29
  If you need only one of the parsers you can do like this:
30
30
 
31
31
  ```ruby
32
- [2] pry(main)> "String heheh http://www.youtube.com/watch?v=BVtYSy83XXw yeah".embedda(:filters => :youtube)
32
+ "String heheh http://www.youtube.com/watch?v=BVtYSy83XXw yeah".embedda(:filters => :youtube)
33
33
  => "String heheh <iframe width=\"560\" height=\"315\" src=\"http://www.youtube.com/embed/BVtYSy83XXw\" frameborder=\"0\" allowfullscreen></iframe> yeah"
34
34
  ```
35
35
 
36
36
  This is the default behavoir:
37
37
 
38
38
  ```ruby
39
- [2] pry(main)> "String heheh http://www.youtube.com/watch?v=BVtYSy83XXw yeah".embedda(:filters => [:youtube, :vimeo])
39
+ "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
41
  ```
42
42
 
43
- You can enforce https URLs (useful on SSL-secured sites):
43
+ You can enforce https URLs (useful on SSL-secured sites):
44
44
 
45
45
  ```ruby
46
- [2] pry(main)> "String heheh http://www.youtube.com/watch?v=BVtYSy83XXw yeah".embedda(ssl: true)
46
+ "String heheh http://www.youtube.com/watch?v=BVtYSy83XXw yeah".embedda(ssl: true)
47
47
  => "String heheh <iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/BVtYSy83XXw\" frameborder=\"0\" allowfullscreen></iframe> yeah"
48
48
  ```
49
49
 
50
+ You can set video width and height with proper settings:
51
+
52
+ ```ruby
53
+ "http://www.youtube.com/watch?v=BVtYSy83XXw yeah".embedda(video_width: 600, video_height: 300)
54
+ => "String heheh <iframe width=\"600\" height=\"300\" src=\"https://www.youtube.com/embed/BVtYSy83XXw\" frameborder=\"0\" allowfullscreen></iframe> yeah"
55
+ ```
56
+
50
57
  ## Supported embeds
51
58
 
52
59
  Currently embedda can embed the following formats:
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.4'
6
+ gem.version = '0.0.5'
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,4 +1,4 @@
1
- require 'cgi'
1
+ require 'embedda/filters/provider'
2
2
 
3
3
  class Embedda
4
4
  class UnknownFilter < StandardError
@@ -7,57 +7,42 @@ class Embedda
7
7
  end
8
8
  end
9
9
 
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
10
+ attr_reader :string, :options
11
+
12
+ def initialize(string_to_process, options = {})
13
+ @options = defaultize_options(options)
14
+ @string = string_to_process.to_s
15
15
  end
16
16
 
17
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
18
+ return @string if @string.empty?
19
+ process_string_with_all_filters
26
20
  @string
27
21
  end
28
22
 
29
23
  private
30
24
 
31
- def youtube_replace(compiled)
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) }
34
-
35
- return compiled
36
- end
37
- def youtube_player(token)
38
- "<iframe width=\"560\" height=\"315\" src=\"#{@protocol}://www.youtube.com/embed/#{token}\" frameborder=\"0\" allowfullscreen></iframe>"
25
+ def defaultize_options(options)
26
+ {:filters => [:youtube, :vimeo, :soundcloud]}.merge(options)
39
27
  end
40
28
 
41
- def vimeo_replace(compiled)
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) }
44
-
45
- return compiled
29
+ def filters
30
+ Array(options[:filters])
46
31
  end
47
- def vimeo_player(token)
48
- "<iframe src=\"#{@protocol}://player.vimeo.com/video/#{token}\" width=\"500\" height=\"281\" frameborder=\"0\" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>"
32
+
33
+ def process_string_with_all_filters
34
+ filters.each{|filter_name| process_with_filter(filter_name) }
49
35
  end
50
36
 
51
- def soundcloud_replace(compiled)
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
53
- compiled.gsub!(r) { |m| soundcloud_player(m) }
37
+ def process_with_filter(filter_name)
38
+ get_filter(filter_name).process(string)
39
+ end
54
40
 
55
- return compiled
41
+ def get_filter(filter_name)
42
+ filter_provider.get(filter_name)
56
43
  end
57
- def soundcloud_player(token)
58
- url_encoded_string = CGI::escape(token)
59
44
 
60
- # Note: added '&' ...?url=...&
61
- "<iframe width=\"100%\" height=\"166\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=#{url_encoded_string}&color=ff6600&amp;auto_play=false&amp;show_artwork=false\"></iframe>"
45
+ def filter_provider
46
+ Filters::Provider.new(options)
62
47
  end
63
48
  end
@@ -0,0 +1,43 @@
1
+ require 'embedda/filters/vimeo'
2
+ require 'embedda/filters/youtube'
3
+ require 'embedda/filters/soundcloud'
4
+
5
+ class Embedda
6
+ module Filters
7
+ class Provider
8
+
9
+ attr_reader :options
10
+ def initialize(options)
11
+ @options = options
12
+ end
13
+
14
+ def get(filter_name)
15
+ case filter_name
16
+ when :vimeo
17
+ Vimeo.new(protocol, video_width, video_height)
18
+ when :youtube
19
+ Youtube.new(protocol, video_width, video_height)
20
+ when :soundcloud
21
+ Soundcloud.new
22
+ else
23
+ raise UnknownFilter.new(filter_name)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def protocol
30
+ options[:ssl] ? 'https' : 'http'
31
+ end
32
+
33
+ def video_width
34
+ options.fetch(:video_width, 560)
35
+ end
36
+
37
+ def video_height
38
+ options.fetch(:video_height, 315)
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,21 @@
1
+ require 'cgi'
2
+
3
+ class Embedda
4
+ module Filters
5
+ class Soundcloud
6
+ def process(string)
7
+ 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
8
+ string.gsub!(r) { |m| player(m) }
9
+ return string
10
+ end
11
+
12
+ private
13
+
14
+ def player(token)
15
+ url_encoded_string = CGI::escape(token)
16
+ # Note: added '&' ...?url=...&
17
+ "<iframe width=\"100%\" height=\"166\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=#{url_encoded_string}&color=ff6600&amp;auto_play=false&amp;show_artwork=false\"></iframe>"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ class Embedda
2
+ module Filters
3
+ class Vimeo
4
+ def initialize(protocol, width, height)
5
+ @protocol = protocol
6
+ @width = width
7
+ @height = height
8
+ end
9
+
10
+ def process(string)
11
+ string.gsub!(/(<a[^>]*?vimeo\.com\/(\d+).*?<\/a>)/i) { |m| player($2) }
12
+ string.gsub!(/([http|https]+:\/\/(?:www\.)?vimeo\.com\/(\d+)\w*)/i) { |m| player($2) }
13
+ return string
14
+ end
15
+
16
+ private
17
+
18
+ def player(token)
19
+ %Q{<iframe src="#{@protocol}://player.vimeo.com/video/#{token}" width="#{@width}" height="#{@height}" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>}
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ class Embedda
2
+ module Filters
3
+ class Youtube
4
+ def initialize(protocol, width, height)
5
+ @protocol = protocol
6
+ @width = width
7
+ @height = height
8
+ end
9
+
10
+ def process(string)
11
+ string.gsub!(/(<a[^>]*?youtube\.com\/watch\?v=([a-zA-Z0-9\-\_]+).*?<\/a>)/i) { |m| player($2) }
12
+ string.gsub!(/([http|https]+:\/\/(?:www\.)?youtube\.com\/watch\?v=([a-zA-Z0-9\-\_]+)\w*)/i) { |m| player($2) }
13
+ return string
14
+ end
15
+
16
+ private
17
+
18
+ def player(token)
19
+ %Q{<iframe width="#{@width}" height="#{@height}" src="#{@protocol}://www.youtube.com/embed/#{token}" frameborder="0" allowfullscreen></iframe>}
20
+ end
21
+ end
22
+ end
23
+ end
24
+
data/spec/embedda_spec.rb CHANGED
@@ -6,7 +6,8 @@ describe Embedda do
6
6
 
7
7
  context "Youtube-link" do
8
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
+ let(:https_embed_string) { embed_string.gsub("http", "https") }
10
+ let(:horizontal_embed_string) { embed_string.gsub("560", "200").gsub("315", "400") }
10
11
 
11
12
  it "should embed when text have a link" do
12
13
  @story = "http://www.youtube.com/watch?v=dQw4w9WgXcQ"
@@ -55,11 +56,18 @@ describe Embedda do
55
56
  @story = "http://www.youtube.com/watch?v=dQw4w9WgXcQ"
56
57
  expect(described_class.new(@story, :ssl => true).embed).to eq(https_embed_string)
57
58
  end
59
+
60
+ it 'outputs link with proper width and height' do
61
+ @story = "http://www.youtube.com/watch?v=dQw4w9WgXcQ"
62
+ embedda = described_class.new(@story, :video_width => 200, :video_height => 400).embed
63
+ expect(embedda).to eq(horizontal_embed_string)
64
+ end
58
65
  end
59
66
 
60
67
  context "Vimeo-link" do
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>' }
68
+ let(:embed_string) { '<iframe src="http://player.vimeo.com/video/20241459" width="560" height="315" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>' }
69
+ let(:https_embed_string) { embed_string.gsub("http", "https") }
70
+ let(:horizontal_embed_string) { embed_string.gsub("560", "200").gsub("315", "400") }
63
71
 
64
72
  it "should embed when text have a link" do
65
73
  @story = "http://vimeo.com/20241459"
@@ -97,6 +105,12 @@ describe Embedda do
97
105
  @story = "http://vimeo.com/20241459"
98
106
  expect(described_class.new(@story, :ssl => true).embed).to eq(https_embed_string)
99
107
  end
108
+
109
+ it 'outputs link with proper width and height' do
110
+ @story = "http://vimeo.com/20241459"
111
+ embedda = described_class.new(@story, :video_width => 200, :video_height => 400).embed
112
+ expect(embedda).to eq(horizontal_embed_string)
113
+ end
100
114
  end
101
115
 
102
116
  context "Soundcloud-link" do
@@ -129,7 +143,7 @@ describe Embedda do
129
143
  context "All embeds in one string" do
130
144
  let(:youtube) { '<iframe width="560" height="315" src="http://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen></iframe>' }
131
145
  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>' }
146
+ let(:vimeo) { '<iframe src="http://player.vimeo.com/video/20241459" width="560" height="315" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>' }
133
147
  let(:vimeo_link) { 'http://vimeo.com/20241459' }
134
148
  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&amp;auto_play=false&amp;show_artwork=false"></iframe>' }
135
149
  let(:soundcloud_link) { 'https://soundcloud.com/flume-1/flume-left-alone-bobby-tank' }
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
4
+ version: 0.0.5
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-08-06 00:00:00.000000000 Z
13
+ date: 2013-08-29 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:
@@ -29,6 +29,10 @@ files:
29
29
  - embedda.gemspec
30
30
  - lib/embedda.rb
31
31
  - lib/embedda/core_ext.rb
32
+ - lib/embedda/filters/provider.rb
33
+ - lib/embedda/filters/soundcloud.rb
34
+ - lib/embedda/filters/vimeo.rb
35
+ - lib/embedda/filters/youtube.rb
32
36
  - spec/embedda_core_ext_spec.rb
33
37
  - spec/embedda_spec.rb
34
38
  - spec/spec_helper.rb