url_format 0.0.8 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1805e62ee4b7a41087fe084c7602dbb8d7514910
4
- data.tar.gz: 9ca2331032efbac625d71ad10ef30350dff9e79d
3
+ metadata.gz: 3297d45a6ae1932a9de8ff8bd8b5a3b5636d6ea4
4
+ data.tar.gz: b646131d7c98760dc919355eb2b1fd669a2c601d
5
5
  SHA512:
6
- metadata.gz: 6b2414aa83d1919196a7e14cacc03b03600663864258e45920ac6cd55cfee80788368ea025f6b1d8f85a5077cff7b549d21e0f44306d81735bb85fed1c2d1743
7
- data.tar.gz: e59d7902ea52328bcdeeaa147890c132f820d5dc8db09b4343505a47323db5125b15fbff2270e1abf3d10d1cf51389a92016ee1b51bec2b4bb7da35b7c19d5c4
6
+ metadata.gz: e0d462f83aaf7b4bed83ec50ec73e5a04f90772779e2877998511747f4cf68c512403cf3aae9460c36c085699898b493ec0cabc767e8be7c022b1ef7b899740e
7
+ data.tar.gz: a74483679fb45b6679b790e5145ddc7671f580c488da65437b4903d12b091c88c63e75212af393cc9dc31b263aa23a357ddb37c6ad231f9dd147dd723fd00d65
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9
4
+ - 2.0
5
+ - 2.1
6
+ - 2.2
7
+
8
+ notifications:
9
+ email:
10
+ - johnotander@gmail.com
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 John Otander
1
+ Copyright (c) 2013-2015 John Otander
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # UrlFormat
2
2
 
3
- TODO: Write a gem description
3
+ [![Build Status](https://travis-ci.org/johnotander/url_format.svg?branch=master)](https://travis-ci.org/johnotander/url_format)
4
+
5
+ This gem validates a URL, adds the `http://` prefix if not included, and provides a `get_domain` method.
4
6
 
5
7
  ## Installation
6
8
 
@@ -18,7 +20,61 @@ Or install it yourself as:
18
20
 
19
21
  ## Usage
20
22
 
21
- TODO: Write usage instructions here
23
+ Using it is as simple as using the `validates` keyword in your model:
24
+
25
+ ```ruby
26
+ class User < ActiveRecord::Base
27
+
28
+ # ...
29
+
30
+ validates :url, url_format: true
31
+
32
+ # ...
33
+
34
+ end
35
+ ```
36
+
37
+ Now the url attribute will be validated accordingly:
38
+
39
+ ```ruby
40
+ User.new(url: 'http://example.com').valid? # => true
41
+ User.new(url: 'invalid_url..com').valid? # => false
42
+ ```
43
+
44
+ Also, the model in question doesn't need to inherit from ActiveRecord::Base, you only need to `include ActiveModel::Validations` in your class:
45
+
46
+ ```ruby
47
+ require 'url_format'
48
+
49
+ class Awesome
50
+ include ActiveModel::Validations
51
+
52
+ attr_accessor :url
53
+
54
+ validates :url, url_format: true
55
+
56
+ def domain
57
+ UrlFormat.get_domain(url)
58
+ end
59
+ end
60
+
61
+ awesome = Awesome.new
62
+
63
+ awesome.url = "www.example.com"
64
+ awesome.valid? # => true
65
+ awesome.url # => 'http://www.example.com'
66
+ awesome.domain # => 'example.com'
67
+
68
+ awesome.url = "invalid_url"
69
+ awesome.valid? # => false
70
+ ```
71
+
72
+ The gem can ensure that a url is prefixed with `http://` as well:
73
+
74
+ ```ruby
75
+ UrlFormat.ensure_http_prefix('google.com') # => 'http://google.com'
76
+ UrlFormat.ensure_http_prefix('https://google.com') # => 'https://google.com'
77
+ ```
22
78
 
23
79
  ## Contributing
24
80
 
data/Rakefile CHANGED
@@ -1 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task default: :spec
7
+ task test: :spec
@@ -5,8 +5,13 @@ require "uri"
5
5
  module UrlFormat
6
6
 
7
7
  def self.get_domain(url)
8
- host = URI.parse(url).host.downcase
8
+ host = URI.parse(ensure_http_prefix(url)).host.downcase
9
9
  host.start_with?('www.') ? host[4..-1] : host
10
- rescue URI::InvalidURIError
10
+ rescue URI::InvalidURIError, NoMethodError
11
+ end
12
+
13
+ def self.ensure_http_prefix(url)
14
+ return url if url =~ /\Ahttps?:\/\//
15
+ "http://#{url}"
11
16
  end
12
17
  end
@@ -9,8 +9,7 @@ class UrlFormatValidator < ActiveModel::EachValidator
9
9
  end
10
10
 
11
11
  def format_value(record, attribute, value)
12
- return value if value =~ /\Ahttps?:\/\//
13
- record.send("#{attribute}=","http://#{value}")
12
+ record.send("#{attribute}=",UrlFormat.ensure_http_prefix(value))
14
13
  end
15
14
 
16
15
  # Thanks to Dean Perry and Ryan Bates
@@ -1,3 +1,3 @@
1
1
  module UrlFormat
2
- VERSION = "0.0.8"
2
+ VERSION = '1.0.0'
3
3
  end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rspec'
4
+ require 'active_model'
5
+ require 'url_format'
6
+
7
+ class FakeModel
8
+ include ActiveModel::Validations
9
+ attr_accessor :url
10
+ validates :url, url_format: true, allow_blank: true
11
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe UrlFormat do
4
+
5
+ subject { UrlFormat }
6
+
7
+ describe '.get_domain' do
8
+
9
+ let(:urls_with_domains) {
10
+ {
11
+ 'http://google.com' => 'google.com',
12
+ 'https://fake.google.com' => 'fake.google.com',
13
+ 'www.fake.google.com/fake_stuff' => 'fake.google.com',
14
+ 'fake.fake.google.com' => 'fake.fake.google.com'
15
+ }
16
+ }
17
+
18
+ it 'should return the proper domain' do
19
+ urls_with_domains.each do |url, domain|
20
+ expect(subject.get_domain(url)).to eq(domain)
21
+ end
22
+ end
23
+ end
24
+
25
+ describe '.ensure_http_prefix' do
26
+
27
+ context 'without a prefix' do
28
+
29
+ let(:url) { 'www.google.com' }
30
+
31
+ it 'should prefix with http://' do
32
+ expect(subject.ensure_http_prefix(url)).to eq("http://#{ url }")
33
+ end
34
+ end
35
+
36
+ context 'with a prefix' do
37
+
38
+ let(:urls) { %w(http://google.com https://google.com) }
39
+
40
+ it 'should not modify the url' do
41
+ urls.each do |url|
42
+ expect(subject.ensure_http_prefix(url)).to eq(url)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe UrlFormatValidator do
4
+
5
+ let(:fake_model) { FakeModel.new }
6
+
7
+ context 'with valid urls' do
8
+
9
+ let(:valid_urls) { %w(http://google.com https://www.google.com www.google.com google.com) }
10
+
11
+ it 'should be happy' do
12
+ valid_urls.each do |url|
13
+ fake_model.url = url
14
+ expect(fake_model.valid?).to be_truthy
15
+ end
16
+ end
17
+ end
18
+
19
+ context 'with invalid urls' do
20
+
21
+ let(:invalid_urls) { %w(invalid http://invalid..com, invalid..com) }
22
+
23
+ it 'should not be happy' do
24
+ invalid_urls.each do |invalid_url|
25
+ fake_model.url = invalid_url
26
+ expect(fake_model.valid?).to be_falsey
27
+ end
28
+ end
29
+ end
30
+ end
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "activemodel"
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.3"
22
24
  spec.add_development_dependency "rake"
23
25
  spec.add_development_dependency "rspec"
metadata CHANGED
@@ -1,55 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: url_format
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Otander
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-27 00:00:00.000000000 Z
11
+ date: 2015-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
- - - ~>
31
+ - - "~>"
18
32
  - !ruby/object:Gem::Version
19
33
  version: '1.3'
20
34
  type: :development
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
- - - ~>
38
+ - - "~>"
25
39
  - !ruby/object:Gem::Version
26
40
  version: '1.3'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - ! '>='
45
+ - - ">="
32
46
  - !ruby/object:Gem::Version
33
47
  version: '0'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - ! '>='
52
+ - - ">="
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rspec
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
- - - ! '>='
59
+ - - ">="
46
60
  - !ruby/object:Gem::Version
47
61
  version: '0'
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - ! '>='
66
+ - - ">="
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
69
  description: Validate the url format of an attribute.
@@ -59,7 +73,8 @@ executables: []
59
73
  extensions: []
60
74
  extra_rdoc_files: []
61
75
  files:
62
- - .gitignore
76
+ - ".gitignore"
77
+ - ".travis.yml"
63
78
  - Gemfile
64
79
  - LICENSE.txt
65
80
  - README.md
@@ -67,6 +82,9 @@ files:
67
82
  - lib/url_format.rb
68
83
  - lib/url_format/url_format_validator.rb
69
84
  - lib/url_format/version.rb
85
+ - spec/spec_helper.rb
86
+ - spec/url_format_spec.rb
87
+ - spec/url_format_validator_spec.rb
70
88
  - url_format.gemspec
71
89
  homepage: https://github.com/johnotander/url_format
72
90
  licenses:
@@ -78,18 +96,21 @@ require_paths:
78
96
  - lib
79
97
  required_ruby_version: !ruby/object:Gem::Requirement
80
98
  requirements:
81
- - - ! '>='
99
+ - - ">="
82
100
  - !ruby/object:Gem::Version
83
101
  version: '0'
84
102
  required_rubygems_version: !ruby/object:Gem::Requirement
85
103
  requirements:
86
- - - ! '>='
104
+ - - ">="
87
105
  - !ruby/object:Gem::Version
88
106
  version: '0'
89
107
  requirements: []
90
108
  rubyforge_project:
91
- rubygems_version: 2.0.3
109
+ rubygems_version: 2.4.5
92
110
  signing_key:
93
111
  specification_version: 4
94
112
  summary: Validate the url format of an attribute.
95
- test_files: []
113
+ test_files:
114
+ - spec/spec_helper.rb
115
+ - spec/url_format_spec.rb
116
+ - spec/url_format_validator_spec.rb