pax_global_header00006660000000000000000000000064125534033470014520gustar00rootroot0000000000000052 comment=c26f7a1b5bf00bbcdadc825e69a9c401a94413ec validates_url-1.0.2/000077500000000000000000000000001255340334700143565ustar00rootroot00000000000000validates_url-1.0.2/.gitignore000066400000000000000000000000561255340334700163470ustar00rootroot00000000000000coverage pkg rdoc test/tmp tmp .idea .DS_Storevalidates_url-1.0.2/.rspec000066400000000000000000000000371255340334700154730ustar00rootroot00000000000000--colour --format documentationvalidates_url-1.0.2/LICENSE.md000066400000000000000000000021271255340334700157640ustar00rootroot00000000000000The 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.md000066400000000000000000000027421255340334700156420ustar00rootroot00000000000000# 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/Rakefile000066400000000000000000000024201255340334700160210ustar00rootroot00000000000000require '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.yml000066400000000000000000000000531255340334700162240ustar00rootroot00000000000000--- :major: 1 :minor: 0 :patch: 2 :build: validates_url-1.0.2/init.rb000066400000000000000000000000261255340334700156440ustar00rootroot00000000000000require 'validate_url'validates_url-1.0.2/install.rb000066400000000000000000000001031255340334700163430ustar00rootroot00000000000000puts IO.read(File.join(File.dirname(__FILE__), 'README.markdown')) validates_url-1.0.2/lib/000077500000000000000000000000001255340334700151245ustar00rootroot00000000000000validates_url-1.0.2/lib/locale/000077500000000000000000000000001255340334700163635ustar00rootroot00000000000000validates_url-1.0.2/lib/locale/de.yml000066400000000000000000000000761255340334700175010ustar00rootroot00000000000000de: errors: messages: url: ist keine gültige URL validates_url-1.0.2/lib/locale/en.yml000066400000000000000000000000721255340334700175070ustar00rootroot00000000000000en: errors: messages: url: is not a valid URL validates_url-1.0.2/lib/locale/it.yml000066400000000000000000000000751255340334700175240ustar00rootroot00000000000000it: errors: messages: url: non è un URL valido. validates_url-1.0.2/lib/locale/ja.yml000066400000000000000000000001001255340334700174670ustar00rootroot00000000000000ja: errors: messages: url: は不正なURLです。 validates_url-1.0.2/lib/locale/pt-BR.yml000066400000000000000000000001021255340334700200230ustar00rootroot00000000000000pt-BR: errors: messages: url: não é uma URL válida validates_url-1.0.2/lib/locale/tr.yml000066400000000000000000000000771255340334700175370ustar00rootroot00000000000000tr: errors: messages: url: Geçerli bir URL değil validates_url-1.0.2/lib/validate_url.rb000066400000000000000000000040301255340334700201210ustar00rootroot00000000000000require '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/000077500000000000000000000000001255340334700153105ustar00rootroot00000000000000validates_url-1.0.2/spec/resources/000077500000000000000000000000001255340334700173225ustar00rootroot00000000000000validates_url-1.0.2/spec/resources/user.rb000066400000000000000000000001611255340334700206230ustar00rootroot00000000000000class User include ActiveModel::Validations attr_accessor :homepage validates :homepage, :url => true endvalidates_url-1.0.2/spec/resources/user_with_ar.rb000066400000000000000000000001521255340334700223400ustar00rootroot00000000000000class UserWithAr < ActiveRecord::Base self.table_name = "users" validates :homepage, :url => true endvalidates_url-1.0.2/spec/resources/user_with_ar_legacy.rb000066400000000000000000000001461255340334700236670ustar00rootroot00000000000000class UserWithArLegacy < ActiveRecord::Base self.table_name = "users" validates_url :homepage endvalidates_url-1.0.2/spec/resources/user_with_blank.rb000066400000000000000000000002601255340334700230250ustar00rootroot00000000000000require 'active_model/validations' class UserWithBlank include ActiveModel::Validations attr_accessor :homepage validates :homepage, :url => {:allow_blank => true} endvalidates_url-1.0.2/spec/resources/user_with_custom_message.rb000066400000000000000000000002211255340334700247510ustar00rootroot00000000000000class UserWithCustomMessage include ActiveModel::Validations attr_accessor :homepage validates :homepage, :url => {message: "wrong"} end validates_url-1.0.2/spec/resources/user_with_custom_scheme.rb000066400000000000000000000002241255340334700245740ustar00rootroot00000000000000class UserWithCustomScheme include ActiveModel::Validations attr_accessor :homepage validates :homepage, :url => { :schemes => ['ftp'] } endvalidates_url-1.0.2/spec/resources/user_with_legacy_syntax.rb000066400000000000000000000002611255340334700246110ustar00rootroot00000000000000require 'active_model/validations' class UserWithLegacySyntax include ActiveModel::Validations attr_accessor :homepage validates_url :homepage, :allow_blank => true endvalidates_url-1.0.2/spec/resources/user_with_nil.rb000066400000000000000000000002541255340334700225230ustar00rootroot00000000000000require 'active_model/validations' class UserWithNil include ActiveModel::Validations attr_accessor :homepage validates :homepage, :url => {:allow_nil => true} endvalidates_url-1.0.2/spec/resources/user_with_no_local.rb000066400000000000000000000002571255340334700235320ustar00rootroot00000000000000require 'active_model/validations' class UserWithNoLocal include ActiveModel::Validations attr_accessor :homepage validates :homepage, :url => {:no_local => true} endvalidates_url-1.0.2/spec/spec_helper.rb000066400000000000000000000020251255340334700201250ustar00rootroot00000000000000$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.rb000066400000000000000000000125671255340334700213350ustar00rootroot00000000000000# 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.gemspec000066400000000000000000000036121255340334700204000ustar00rootroot00000000000000# 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