url_to_media_tag 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -19,8 +19,25 @@ Usage
19
19
 
20
20
  UrlToMediaTag.convert('http://www.youtube.com/watch?v=kW-dS4otEZU') # -> <iframe ...>
21
21
  UrlToMediaTag.convert(url, :width => 480, :height => 320) # -> <iframe ...>
22
+ UrlToMediaTag.convert('http://foo.com/xxx.jpg') # -> <img
22
23
  UrlToMediaTag.convert('no-url') # -> nil
23
24
 
25
+ ### settings
26
+
27
+ More specific settings overwrite others
28
+
29
+ UrlToMediaTag.convert(url, :width => '400px', :settings => {:image => {:width => '100%'}, :vimeo => {:show_title => true})
30
+ any -> :width => '400px'
31
+ image -> :width => '100%'
32
+ vimeo -> :width => '400px', :show_title => true
33
+
34
+ Videos get default settings
35
+
36
+ :width => 640,
37
+ :height => 480,
38
+ :class => "url-to-media-tag-video",
39
+ :frameborder => 0
40
+
24
41
  ### Find
25
42
 
26
43
  urls = text.scan(%r{https?://[^\s]*})
@@ -35,7 +52,6 @@ Alternative
35
52
 
36
53
  TODO
37
54
  ====
38
- - support images
39
55
  - let users choose which providers to convert
40
56
 
41
57
  Author
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -1,13 +1,15 @@
1
1
  module UrlToMediaTag
2
2
  VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
3
3
 
4
- DEFAULTS = {
4
+ VIDEO_DEFAULTS = {
5
5
  :width => 640,
6
- :height => 480
6
+ :height => 480,
7
+ :class => "url-to-media-tag-video",
8
+ :frameborder => 0
7
9
  }
8
10
 
9
11
  def self.video_iframe(url, options)
10
- options = {:src => url, :class => "url-to-media-tag-video", :frameborder => 0}.merge(options)
12
+ options = {:src => url}.merge(VIDEO_DEFAULTS).merge(options)
11
13
  tag(:iframe, options)
12
14
  end
13
15
 
@@ -16,8 +18,18 @@ module UrlToMediaTag
16
18
  %{<#{name} #{options}></#{name}>}
17
19
  end
18
20
 
21
+ # :video, :x => 1, :settings => {:video => {:width => 300}}
22
+ # --> {:x => 1, :width => 300}
23
+ def self.merge_settings(*args)
24
+ options = args.pop.dup
25
+ raise unless options.kind_of?(Hash)
26
+ settings = options.delete(:settings) || {}
27
+ args.each{|type| options.merge!(settings[type] || {}) }
28
+ options
29
+ end
30
+
19
31
  def self.convert(url, options={})
20
- options = DEFAULTS.merge(options)
32
+ options = options.dup
21
33
 
22
34
  # prevent any kind of html or xss
23
35
  return if url.include?('>') or url.include?('<')
@@ -26,11 +38,14 @@ module UrlToMediaTag
26
38
 
27
39
  # youtube
28
40
  when /http:\/\/(www.)?youtube\.com\/watch\?v=([A-Za-z0-9._%-]*)(\&\S+)?|http:\/\/(www.)?youtu\.be\/([A-Za-z0-9._%-]*)?/
41
+ options = merge_settings(:video, :vimeo, options)
29
42
  youtube_id = $2 || $5
30
43
  video_iframe "http://www.youtube.com/embed/#{youtube_id}", options
31
44
 
32
45
  # vimeo
33
46
  when /http:\/\/(www.)?vimeo\.com\/([A-Za-z0-9._%-]*)((\?|#)\S+)?/
47
+ options = merge_settings(:video, :vimeo, options)
48
+
34
49
  vimeo_id = $2
35
50
  show_title = "title=0" unless options.delete(:show_title)
36
51
  show_byline = "byline=0" unless options.delete(:show_byline)
@@ -42,6 +57,7 @@ module UrlToMediaTag
42
57
 
43
58
  # image
44
59
  when /https?:\/\/\S+\.(jpe?g|gif|png|bmp|tif)(\?\S+)?/i
60
+ options = merge_settings(:image, options)
45
61
  tag(:img, options.merge(:src => $&))
46
62
  end
47
63
 
@@ -17,15 +17,15 @@ describe UrlToMediaTag do
17
17
  end
18
18
 
19
19
  it "converts images" do
20
- UrlToMediaTag.convert('http://foo.com/foo.jpg').should == "<img height=\"480\" src=\"http://foo.com/foo.jpg\" width=\"640\"></img>"
21
- UrlToMediaTag.convert('http://foo.com/foo.gif').should == "<img height=\"480\" src=\"http://foo.com/foo.gif\" width=\"640\"></img>"
22
- UrlToMediaTag.convert('http://foo.com/foo.png').should == "<img height=\"480\" src=\"http://foo.com/foo.png\" width=\"640\"></img>"
23
- UrlToMediaTag.convert('http://foo.com/foo.jpeg').should == "<img height=\"480\" src=\"http://foo.com/foo.jpeg\" width=\"640\"></img>"
24
- UrlToMediaTag.convert('http://foo.com/foo.JPG').should == "<img height=\"480\" src=\"http://foo.com/foo.JPG\" width=\"640\"></img>"
20
+ UrlToMediaTag.convert('http://foo.com/foo.jpg').should == "<img src=\"http://foo.com/foo.jpg\"></img>"
21
+ UrlToMediaTag.convert('http://foo.com/foo.gif').should == "<img src=\"http://foo.com/foo.gif\"></img>"
22
+ UrlToMediaTag.convert('http://foo.com/foo.png').should == "<img src=\"http://foo.com/foo.png\"></img>"
23
+ UrlToMediaTag.convert('http://foo.com/foo.jpeg').should == "<img src=\"http://foo.com/foo.jpeg\"></img>"
24
+ UrlToMediaTag.convert('http://foo.com/foo.JPG').should == "<img src=\"http://foo.com/foo.JPG\"></img>"
25
25
  end
26
26
 
27
27
  it "converts images with ?" do
28
- UrlToMediaTag.convert('http://foo.com/foo.jpg?foo=bar').should == "<img height=\"480\" src=\"http://foo.com/foo.jpg?foo=bar\" width=\"640\"></img>"
28
+ UrlToMediaTag.convert('http://foo.com/foo.jpg?foo=bar').should == "<img src=\"http://foo.com/foo.jpg?foo=bar\"></img>"
29
29
  end
30
30
 
31
31
  it "does not convert unknown" do
@@ -40,5 +40,32 @@ describe UrlToMediaTag do
40
40
  UrlToMediaTag.convert('http://vimeo.com/26881896<').should == nil
41
41
  UrlToMediaTag.convert('http://vimeo.com/26881896>').should == nil
42
42
  end
43
+
44
+ describe 'settings' do
45
+ it "uses directly given settings" do
46
+ UrlToMediaTag.convert('http://foo.com/foo.jpg', :foo => 'bar').should == "<img foo=\"bar\" src=\"http://foo.com/foo.jpg\"></img>"
47
+ end
48
+
49
+ it "uses indirectly given settings" do
50
+ UrlToMediaTag.convert('http://foo.com/foo.jpg', :settings => {:image => {:foo => 'bar'}}).should == "<img foo=\"bar\" src=\"http://foo.com/foo.jpg\"></img>"
51
+ end
52
+
53
+ it "does not use not-matching settings" do
54
+ UrlToMediaTag.convert('http://foo.com/foo.jpg', :settings => {:video => {:foo => 'bar'}}).should == "<img src=\"http://foo.com/foo.jpg\"></img>"
55
+ end
56
+
57
+ it "users more precise settings" do
58
+ UrlToMediaTag.convert('http://vimeo.com/26881896', :settings => {:video => {:foo => 'bar'}, :vimeo => {:foo => 'baz'}}).should include('foo="baz"')
59
+ end
60
+
61
+ it "overwrites default settings" do
62
+ UrlToMediaTag.convert('http://foo.com/foo.jpg', :foo => 'bar', :settings => {:image => {:foo => 'baz'}}).should include('foo="baz"')
63
+ end
64
+
65
+ it "can take options from settings" do
66
+ UrlToMediaTag.convert('http://vimeo.com/26881896').should include('title=0')
67
+ UrlToMediaTag.convert('http://vimeo.com/26881896', :settings => {:vimeo => {:show_title => true}}).should_not include('title=0')
68
+ end
69
+ end
43
70
  end
44
71
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{url_to_media_tag}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Grosser"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: url_to_media_tag
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Grosser