linkformatter 0.0.2

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 ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,2 @@
1
+ rvm:
2
+ - 1.9.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.0.1
2
+
3
+ * initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in url_formatter.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Ryan Bates
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # URL Formatter [![Build Status](https://secure.travis-ci.org/ryanb/url_formatter.png)](http://travis-ci.org/ryanb/url_formatter)
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
+
6
+ ## Installation
7
+
8
+ Add to your Gemfile and run the `bundle` command to install it.
9
+
10
+ ```ruby
11
+ gem "url_formatter"
12
+ ```
13
+
14
+ **Requires Ruby 1.9.2 or later.**
15
+
16
+
17
+ ## Usage
18
+
19
+ Call [format_url](http://rubydoc.info/github/ryanb/url_formatter/master/UrlFormatter/ModelAdditions:format_url) in an ActiveRecord class and pass the name of the attribute you wish to format into a URL and validate.
20
+
21
+ ```ruby
22
+ class Comment < ActiveRecord::Base
23
+ format_url :website
24
+ end
25
+ ```
26
+
27
+ 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.
28
+
29
+
30
+ ## Development
31
+
32
+ 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`.
33
+
34
+ This gem is created by Ryan Bates and is under the MIT License.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,17 @@
1
+ require "url_formatter/version"
2
+ require "url_formatter/model_additions"
3
+ require "url_formatter/railtie" if defined? Rails
4
+
5
+ module UrlFormatter
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
@@ -0,0 +1,20 @@
1
+ module UrlFormatter
2
+ module ModelAdditions
3
+ # To format and validate a URL attribute, call <tt>format_url</tt>
4
+ # in any Active Record model class and pass it the name of an attribute.
5
+ #
6
+ # class User < ActiveRecord::Base
7
+ # format_url :website
8
+ # end
9
+ #
10
+ # This will add a <tt>before_validation</tt> callback to add "http://" to
11
+ # the attribute if a protocol doesn't exist already. It then validates the
12
+ # format of the URL.
13
+ def format_url(attribute)
14
+ before_validation do
15
+ send("#{attribute}=", UrlFormatter.format_url(send(attribute)))
16
+ end
17
+ validates_format_of attribute, with: UrlFormatter.url_regexp, message: "is not a valid URL"
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ module UrlFormatter
2
+ class Railtie < Rails::Railtie
3
+ initializer 'url_formatter.model_additions' do
4
+ ActiveSupport.on_load :active_record do
5
+ extend ModelAdditions
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module UrlFormatter
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'url_formatter'
2
+ require 'supermodel'
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ class Comment < SuperModel::Base
4
+ include ActiveModel::Validations::Callbacks
5
+ extend UrlFormatter::ModelAdditions
6
+ format_url :website
7
+ end
8
+
9
+ describe UrlFormatter::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,53 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe UrlFormatter do
5
+ describe ".format_url" do
6
+ it "adds http:// to a URL if not provided" do
7
+ UrlFormatter.format_url("example.com").should eq("http://example.com")
8
+ end
9
+
10
+ it "does not add http:// to a URL if already provided" do
11
+ UrlFormatter.format_url("http://example.com").should eq("http://example.com")
12
+ end
13
+
14
+ it "returns an invalid URL unchanged" do
15
+ UrlFormatter.format_url("foo bar").should eq("foo bar")
16
+ UrlFormatter.format_url(nil).should eq(nil)
17
+ end
18
+ end
19
+
20
+ describe ".url_regexp" do
21
+ it "matches valid URLs" do
22
+ [
23
+ 'http://example.com/',
24
+ 'HTTP://E-XAMLE.COM',
25
+ 'https://example.co.uk./foo',
26
+ 'http://example.com:8080',
27
+ 'http://www.example.com/anything/after?slash',
28
+ 'http://www.example.com?anything_after=question',
29
+ 'http://user123:sEcr3t@example.com',
30
+ 'http://user123:@example.com',
31
+ 'http://example.com/~user',
32
+ 'http://1.2.3.4:8080',
33
+ 'http://ütf8.com',
34
+ ].each do |url|
35
+ url.should match(UrlFormatter.url_regexp)
36
+ end
37
+ end
38
+
39
+ it "does not match invalid URLs" do
40
+ [
41
+ "www.example.com",
42
+ "http://",
43
+ "http://example..com",
44
+ "http://e xample.com",
45
+ "http://example.com/foo bar",
46
+ "http://example", # technically valid but not what we want from user
47
+ "other://example.com", # we also don't want other protocols
48
+ ].each do |url|
49
+ url.should_not match(UrlFormatter.url_regexp)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "url_formatter/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "linkformatter"
7
+ s.version = UrlFormatter::VERSION
8
+ s.authors = ["xiaodong"]
9
+ s.email = ["xiaodong.casewestern@gmail.com"]
10
+ s.homepage = "https://github.com/dongxiao/url_formatter"
11
+ s.summary = %q{Format and validate a URL in Active Record}
12
+ s.description = %q{Example of creating a Ruby Gem for RailsCasts episode #301}
13
+
14
+ s.rubyforge_project = "linkformatter"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rake"
22
+ s.add_development_dependency "rspec"
23
+ s.add_development_dependency "supermodel"
24
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: linkformatter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - xiaodong
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-29 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70310326543000 !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: *70310326543000
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70310326542380 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70310326542380
36
+ - !ruby/object:Gem::Dependency
37
+ name: supermodel
38
+ requirement: &70310326541700 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70310326541700
47
+ description: ! 'Example of creating a Ruby Gem for RailsCasts episode #301'
48
+ email:
49
+ - xiaodong.casewestern@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - .rspec
56
+ - .travis.yml
57
+ - CHANGELOG.md
58
+ - Gemfile
59
+ - LICENSE
60
+ - README.md
61
+ - Rakefile
62
+ - lib/url_formatter.rb
63
+ - lib/url_formatter/model_additions.rb
64
+ - lib/url_formatter/railtie.rb
65
+ - lib/url_formatter/version.rb
66
+ - spec/spec_helper.rb
67
+ - spec/url_formatter/model_additions_spec.rb
68
+ - spec/url_formatter_spec.rb
69
+ - url_formatter.gemspec
70
+ homepage: https://github.com/dongxiao/url_formatter
71
+ licenses: []
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project: linkformatter
90
+ rubygems_version: 1.8.11
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Format and validate a URL in Active Record
94
+ test_files:
95
+ - spec/spec_helper.rb
96
+ - spec/url_formatter/model_additions_spec.rb
97
+ - spec/url_formatter_spec.rb