pax_global_header 0000666 0000000 0000000 00000000064 12553403347 0014520 g ustar 00root root 0000000 0000000 52 comment=c26f7a1b5bf00bbcdadc825e69a9c401a94413ec
validates_url-1.0.2/ 0000775 0000000 0000000 00000000000 12553403347 0014356 5 ustar 00root root 0000000 0000000 validates_url-1.0.2/.gitignore 0000664 0000000 0000000 00000000056 12553403347 0016347 0 ustar 00root root 0000000 0000000 coverage
pkg
rdoc
test/tmp
tmp
.idea
.DS_Store validates_url-1.0.2/.rspec 0000664 0000000 0000000 00000000037 12553403347 0015473 0 ustar 00root root 0000000 0000000 --colour
--format documentation validates_url-1.0.2/LICENSE.md 0000664 0000000 0000000 00000002127 12553403347 0015764 0 ustar 00root root 0000000 0000000 The MIT License (MIT)
Copyright (c) 2010 [PerfectLine](http://www.perfectline.co), LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. validates_url-1.0.2/README.md 0000664 0000000 0000000 00000002742 12553403347 0015642 0 ustar 00root root 0000000 0000000 # Validates URL
This gem adds the capability of validating URLs to ActiveRecord and ActiveModel (Rails 3).
## Installation
```
# add this to your Gemfile
gem "validate_url"
# and run
sudo gem install validate_url
```
## Usage
### With ActiveRecord
```ruby
class Pony < ActiveRecord::Base
# standard validation
validates :homepage, :url => true
# with allow_nil
validates :homepage, :url => {:allow_nil => true}
# with allow_blank
validates :homepage, :url => {:allow_blank => true}
# without local hostnames
validates :homepage, :url => {:no_local => true}
end
```
### With ActiveModel
```ruby
class Unicorn
include ActiveModel::Validations
attr_accessor :homepage
# with legacy syntax (the syntax above works also)
validates_url :homepage, :allow_blank => true
end
```
### I18n
The default error message `is not valid url`.
You can pass the `:message => "my custom error"` option to your validation to define your own, custom message.
## Contributing
Big thanks to Tanel Suurhans, Tarmo Lehtpuu, Steve Smith and all the [contributors](https://github.com/perfectline/validates_url/contributors)! We appreciate all your work on new features and bugfixes.
## Credits
Validates URL is created and maintained by [PerfectLine](http://www.perfectline.co), LLC.
## License
Validates URL is Copyright © 2010-2014 [PerfectLine](http://www.perfectline.co), LLC. It is free software, and may be
redistributed under the terms specified in the LICENSE file. validates_url-1.0.2/Rakefile 0000664 0000000 0000000 00000002420 12553403347 0016021 0 ustar 00root root 0000000 0000000 require 'rake'
require 'rdoc/task'
require 'rake/clean'
require 'rspec/core/rake_task'
require 'jeweler'
desc 'Default: run unit tests.'
task :default => :test
Jeweler::Tasks.new do |jewel|
jewel.name = 'validate_url'
jewel.summary = 'Library for validating urls in Rails.'
jewel.email = ['tanel.suurhans@perfectline.co', 'tarmo.lehtpuu@perfectline.co', 'vladimir.krylov@perfectline.co']
jewel.homepage = 'http://github.com/perfectline/validates_url/tree/master'
jewel.description = 'Library for validating urls in Rails.'
jewel.authors = ["Tanel Suurhans", "Tarmo Lehtpuu", "Vladimir Krylov"]
jewel.files = FileList["lib/**/*.rb", "lib/locale/*.yml", "*.rb", "MIT-LICENCE", "README.markdown"]
jewel.add_dependency 'activemodel', '>= 3.0.0'
jewel.add_dependency 'addressable'
jewel.add_development_dependency 'rspec'
jewel.add_development_dependency 'diff-lcs', '>= 1.1.2'
end
desc 'Generate documentation plugin.'
RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'ValidatesUrl'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README.markdown')
rdoc.rdoc_files.include('lib/**/*.rb')
end
desc 'Run all rspec tests'
RSpec::Core::RakeTask.new(:test)
validates_url-1.0.2/VERSION.yml 0000664 0000000 0000000 00000000053 12553403347 0016224 0 ustar 00root root 0000000 0000000 ---
:major: 1
:minor: 0
:patch: 2
:build:
validates_url-1.0.2/init.rb 0000664 0000000 0000000 00000000026 12553403347 0015644 0 ustar 00root root 0000000 0000000 require 'validate_url' validates_url-1.0.2/install.rb 0000664 0000000 0000000 00000000103 12553403347 0016343 0 ustar 00root root 0000000 0000000 puts IO.read(File.join(File.dirname(__FILE__), 'README.markdown'))
validates_url-1.0.2/lib/ 0000775 0000000 0000000 00000000000 12553403347 0015124 5 ustar 00root root 0000000 0000000 validates_url-1.0.2/lib/locale/ 0000775 0000000 0000000 00000000000 12553403347 0016363 5 ustar 00root root 0000000 0000000 validates_url-1.0.2/lib/locale/de.yml 0000664 0000000 0000000 00000000076 12553403347 0017501 0 ustar 00root root 0000000 0000000 de:
errors:
messages:
url: ist keine gültige URL
validates_url-1.0.2/lib/locale/en.yml 0000664 0000000 0000000 00000000072 12553403347 0017507 0 ustar 00root root 0000000 0000000 en:
errors:
messages:
url: is not a valid URL
validates_url-1.0.2/lib/locale/it.yml 0000664 0000000 0000000 00000000075 12553403347 0017524 0 ustar 00root root 0000000 0000000 it:
errors:
messages:
url: non è un URL valido.
validates_url-1.0.2/lib/locale/ja.yml 0000664 0000000 0000000 00000000100 12553403347 0017467 0 ustar 00root root 0000000 0000000 ja:
errors:
messages:
url: は不正なURLです。
validates_url-1.0.2/lib/locale/pt-BR.yml 0000664 0000000 0000000 00000000102 12553403347 0020023 0 ustar 00root root 0000000 0000000 pt-BR:
errors:
messages:
url: não é uma URL válida
validates_url-1.0.2/lib/locale/tr.yml 0000664 0000000 0000000 00000000077 12553403347 0017537 0 ustar 00root root 0000000 0000000 tr:
errors:
messages:
url: Geçerli bir URL değil
validates_url-1.0.2/lib/validate_url.rb 0000664 0000000 0000000 00000004030 12553403347 0020121 0 ustar 00root root 0000000 0000000 require 'addressable/uri'
require 'active_model'
require 'active_support/i18n'
I18n.load_path << File.dirname(__FILE__) + '/locale/en.yml'
I18n.load_path << File.dirname(__FILE__) + '/locale/tr.yml'
I18n.load_path << File.dirname(__FILE__) + '/locale/ja.yml'
module ActiveModel
module Validations
class UrlValidator < ActiveModel::EachValidator
def initialize(options)
options.reverse_merge!(:schemes => %w(http https))
options.reverse_merge!(:message => :url)
options.reverse_merge!(:no_local => false)
super(options)
end
def validate_each(record, attribute, value)
schemes = [*options.fetch(:schemes)].map(&:to_s)
begin
uri = Addressable::URI.parse(value)
unless uri && uri.host && schemes.include?(uri.scheme) && (!options.fetch(:no_local) || uri.host.include?('.'))
record.errors.add(attribute, options.fetch(:message), :value => value)
end
rescue Addressable::URI::InvalidURIError
record.errors.add(attribute, options.fetch(:message), :value => value)
end
end
end
module ClassMethods
# Validates whether the value of the specified attribute is valid url.
#
# class Unicorn
# include ActiveModel::Validations
# attr_accessor :homepage, :ftpsite
# validates_url :homepage, :allow_blank => true
# validates_url :ftpsite, :schemes => ['ftp']
# end
# Configuration options:
# * :message - A custom error message (default is: "is not a valid URL").
# * :allow_nil - If set to true, skips this validation if the attribute is +nil+ (default is +false+).
# * :allow_blank - If set to true, skips this validation if the attribute is blank (default is +false+).
# * :schemes - Array of URI schemes to validate against. (default is +['http', 'https']+)
def validates_url(*attr_names)
validates_with UrlValidator, _merge_attributes(attr_names)
end
end
end
end
validates_url-1.0.2/spec/ 0000775 0000000 0000000 00000000000 12553403347 0015310 5 ustar 00root root 0000000 0000000 validates_url-1.0.2/spec/resources/ 0000775 0000000 0000000 00000000000 12553403347 0017322 5 ustar 00root root 0000000 0000000 validates_url-1.0.2/spec/resources/user.rb 0000664 0000000 0000000 00000000161 12553403347 0020623 0 ustar 00root root 0000000 0000000 class User
include ActiveModel::Validations
attr_accessor :homepage
validates :homepage, :url => true
end validates_url-1.0.2/spec/resources/user_with_ar.rb 0000664 0000000 0000000 00000000152 12553403347 0022340 0 ustar 00root root 0000000 0000000 class UserWithAr < ActiveRecord::Base
self.table_name = "users"
validates :homepage, :url => true
end validates_url-1.0.2/spec/resources/user_with_ar_legacy.rb 0000664 0000000 0000000 00000000146 12553403347 0023667 0 ustar 00root root 0000000 0000000 class UserWithArLegacy < ActiveRecord::Base
self.table_name = "users"
validates_url :homepage
end validates_url-1.0.2/spec/resources/user_with_blank.rb 0000664 0000000 0000000 00000000260 12553403347 0023025 0 ustar 00root root 0000000 0000000 require 'active_model/validations'
class UserWithBlank
include ActiveModel::Validations
attr_accessor :homepage
validates :homepage, :url => {:allow_blank => true}
end validates_url-1.0.2/spec/resources/user_with_custom_message.rb 0000664 0000000 0000000 00000000221 12553403347 0024751 0 ustar 00root root 0000000 0000000 class UserWithCustomMessage
include ActiveModel::Validations
attr_accessor :homepage
validates :homepage, :url => {message: "wrong"}
end
validates_url-1.0.2/spec/resources/user_with_custom_scheme.rb 0000664 0000000 0000000 00000000224 12553403347 0024574 0 ustar 00root root 0000000 0000000 class UserWithCustomScheme
include ActiveModel::Validations
attr_accessor :homepage
validates :homepage, :url => { :schemes => ['ftp'] }
end validates_url-1.0.2/spec/resources/user_with_legacy_syntax.rb 0000664 0000000 0000000 00000000261 12553403347 0024611 0 ustar 00root root 0000000 0000000 require 'active_model/validations'
class UserWithLegacySyntax
include ActiveModel::Validations
attr_accessor :homepage
validates_url :homepage, :allow_blank => true
end validates_url-1.0.2/spec/resources/user_with_nil.rb 0000664 0000000 0000000 00000000254 12553403347 0022523 0 ustar 00root root 0000000 0000000 require 'active_model/validations'
class UserWithNil
include ActiveModel::Validations
attr_accessor :homepage
validates :homepage, :url => {:allow_nil => true}
end validates_url-1.0.2/spec/resources/user_with_no_local.rb 0000664 0000000 0000000 00000000257 12553403347 0023532 0 ustar 00root root 0000000 0000000 require 'active_model/validations'
class UserWithNoLocal
include ActiveModel::Validations
attr_accessor :homepage
validates :homepage, :url => {:no_local => true}
end validates_url-1.0.2/spec/spec_helper.rb 0000664 0000000 0000000 00000002025 12553403347 0020125 0 ustar 00root root 0000000 0000000 $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/resources')
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'rspec'
require 'sqlite3'
require 'active_record'
require 'active_record/base'
require 'active_record/migration'
ActiveRecord::Migration.verbose = false
ActiveRecord::Base.establish_connection(
"adapter" => "sqlite3",
"database" => ":memory:"
)
require File.join(File.dirname(__FILE__), '..', 'init')
autoload :User, 'resources/user'
autoload :UserWithNil, 'resources/user_with_nil'
autoload :UserWithBlank, 'resources/user_with_blank'
autoload :UserWithLegacySyntax, 'resources/user_with_legacy_syntax'
autoload :UserWithAr, 'resources/user_with_ar'
autoload :UserWithArLegacy, 'resources/user_with_ar_legacy'
autoload :UserWithCustomScheme, 'resources/user_with_custom_scheme'
autoload :UserWithCustomMessage, 'resources/user_with_custom_message'
autoload :UserWithNoLocal, 'resources/user_with_no_local'
validates_url-1.0.2/spec/validate_url_spec.rb 0000664 0000000 0000000 00000012567 12553403347 0021335 0 ustar 00root root 0000000 0000000 # encoding: utf-8
require 'spec_helper'
describe "URL validation" do
before(:all) do
ActiveRecord::Schema.define(:version => 1) do
create_table :users, :force => true do |t|
t.column :homepage, :string
end
end
end
after(:all) do
ActiveRecord::Base.connection.drop_table(:users)
end
context "with regular validator" do
before do
@user = User.new
end
it "should not allow nil as url" do
@user.homepage = nil
@user.should_not be_valid
end
it "should not allow blank as url" do
@user.homepage = ""
@user.should_not be_valid
end
it "should not allow an url without scheme" do
@user.homepage = "www.example.com"
@user.should_not be_valid
end
it "should allow an url with http" do
@user.homepage = "http://localhost"
@user.should be_valid
end
it "should allow an url with https" do
@user.homepage = "https://localhost"
@user.should be_valid
end
it "should not allow a url with an invalid scheme" do
@user.homepage = "ftp://localhost"
@user.should_not be_valid
end
it "should not allow a url with only a scheme" do
@user.homepage = "http://"
@user.should_not be_valid
end
it "should not allow a url without a host" do
@user.homepage = "http:/"
@user.should_not be_valid
end
it "should allow a url with an underscore" do
@user.homepage = "http://foo_bar.com"
@user.should be_valid
end
it "should return a default error message" do
@user.homepage = "invalid"
@user.valid?
@user.errors[:homepage].should == ["is not a valid URL"]
end
context "when locale is turkish" do
it "should return a Turkish default error message" do
I18n.locale = :tr
@user.homepage = "Black Tea"
@user.valid?
@user.errors[:homepage].should == ["Geçerli bir URL değil"]
end
end
context "when locale is Japanese" do
it "should return a Japanese default error message" do
I18n.locale = :ja
@user.homepage = "黒麦茶"
@user.valid?
@user.errors[:homepage].should == ["は不正なURLです。"]
end
end
end
context "with allow nil" do
before do
@user = UserWithNil.new
end
it "should allow nil as url" do
@user.homepage = nil
@user.should be_valid
end
it "should not allow blank as url" do
@user.homepage = ""
@user.should_not be_valid
end
it "should allow a valid url" do
@user.homepage = "http://www.example.com"
@user.should be_valid
end
it "should allow a url with an underscore" do
@user.homepage = "http://foo_bar.com"
@user.should be_valid
end
end
context "with allow blank" do
before do
@user = UserWithBlank.new
end
it "should allow nil as url" do
@user.homepage = nil
@user.should be_valid
end
it "should allow blank as url" do
@user.homepage = ""
@user.should be_valid
end
it "should allow a valid url" do
@user.homepage = "http://www.example.com"
@user.should be_valid
end
it "should allow a url with an underscore" do
@user.homepage = "http://foo_bar.com"
@user.should be_valid
end
end
context "with no_local" do
before do
@user = UserWithNoLocal.new
end
it "should allow a valid internet url" do
@user.homepage = "http://www.example.com"
@user.should be_valid
end
it "should not allow a local hostname" do
@user.homepage = "http://localhost"
@user.should_not be_valid
end
it "should not allow weird urls that get interpreted as local hostnames" do
@user.homepage = "http://http://example.com"
@user.should_not be_valid
end
end
context "with legacy syntax" do
before do
@user = UserWithLegacySyntax.new
end
it "should allow nil as url" do
@user.homepage = nil
@user.should be_valid
end
it "should allow blank as url" do
@user.homepage = ""
@user.should be_valid
end
it "should allow a valid url" do
@user.homepage = "http://www.example.com"
@user.should be_valid
end
it "should not allow invalid url" do
@user.homepage = "random"
@user.should_not be_valid
end
it "should allow a url with an underscore" do
@user.homepage = "http://foo_bar.com"
@user.should be_valid
end
end
context "with ActiveRecord" do
before do
@user = UserWithAr.new
end
it "should not allow invalid url" do
@user.homepage = "random"
@user.should_not be_valid
end
end
context "with ActiveRecord and legacy syntax" do
before do
@user = UserWithArLegacy.new
end
it "should not allow invalid url" do
@user.homepage = "random"
@user.should_not be_valid
end
end
context "with regular validator and custom scheme" do
before do
@user = UserWithCustomScheme.new
end
it "should allow alternative URI schemes" do
@user.homepage = "ftp://ftp.example.com"
@user.should be_valid
end
end
context "with custom message" do
before do
@user = UserWithCustomMessage.new
end
it "should use custom message" do
@user.homepage = "invalid"
@user.valid?
@user.errors[:homepage].should == ["wrong"]
end
end
end
validates_url-1.0.2/validate_url.gemspec 0000664 0000000 0000000 00000003612 12553403347 0020400 0 ustar 00root root 0000000 0000000 # Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
# stub: validate_url 1.0.2 ruby lib
Gem::Specification.new do |s|
s.name = "validate_url"
s.version = "1.0.2"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.require_paths = ["lib"]
s.authors = ["Tanel Suurhans", "Tarmo Lehtpuu", "Vladimir Krylov"]
s.date = "2015-07-21"
s.description = "Library for validating urls in Rails."
s.email = ["tanel.suurhans@perfectline.co", "tarmo.lehtpuu@perfectline.co", "vladimir.krylov@perfectline.co"]
s.extra_rdoc_files = [
"LICENSE.md",
"README.md"
]
s.files = [
"init.rb",
"install.rb",
"lib/locale/de.yml",
"lib/locale/en.yml",
"lib/locale/it.yml",
"lib/locale/ja.yml",
"lib/locale/pt-BR.yml",
"lib/locale/tr.yml",
"lib/validate_url.rb"
]
s.homepage = "http://github.com/perfectline/validates_url/tree/master"
s.rubygems_version = "2.4.5"
s.summary = "Library for validating urls in Rails."
if s.respond_to? :specification_version then
s.specification_version = 4
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q, [">= 3.0.0"])
s.add_runtime_dependency(%q, [">= 0"])
s.add_development_dependency(%q, [">= 0"])
s.add_development_dependency(%q, [">= 1.1.2"])
else
s.add_dependency(%q, [">= 3.0.0"])
s.add_dependency(%q, [">= 0"])
s.add_dependency(%q, [">= 0"])
s.add_dependency(%q, [">= 1.1.2"])
end
else
s.add_dependency(%q, [">= 3.0.0"])
s.add_dependency(%q, [">= 0"])
s.add_dependency(%q, [">= 0"])
s.add_dependency(%q, [">= 1.1.2"])
end
end