normalize_url 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +83 -0
- data/lib/normalize_url.rb +10 -0
- data/lib/normalize_url/normalizer.rb +46 -0
- data/lib/normalize_url/version.rb +3 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8028e7b8023b9a5bd1eedb26cc0a3f827573b0b5
|
4
|
+
data.tar.gz: abfdc72f52b71bcdb14d654f6f5c42fd9df3e682
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8c9d689c0e501cdc118c026842eb40e6f3ff5a546e61e0cbb333f628c6f385af20fa5eba3b0088026ed21d3f7692b3e60e5fa6b0195f798294ddfad4f049da02
|
7
|
+
data.tar.gz: 43cfa103f3f30a8c5c8b5ea2f11ba2677950c2a19b7d9f5b10a3f883117a6830cfc2ec12352e81a5e4d955e055cc95af07d383438995ae05b747405cede52deb
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Pavel Pravosud
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# NormalizeUrl
|
2
|
+
|
3
|
+
This gem can normalize HTTP(S) URLs by applying a certain set of
|
4
|
+
transformations. After normalization, two different URLs that point to the same
|
5
|
+
resource should look exactly the same.
|
6
|
+
|
7
|
+
For example:
|
8
|
+
|
9
|
+
- `http://example.com/products`
|
10
|
+
- `http://example.com/products/`
|
11
|
+
- `http://example.com/foo/../products`
|
12
|
+
- `http://example.com/products#comments-section`
|
13
|
+
- `http://example.com//products/`
|
14
|
+
- `http://example.com/products?`
|
15
|
+
|
16
|
+
will all become `http://example.com/products` after normalization.
|
17
|
+
|
18
|
+
Some of the transformations are potentially dangerous, since not all webservers
|
19
|
+
comform to standards and some of them are just plain weird. So there is no
|
20
|
+
gurantee that the URL will still work.
|
21
|
+
|
22
|
+
## Installation
|
23
|
+
|
24
|
+
Add this line to your application's Gemfile:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
gem "normalize_url"
|
28
|
+
```
|
29
|
+
|
30
|
+
And then execute:
|
31
|
+
|
32
|
+
$ bundle
|
33
|
+
|
34
|
+
Or install it yourself as:
|
35
|
+
|
36
|
+
$ gem install normalize_url
|
37
|
+
|
38
|
+
## Usage
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
NormalizeUrl.process("http://example.com/products/?foo=bar&baz") # => "http://example.com/products?baz&foo=bar"
|
42
|
+
```
|
43
|
+
|
44
|
+
Each tranformation could be skipped by demand. All you need is to pass it as a
|
45
|
+
optional value to `normalize` method:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
NormalizeUrl.normalize("http://example.com/foo/", remove_trailing_slash: false) # => "http://example.com/foo/"
|
49
|
+
```
|
50
|
+
|
51
|
+
## Transformations
|
52
|
+
|
53
|
+
- Remove trailing slash. Option `:remove_trailing_slash`
|
54
|
+
|
55
|
+
Example:
|
56
|
+
|
57
|
+
`http://example.com/products/` -> `http://example.com/products`
|
58
|
+
|
59
|
+
- Remove repeating slashes. Option `:remove_repeating_slashes`
|
60
|
+
|
61
|
+
Example:
|
62
|
+
|
63
|
+
`http://example.com/foo//bar` -> `http://example.com/foo/bar`
|
64
|
+
|
65
|
+
- Remove hash fragment. Option `:remove_hash`.
|
66
|
+
|
67
|
+
Example:
|
68
|
+
|
69
|
+
`http://example.com/foo#bar` -> `http://example.com/foo`
|
70
|
+
|
71
|
+
- Sort query string. Option `:sort_query`.
|
72
|
+
|
73
|
+
Example:
|
74
|
+
|
75
|
+
`http://example.com/products/?foo=bar&baz` -> `http://example.com/products?baz&foo=bar`
|
76
|
+
|
77
|
+
## Contributing
|
78
|
+
|
79
|
+
1. Fork it (https://github.com/rwz/normalize_url/fork)
|
80
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
81
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
82
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
83
|
+
5. Create a new Pull Request
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require "addressable/uri"
|
2
|
+
|
3
|
+
module NormalizeUrl
|
4
|
+
class Normalizer
|
5
|
+
attr_reader :uri, :options
|
6
|
+
|
7
|
+
def initialize(original_uri, options={})
|
8
|
+
@uri = Addressable::URI.parse(original_uri).normalize
|
9
|
+
@options = options
|
10
|
+
end
|
11
|
+
|
12
|
+
def normalize
|
13
|
+
process :remove_trailing_slash
|
14
|
+
process :remove_repeating_slashes
|
15
|
+
process :remove_hash
|
16
|
+
process :sort_query
|
17
|
+
uri.to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def process(step)
|
23
|
+
send "process_#{step}" if process?(step)
|
24
|
+
end
|
25
|
+
|
26
|
+
def process?(step)
|
27
|
+
@options.fetch(step, true)
|
28
|
+
end
|
29
|
+
|
30
|
+
def process_remove_trailing_slash
|
31
|
+
uri.path = uri.path.chomp(?/) unless uri.path == ?/
|
32
|
+
end
|
33
|
+
|
34
|
+
def process_sort_query
|
35
|
+
uri.query = uri.query.split(?&).sort.join(?&) if uri.query
|
36
|
+
end
|
37
|
+
|
38
|
+
def process_remove_hash
|
39
|
+
uri.fragment = nil
|
40
|
+
end
|
41
|
+
|
42
|
+
def process_remove_repeating_slashes
|
43
|
+
uri.path = uri.path.squeeze(?/) if uri.host
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: normalize_url
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pavel Pravosud
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: addressable
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.3'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- pavel@pravosud.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- LICENSE.txt
|
35
|
+
- README.md
|
36
|
+
- lib/normalize_url.rb
|
37
|
+
- lib/normalize_url/normalizer.rb
|
38
|
+
- lib/normalize_url/version.rb
|
39
|
+
homepage: https://github.com/rwz/normalize_url
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
metadata: {}
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - "~>"
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '2.0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 2.2.2
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: Normalizing URLs like a Boss
|
63
|
+
test_files: []
|