Skip to content
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: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source :rubygems
source 'https://rubygems.org'

gem 'addressable', :require => 'addressable/uri' # for unicode URIs
gem 'activesupport'
Expand Down
72 changes: 38 additions & 34 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
GEM
remote: http://rubygems.org/
remote: https://rubygems.org/
specs:
RedCloth (4.2.7)
activemodel (3.0.7)
activesupport (= 3.0.7)
builder (~> 2.1.2)
i18n (~> 0.5.0)
activerecord (3.0.7)
activemodel (= 3.0.7)
activesupport (= 3.0.7)
arel (~> 2.0.2)
tzinfo (~> 0.3.23)
activesupport (3.0.7)
addressable (2.2.6)
arel (2.0.10)
builder (2.1.2)
diff-lcs (1.1.2)
RedCloth (4.2.9)
activemodel (3.2.13)
activesupport (= 3.2.13)
builder (~> 3.0.0)
activerecord (3.2.13)
activemodel (= 3.2.13)
activesupport (= 3.2.13)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activesupport (3.2.13)
i18n (= 0.6.1)
multi_json (~> 1.0)
addressable (2.3.4)
arel (3.0.2)
builder (3.0.4)
diff-lcs (1.2.4)
git (1.2.5)
httpi (0.9.4)
pyu-ntlm-http (>= 0.1.3.1)
httpi (2.0.2)
rack
i18n (0.5.0)
jeweler (1.6.0)
bundler (~> 1.0.0)
i18n (0.6.1)
jeweler (1.8.4)
bundler (~> 1.0)
git (>= 1.2.5)
rake
pyu-ntlm-http (0.1.3.1)
rack (1.3.0)
rake (0.9.0)
rspec (2.6.0)
rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0)
rspec-mocks (~> 2.6.0)
rspec-core (2.6.3)
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
tzinfo (0.3.27)
yard (0.7.1)
rdoc
json (1.7.7)
multi_json (1.7.2)
rack (1.5.2)
rake (10.0.4)
rdoc (4.0.1)
json (~> 1.4)
rspec (2.13.0)
rspec-core (~> 2.13.0)
rspec-expectations (~> 2.13.0)
rspec-mocks (~> 2.13.0)
rspec-core (2.13.1)
rspec-expectations (2.13.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.13.1)
tzinfo (0.3.37)
yard (0.8.6.1)

PLATFORMS
ruby
Expand Down
37 changes: 19 additions & 18 deletions lib/url_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,49 +116,50 @@ class UrlValidator < ActiveModel::EachValidator
:insufficient_storage => 507,
:not_extended => 510
}


# @private
def validate_each(record, attribute, value)
return if options[:allow_nil] and value.nil?
return if options[:allow_blank] and value.blank?

uri = Addressable::URI.parse(value)
if uri.scheme.nil? and options[:default_scheme] then
uri = Addressable::URI.parse("#{options[:default_scheme]}://#{value}")

if uri = Addressable::URI.parse(value)
if uri.scheme.nil? and options[:default_scheme] then
uri = Addressable::URI.parse("#{options[:default_scheme]}://#{value}")
end

record.errors.add(attribute, options[:invalid_url_message] || :invalid_url) unless url_format_valid?(uri, options)
record.errors.add(attribute, options[:url_not_accessible_message] || :url_not_accessible) unless response = url_accessible?(uri, options)
record.errors.add(attribute, options[:url_invalid_response_message] || :url_invalid_response) unless url_response_valid?(response, record, attribute, value, options)
end

record.errors.add(attribute, options[:invalid_url_message] || :invalid_url) unless url_format_valid?(uri, options)
record.errors.add(attribute, options[:url_not_accessible_message] || :url_not_accessible) unless response = url_accessible?(uri, options)
record.errors.add(attribute, options[:url_invalid_response_message] || :url_invalid_response) unless url_response_valid?(response, record, attribute, value, options)
end

private

def url_format_valid?(uri, options)
return false unless Array.wrap(options[:scheme] || %w( http https )).include?(uri.scheme)

case uri.scheme
when 'http', 'https'
return http_url_format_valid?(uri)
else
return true
end
end

def http_url_format_valid?(uri)
uri.host.present? and not uri.path.nil?
end

def url_accessible?(uri, options)
return true unless options[:check_host] or options[:check_path]

check_host = options[:check_host]
check_host ||= %w( http https ) if options[:check_path]
if (schemes = Array.wrap(check_host)) and schemes.all? { |scheme| scheme.kind_of?(String) } then
return true unless schemes.include?(uri.scheme)
end

case uri.scheme
when 'http', 'https'
return http_url_accessible?(uri, options)
Expand All @@ -174,7 +175,7 @@ def http_url_accessible?(uri, options)
rescue
return false
end

def url_response_valid?(response, record, attribute, value, options)
return true unless response.kind_of?(HTTPI::Response) and options[:check_path]
options[:response_callback].call(response, record, attribute, value) if options[:response_callback].respond_to?(:call)
Expand Down