Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More precisely check if the content is URL #102

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
before_install:
- gem install bundler
rvm:
- "2.1.5"
- "2.1.4"
Expand Down
21 changes: 12 additions & 9 deletions lib/imgkit/source.rb
Original file line number Diff line number Diff line change
@@ -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
31 changes: 18 additions & 13 deletions spec/source_spec.rb
Original file line number Diff line number Diff line change
@@ -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('<blink>Oh Hai!</blink>')
source.should_not be_url
Expand All @@ -22,8 +22,13 @@
source = IMGKit::Source.new("<blink>Oh Hai!</blink>\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__))
Expand All @@ -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('<blink>Oh Hai!</blink>')
source.should_not be_file
end
end

describe "#html?" do
it "should return true if passed HTML" do
source = IMGKit::Source.new('<blink>Oh Hai!</blink>')
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('<blink>Oh Hai!</blink>')
source.to_s.should == '<blink>Oh Hai!</blink>'
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