url_formatter 0.0.1.alpha → 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/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,3 @@
1
+ ## v0.0.1
2
+
3
+ * initial release
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.
@@ -0,0 +1,34 @@
1
+ # 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` 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.
@@ -0,0 +1,10 @@
1
+ module UrlFormatter
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"
8
+ end
9
+ end
10
+ 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
@@ -1,3 +1,3 @@
1
1
  module UrlFormatter
2
- VERSION = "0.0.1.alpha"
2
+ VERSION = "0.0.1"
3
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
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: url_formatter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.alpha
5
- prerelease: 6
4
+ version: 0.0.1
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ryan Bates
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-21 00:00:00.000000000 Z
12
+ date: 2011-11-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70221063413020 !ruby/object:Gem::Requirement
16
+ requirement: &70208597576560 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70221063413020
24
+ version_requirements: *70208597576560
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: supermodel
27
- requirement: &70221063412600 !ruby/object:Gem::Requirement
27
+ requirement: &70208597575760 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70221063412600
35
+ version_requirements: *70208597575760
36
36
  description: ! 'Example of creating a Ruby Gem for RailsCasts episode #301'
37
37
  email:
38
38
  - ryan@railscasts.com
@@ -41,10 +41,19 @@ extensions: []
41
41
  extra_rdoc_files: []
42
42
  files:
43
43
  - .gitignore
44
+ - .rspec
45
+ - CHANGELOG.md
44
46
  - Gemfile
47
+ - LICENSE
48
+ - README.md
45
49
  - Rakefile
46
50
  - lib/url_formatter.rb
51
+ - lib/url_formatter/model_additions.rb
52
+ - lib/url_formatter/railtie.rb
47
53
  - lib/url_formatter/version.rb
54
+ - spec/spec_helper.rb
55
+ - spec/url_formatter/model_additions_spec.rb
56
+ - spec/url_formatter_spec.rb
48
57
  - url_formatter.gemspec
49
58
  homepage: https://github.com/ryanb/url_formatter
50
59
  licenses: []
@@ -58,16 +67,25 @@ required_ruby_version: !ruby/object:Gem::Requirement
58
67
  - - ! '>='
59
68
  - !ruby/object:Gem::Version
60
69
  version: '0'
70
+ segments:
71
+ - 0
72
+ hash: 1712855206576306572
61
73
  required_rubygems_version: !ruby/object:Gem::Requirement
62
74
  none: false
63
75
  requirements:
64
- - - ! '>'
76
+ - - ! '>='
65
77
  - !ruby/object:Gem::Version
66
- version: 1.3.1
78
+ version: '0'
79
+ segments:
80
+ - 0
81
+ hash: 1712855206576306572
67
82
  requirements: []
68
83
  rubyforge_project: url_formatter
69
84
  rubygems_version: 1.8.11
70
85
  signing_key:
71
86
  specification_version: 3
72
87
  summary: Format and validate a URL in Active Record
73
- test_files: []
88
+ test_files:
89
+ - spec/spec_helper.rb
90
+ - spec/url_formatter/model_additions_spec.rb
91
+ - spec/url_formatter_spec.rb