Skip to content

Commit bb44f81

Browse files
committed
More precisely check if the content is URL
1 parent e002662 commit bb44f81

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

lib/imgkit/source.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1+
require 'uri'
2+
13
class IMGKit
2-
4+
35
class Source
4-
6+
URL_REGEX = /\A#{URI.regexp(['http', 'https'])}\z/
7+
58
def initialize(url_file_or_html)
69
@source = url_file_or_html
710
end
8-
11+
912
def url?
10-
@source.is_a?(String) && @source.match(/\Ahttp/)
13+
@source.is_a?(String) && @source.match(URL_REGEX)
1114
end
12-
15+
1316
def file?
1417
@source.kind_of?(File) || @source.kind_of?(Tempfile)
1518
end
16-
19+
1720
def html?
1821
!(url? || file?)
1922
end
20-
23+
2124
def to_s
2225
file? ? @source.path : @source
2326
end
24-
27+
2528
end
26-
29+
2730
end

spec/source_spec.rb

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
require 'spec_helper'
22

33
describe IMGKit::Source do
4-
4+
55
describe "#url?" do
66
it "should return true if passed a url like string" do
77
source = IMGKit::Source.new('http://google.com')
88
source.should be_url
99
end
10-
10+
1111
it "should return false if passed a file" do
1212
source = IMGKit::Source.new(File.new(__FILE__))
1313
source.should_not be_url
1414
end
15-
15+
1616
it "should return false if passed HTML" do
1717
source = IMGKit::Source.new('<blink>Oh Hai!</blink>')
1818
source.should_not be_url
@@ -22,8 +22,13 @@
2222
source = IMGKit::Source.new("<blink>Oh Hai!</blink>\nhttp://google.com")
2323
source.should_not be_url
2424
end
25+
26+
it "should return false if passed string starts with http but is not a url" do
27+
source = IMGKit::Source.new('http')
28+
source.should_not be_url
29+
end
2530
end
26-
31+
2732
describe "#file?" do
2833
it "should return true if passed a file" do
2934
source = IMGKit::Source.new(File.new(__FILE__))
@@ -34,50 +39,50 @@
3439
source = IMGKit::Source.new(Tempfile.new 'temp_file')
3540
source.should be_file
3641
end
37-
42+
3843
it "should return false if passed a url like string" do
3944
source = IMGKit::Source.new('http://google.com')
4045
source.should_not be_file
4146
end
42-
47+
4348
it "should return false if passed HTML" do
4449
source = IMGKit::Source.new('<blink>Oh Hai!</blink>')
4550
source.should_not be_file
4651
end
4752
end
48-
53+
4954
describe "#html?" do
5055
it "should return true if passed HTML" do
5156
source = IMGKit::Source.new('<blink>Oh Hai!</blink>')
5257
source.should be_html
5358
end
54-
59+
5560
it "should return false if passed a file" do
5661
source = IMGKit::Source.new(File.new(__FILE__))
5762
source.should_not be_html
5863
end
59-
64+
6065
it "should return false if passed a url like string" do
6166
source = IMGKit::Source.new('http://google.com')
6267
source.should_not be_html
6368
end
6469
end
65-
70+
6671
describe "#to_s" do
6772
it "should return the HTML if passed HTML" do
6873
source = IMGKit::Source.new('<blink>Oh Hai!</blink>')
6974
source.to_s.should == '<blink>Oh Hai!</blink>'
7075
end
71-
76+
7277
it "should return a path if passed a file" do
7378
source = IMGKit::Source.new(File.new(__FILE__))
7479
source.to_s.should == __FILE__
7580
end
76-
81+
7782
it "should return the url if passed a url like string" do
7883
source = IMGKit::Source.new('http://google.com')
7984
source.to_s.should == 'http://google.com'
8085
end
8186
end
82-
87+
8388
end

0 commit comments

Comments
 (0)