urlish 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 @@
1
+ 1.9.3-p392
@@ -0,0 +1,3 @@
1
+ ## v0.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 urlish.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Robert Gratwick
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,29 @@
1
+ # UrlFormatter
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'url_formatter'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install url_formatter
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
@@ -0,0 +1,17 @@
1
+ require "urlish/version"
2
+ require "urlish/model_additions"
3
+ require "urlish/railtie" if defined? Rails
4
+
5
+ module Urlish
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 Urlish
2
+ module ModelAdditions
3
+ def format_url(attribute)
4
+ before_validation do
5
+ send("#{attribute}=", Urlish.format_url(send(attribute)))
6
+ end
7
+ validates_format_of attribute, with: Urlish.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 'urlish.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 Urlish
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'urlish'
2
+ require 'supermodel'
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ class Comment < SuperModel::Base
4
+ include ActiveModel::Validations::Callbacks
5
+ extend Urlish::ModelAdditions
6
+ format_url :website
7
+ end
8
+
9
+ describe Urlish::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 Urlish do
4
+ describe ".format_url" do
5
+ it "adds http:// to a URL if not provided" do
6
+ Urlish.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
+ Urlish.format_url("http://example.com").should eq("http://example.com")
11
+ end
12
+
13
+ it "returns an invalid URL unchanged" do
14
+ Urlish.format_url("foo bar").should eq("foo bar")
15
+ Urlish.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(Urlish.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(Urlish.url_regexp)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'urlish/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "urlish"
8
+ spec.version = Urlish::VERSION
9
+ spec.authors = ["Robert Gratwick"]
10
+ spec.email = ["rgratwick@gmail.com"]
11
+ spec.description = %q{Testing a gem description}
12
+ spec.summary = %q{Testing a gem summary}
13
+ spec.homepage = "http://github.com/rgratwick/urlish"
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 "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "supermodel"
25
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: urlish
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Robert Gratwick
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
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: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
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
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: supermodel
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Testing a gem description
79
+ email:
80
+ - rgratwick@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - .ruby-version
88
+ - CHANGELOG
89
+ - Gemfile
90
+ - LICENSE.txt
91
+ - README.md
92
+ - Rakefile
93
+ - lib/urlish.rb
94
+ - lib/urlish/model_additions.rb
95
+ - lib/urlish/railtie.rb
96
+ - lib/urlish/version.rb
97
+ - spec/spec_helper.rb
98
+ - spec/urlish/model_additions_spec.rb
99
+ - spec/urlish_spec.rb
100
+ - urlish.gemspec
101
+ homepage: http://github.com/rgratwick/urlish
102
+ licenses:
103
+ - MIT
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 1.8.23
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: Testing a gem summary
126
+ test_files:
127
+ - spec/spec_helper.rb
128
+ - spec/urlish/model_additions_spec.rb
129
+ - spec/urlish_spec.rb