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" 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