From bb44f81f7de39a9cdbde7a7dd412fc8415324464 Mon Sep 17 00:00:00 2001 From: Dalibor Nasevic Date: Tue, 18 Apr 2017 14:43:01 +0200 Subject: [PATCH 1/2] More precisely check if the content is URL --- lib/imgkit/source.rb | 21 ++++++++++++--------- spec/source_spec.rb | 31 ++++++++++++++++++------------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/lib/imgkit/source.rb b/lib/imgkit/source.rb index 9340036..393e680 100644 --- a/lib/imgkit/source.rb +++ b/lib/imgkit/source.rb @@ -1,27 +1,30 @@ +require 'uri' + class IMGKit - + class Source - + URL_REGEX = /\A#{URI.regexp(['http', 'https'])}\z/ + def initialize(url_file_or_html) @source = url_file_or_html end - + def url? - @source.is_a?(String) && @source.match(/\Ahttp/) + @source.is_a?(String) && @source.match(URL_REGEX) end - + def file? @source.kind_of?(File) || @source.kind_of?(Tempfile) end - + def html? !(url? || file?) end - + def to_s file? ? @source.path : @source end - + end - + end diff --git a/spec/source_spec.rb b/spec/source_spec.rb index 4234fb4..17d4c26 100644 --- a/spec/source_spec.rb +++ b/spec/source_spec.rb @@ -1,18 +1,18 @@ require 'spec_helper' describe IMGKit::Source do - + describe "#url?" do it "should return true if passed a url like string" do source = IMGKit::Source.new('http://google.com') source.should be_url end - + it "should return false if passed a file" do source = IMGKit::Source.new(File.new(__FILE__)) source.should_not be_url end - + it "should return false if passed HTML" do source = IMGKit::Source.new('Oh Hai!') source.should_not be_url @@ -22,8 +22,13 @@ source = IMGKit::Source.new("Oh Hai!\nhttp://google.com") source.should_not be_url end + + it "should return false if passed string starts with http but is not a url" do + source = IMGKit::Source.new('http') + source.should_not be_url + end end - + describe "#file?" do it "should return true if passed a file" do source = IMGKit::Source.new(File.new(__FILE__)) @@ -34,50 +39,50 @@ source = IMGKit::Source.new(Tempfile.new 'temp_file') source.should be_file end - + it "should return false if passed a url like string" do source = IMGKit::Source.new('http://google.com') source.should_not be_file end - + it "should return false if passed HTML" do source = IMGKit::Source.new('Oh Hai!') source.should_not be_file end end - + describe "#html?" do it "should return true if passed HTML" do source = IMGKit::Source.new('Oh Hai!') source.should be_html end - + it "should return false if passed a file" do source = IMGKit::Source.new(File.new(__FILE__)) source.should_not be_html end - + it "should return false if passed a url like string" do source = IMGKit::Source.new('http://google.com') source.should_not be_html end end - + describe "#to_s" do it "should return the HTML if passed HTML" do source = IMGKit::Source.new('Oh Hai!') source.to_s.should == 'Oh Hai!' end - + it "should return a path if passed a file" do source = IMGKit::Source.new(File.new(__FILE__)) source.to_s.should == __FILE__ end - + it "should return the url if passed a url like string" do source = IMGKit::Source.new('http://google.com') source.to_s.should == 'http://google.com' end end - + end From 6cd9c10d9c03aca6e73ace5dda78138269147ac2 Mon Sep 17 00:00:00 2001 From: Dalibor Nasevic Date: Tue, 18 Apr 2017 15:02:13 +0200 Subject: [PATCH 2/2] Install most recent bundler to fix build on Ruby 1.9.3 --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index a484482..a3dc865 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,5 @@ +before_install: + - gem install bundler rvm: - "2.1.5" - "2.1.4"