urrl_formatter 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/CHANGELOG.md +0 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +38 -0
- data/Rakefile +6 -0
- data/lib/urrl_formatter/model_additions.rb +9 -0
- data/lib/urrl_formatter/railtie.rb +9 -0
- data/lib/urrl_formatter/version.rb +3 -0
- data/lib/urrl_formatter.rb +17 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/urrl_formatter/model_additions_spec.rb +22 -0
- data/spec/urrl_formatter_spec.rb +52 -0
- data/urrl_formatter.gemspec +22 -0
- metadata +95 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/CHANGELOG.md
ADDED
File without changes
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Valmir Dimas
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# UrrlFormatter
|
2
|
+
|
3
|
+
Format and validate a URL attribute in Active Record. This is an example gem created for [RailsCasts episode #301](http://railscasts.com/episodes/301-extracting-a-ruby-gem).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'urrl_formatter'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install urrl_formatter
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Call `format_url` in an ActiveRecord class and pass the name of the attribute you wish to format into a URL and validate.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class Comment < ActiveRecord::Base
|
27
|
+
format_url :website
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
This will automatically add "http://" to the beginning of the `website` attribute upon saving if no protocol is present. It will also do validation to ensure it looks like a URL.
|
32
|
+
|
33
|
+
|
34
|
+
## Development
|
35
|
+
|
36
|
+
Questions or problems? Please post them on the [issue tracker](https://github.com/ryanb/url_formatter/issues). You can contribute changes by forking the project and submitting a pull request. You can ensure the tests passing by running `bundle` and `rake`.
|
37
|
+
|
38
|
+
This gem is created by Ryan Bates and is under the MIT License.
|
data/Rakefile
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
module UrrlFormatter
|
2
|
+
module ModelAdditions
|
3
|
+
def format_url(attribute)
|
4
|
+
before_validation do
|
5
|
+
send("#{attribute}=", UrlFormatter.format_url(send(attribute)))
|
6
|
+
end
|
7
|
+
validates_format_of attribute, with: UrlFormatter.url_regexp, message: "is not a valid URL" end
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "urrl_formatter/version"
|
2
|
+
require "urrl_formatter/model_additions"
|
3
|
+
require "urrl_formatter/railtie" if defined? Rails
|
4
|
+
|
5
|
+
module UrrlFormatter
|
6
|
+
def self.format_url(url)
|
7
|
+
if url.to_s !~ url_regexp && "http://#{url}" =~ url_regexp
|
8
|
+
"http://#{url}"
|
9
|
+
else
|
10
|
+
url
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.url_regexp
|
15
|
+
/^https?:\/\/([^\s:@]+:[^\s:@]*@)?[-[[:alnum:]]]+(\.[-[[:alnum:]]]+)+\.?(:\d{1,5})?([\/?]\S*)?$/iux
|
16
|
+
end
|
17
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Comment < SuperModel::Base
|
4
|
+
include ActiveModel::Validations::Callbacks
|
5
|
+
extend UrrlFormatter::ModelAdditions
|
6
|
+
format_url :website
|
7
|
+
end
|
8
|
+
|
9
|
+
describe UrrlFormatter::ModelAdditions do
|
10
|
+
it "adds http:// to URL upon saving" do
|
11
|
+
Comment.create!(website: "example.com").website.should eq("http://example.com")
|
12
|
+
Comment.create!(website: "http://example.com").website.should eq("http://example.com")
|
13
|
+
end
|
14
|
+
|
15
|
+
it "validates URL format" do
|
16
|
+
comment = Comment.new(website: "foo bar")
|
17
|
+
comment.should_not be_valid
|
18
|
+
comment.errors[:website].should eq(["is not a valid URL"])
|
19
|
+
comment.website = "example.com"
|
20
|
+
comment.should be_valid
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
describe UrrlFormatter do
|
4
|
+
describe ".format_url" do
|
5
|
+
it "adds http:// to a URL if not provided" do
|
6
|
+
UrrlFormatter.format_url("example.com").should eq("http://example.com")
|
7
|
+
end
|
8
|
+
|
9
|
+
it "does not add http:// to a URL if already provided" do
|
10
|
+
UrrlFormatter.format_url("http://example.com").should eq("http://example.com")
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns an invalid URL unchanged" do
|
14
|
+
UrrlFormatter.format_url("foo bar").should eq("foo bar")
|
15
|
+
UrrlFormatter.format_url(nil).should eq(nil)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe ".url_regexp" do
|
20
|
+
it "matches valid URLs" do
|
21
|
+
[
|
22
|
+
'http://example.com/',
|
23
|
+
'HTTP://E-XAMLE.COM',
|
24
|
+
'https://example.co.uk./foo',
|
25
|
+
'http://example.com:8080',
|
26
|
+
'http://www.example.com/anything/after?slash',
|
27
|
+
'http://www.example.com?anything_after=question',
|
28
|
+
'http://user123:sEcr3t@example.com',
|
29
|
+
'http://user123:@example.com',
|
30
|
+
'http://example.com/~user',
|
31
|
+
'http://1.2.3.4:8080',
|
32
|
+
'http://ütf8.com',
|
33
|
+
].each do |url|
|
34
|
+
url.should match(UrrlFormatter.url_regexp)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it "does not match invalid URLs" do
|
39
|
+
[
|
40
|
+
"www.example.com",
|
41
|
+
"http://",
|
42
|
+
"http://example..com",
|
43
|
+
"http://e xample.com",
|
44
|
+
"http://example.com/foo bar",
|
45
|
+
"http://example", # technically valid but not what we want from user
|
46
|
+
"other://example.com", # we also don't want other protocols
|
47
|
+
].each do |url|
|
48
|
+
url.should_not match(UrrlFormatter.url_regexp)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'urrl_formatter/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "urrl_formatter"
|
8
|
+
gem.version = UrrlFormatter::VERSION
|
9
|
+
gem.authors = ["Valmir Dimas"]
|
10
|
+
gem.email = ["valmirdimas@gmail.com"]
|
11
|
+
gem.summary = %q{Format and validate a URL in Active Record}
|
12
|
+
gem.description = %q{Example of creating a Ruby gem for ASCIIcast #301}
|
13
|
+
gem.homepage = "http://github.com/Valmasso/urrl_formatter"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency "rspec"
|
21
|
+
gem.add_development_dependency "supermodel"
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: urrl_formatter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Valmir Dimas
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: supermodel
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: ! 'Example of creating a Ruby gem for ASCIIcast #301'
|
47
|
+
email:
|
48
|
+
- valmirdimas@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- .rspec
|
55
|
+
- CHANGELOG.md
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE.txt
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- lib/urrl_formatter.rb
|
61
|
+
- lib/urrl_formatter/model_additions.rb
|
62
|
+
- lib/urrl_formatter/railtie.rb
|
63
|
+
- lib/urrl_formatter/version.rb
|
64
|
+
- spec/spec_helper.rb
|
65
|
+
- spec/urrl_formatter/model_additions_spec.rb
|
66
|
+
- spec/urrl_formatter_spec.rb
|
67
|
+
- urrl_formatter.gemspec
|
68
|
+
homepage: http://github.com/Valmasso/urrl_formatter
|
69
|
+
licenses: []
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.8.24
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: Format and validate a URL in Active Record
|
92
|
+
test_files:
|
93
|
+
- spec/spec_helper.rb
|
94
|
+
- spec/urrl_formatter/model_additions_spec.rb
|
95
|
+
- spec/urrl_formatter_spec.rb
|