format_url 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 01401f671d38bf76fdc22da5c9f0aaea4595d93d
4
+ data.tar.gz: 0fdc81d55b5fb80b8beb688c150afb1181c6b212
5
+ SHA512:
6
+ metadata.gz: 7f9cb2ade538ad4668d0694caae880eed88463d3a7dbb1413e302d4d0cad04c597c49cb8bba62f7d53d1c5d230ae81f72b966df21ec2f36a6abafee2862a51c5
7
+ data.tar.gz: cdf8136751c2057aae94e75b9ec1daf2e1901e20aa83c9bf6053d1c5429bc8fba65d85017762982fe315ae0210c902ae17a26528d00ae1ac6a637a38de4a4c43
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,3 @@
1
+ ## 0.0.1
2
+
3
+ * initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in format_url.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 David Hopkinson
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.
@@ -0,0 +1,36 @@
1
+ # FormatUrl
2
+
3
+ Format and validate a URL attribute in Active Record. This is an example gem created and based on [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
+ gem 'format_url'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install format_url
18
+
19
+ ## Usage
20
+ Call `format_url` in an ActiveRecord class and pass the name of the attribute you wish to format into a URL and validate.
21
+
22
+ ```ruby
23
+ class MyClass < ActiveRecord::Base
24
+ format_url :website
25
+ end
26
+ ```
27
+
28
+ 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.
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create new Pull Request
@@ -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,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'format_url/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "format_url"
8
+ spec.version = FormatUrl::VERSION
9
+ spec.authors = ["David Hopkinson"]
10
+ spec.email = ["david@pinchtozoom.co.uk"]
11
+ spec.description = %q{Format and validation a URL in active record}
12
+ spec.summary = %q{An example of formatting a url based on Railscast Episode #301. Ryan Rocks!}
13
+ spec.homepage = "https://github.com/pinchtozoom/format_url"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "rspec"
22
+ spec.add_development_dependency "supermodel"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ end
@@ -0,0 +1,17 @@
1
+ require "format_url/version"
2
+ require "format_url/model_additions"
3
+ require "format_url/railtie" if defined? Rails
4
+
5
+ module FormatUrl
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,10 @@
1
+ module FormatUrl
2
+ module ModelAdditions
3
+ def format_url(attribute)
4
+ before_validation do
5
+ send("#{attribute}=", FormatUrl.format_url(send(attribute)))
6
+ end
7
+ validates_format_of attribute, with: FormatUrl.url_regexp, message: "is not a valid URL"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module FormatUrl
2
+ class Railtie < Rails::Railtie
3
+ initializer 'format_url.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 FormatUrl
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ class User < SuperModel::Base
4
+ include ActiveModel::Validations::Callbacks
5
+ extend FormatUrl::ModelAdditions
6
+ format_url :website
7
+ end
8
+
9
+ describe FormatUrl::ModelAdditions do
10
+ it "adds http:// to URL upon saving" do
11
+ User.create!(website: "example.com").website.should eq("http://example.com")
12
+ User.create!(website: "http://example.com").website.should eq("http://example.com")
13
+ end
14
+
15
+ it "validates URL format" do
16
+ user = User.new(website: "foo bar")
17
+ user.should_not be_valid
18
+ user.errors[:website].should eq(["is not a valid URL"])
19
+ user.website = "example.com"
20
+ user.should be_valid
21
+ end
22
+ end
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe FormatUrl do
5
+ describe ".format_url" do
6
+ it "adds http:// to a URL if not provided" do
7
+ FormatUrl.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
+ FormatUrl.format_url("http://example.com").should eq("http://example.com")
12
+ end
13
+
14
+ it "returns an invalid URL unchanged" do
15
+ FormatUrl.format_url("foo bar").should eq("foo bar")
16
+ FormatUrl.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(FormatUrl.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(FormatUrl.url_regexp)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,2 @@
1
+ require 'format_url'
2
+ require 'supermodel'
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: format_url
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - David Hopkinson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: supermodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Format and validation a URL in active record
70
+ email:
71
+ - david@pinchtozoom.co.uk
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - CHANGELOG.md
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - format_url.gemspec
84
+ - lib/format_url.rb
85
+ - lib/format_url/model_additions.rb
86
+ - lib/format_url/railtie.rb
87
+ - lib/format_url/version.rb
88
+ - spec/format_url/model_additions_spec.rb
89
+ - spec/format_url_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: https://github.com/pinchtozoom/format_url
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.0.3
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: 'An example of formatting a url based on Railscast Episode #301. Ryan Rocks!'
115
+ test_files:
116
+ - spec/format_url/model_additions_spec.rb
117
+ - spec/format_url_spec.rb
118
+ - spec/spec_helper.rb