normalize_url 0.0.4 → 0.0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc28344596c36795dbe9e99a32aafef83338b833
4
- data.tar.gz: f6d80c0b99786a59e03bd4a79a9da857ce6cabc8
3
+ metadata.gz: 7aaea38f5f19347a15ea18728ae96f405c81ce03
4
+ data.tar.gz: 73a549a8aaf2941a3ad7e9f477e13dbb10fa2eb7
5
5
  SHA512:
6
- metadata.gz: ea1afeacb3afecd2925ad6f21ff453a4aba887e34e58d900c3ae1444d8b35e0825071f385278ee5530fb5d802eafe763d1413f65b7e1f3bd59df0dca5151088c
7
- data.tar.gz: 1ee821c5e8a359a8d2cb6065add1d3068fa3b5e6c851cdb5614d4b80bbf9fcc8fb6857220ee3596bdf45de8b581d36d711b552a17f025364ad5efae3ac82e70e
6
+ metadata.gz: 8d522e53f691736bb59295b06de9a6278b50394a77043c57a3b6291a22665185942ee4d68bc7658b2ca50ab7a2ba1eae67a52a1c6a6fe0fd2838ec038f5ef7e5
7
+ data.tar.gz: 4bfc571d1283e71f67cd2db0ce577a4f036e8e663cb1b6c54e3e9060236692cd13b7a3cc8a00f8ec6e27856c2af21391903ff150536c6f6aa773fb62e632fd3a
data/README.md CHANGED
@@ -15,7 +15,7 @@ For example:
15
15
  - `http://example.com/foo/../products?product_id=123`
16
16
  - `http://example.com/products?product_id=123#comments-section`
17
17
  - `http://example.com//products/?product_id=123`
18
- - `http://example.com//products/?product_id=123&`
18
+ - `http://example.com/products/?product_id=123&`
19
19
  - `http://example.com/products?utm_source=whatever&product_id=123&utm_medium=twitter&utm_campaign=blah`
20
20
 
21
21
  will all become `http://example.com/products?product_id=123` after normalization.
@@ -46,10 +46,11 @@ Or install it yourself as:
46
46
  NormalizeUrl.process("http://example.com/products/?foo=bar&baz") # => "http://example.com/products?baz&foo=bar"
47
47
  ```
48
48
 
49
- Each tranformation could be skipped by demand. All you need is to pass it as a
50
- optional value to `normalize` method:
49
+ Some tranformations could be skipped by demand. All you need is to pass its
50
+ name as a optional value to `normalize` method:
51
51
 
52
52
  ```ruby
53
+ NormalizeUrl.process("http://example.com/foo/") # => "http://example.com/foo"
53
54
  NormalizeUrl.process("http://example.com/foo/", remove_trailing_slash: false) # => "http://example.com/foo/"
54
55
  ```
55
56
 
@@ -73,12 +74,6 @@ NormalizeUrl.process("http://example.com/foo/", remove_trailing_slash: false) #
73
74
 
74
75
  `http://example.com/foo#bar` -> `http://example.com/foo`
75
76
 
76
- - Reparse query params. Option `:reparse_query`.
77
-
78
- Example:
79
-
80
- `http://example.com/?foo=BAR+BAZ` -> `http://example.com/?foo=BAR%20BAZ`
81
-
82
77
  - Remove known commonly used tracking query parameters. Option `:remove_tracking`.
83
78
 
84
79
  Example:
@@ -30,10 +30,7 @@ module NormalizeUrl
30
30
  process :remove_trailing_slash
31
31
  process :remove_repeating_slashes
32
32
  process :remove_hash
33
- process :reparse_query
34
- process :remove_tracking
35
- process :remove_params
36
- process :sort_query
33
+ process_query
37
34
  uri.to_s
38
35
  end
39
36
 
@@ -43,18 +40,25 @@ module NormalizeUrl
43
40
  send "process_#{step}" if process?(step)
44
41
  end
45
42
 
43
+ def process_query
44
+ query_values = uri.query_values
45
+ return if query_values.nil?
46
+
47
+ query_values = remove_params(query_values, TRACKING_QUERY_PARAMS) if process?(:remove_tracking)
48
+ query_values = remove_params(query_values, params_to_remove) if process?(:remove_params)
49
+ query_values = query_values.to_a unless process?(:sort_query)
50
+
51
+ uri.query_values = query_values.empty? ? nil : query_values
52
+ end
53
+
46
54
  def process?(step)
47
- @options.fetch(step, true)
55
+ options.fetch(step, true)
48
56
  end
49
57
 
50
58
  def process_remove_trailing_slash
51
59
  uri.path = uri.path.chomp(?/) unless uri.path == ?/
52
60
  end
53
61
 
54
- def process_sort_query
55
- uri.query = uri.query.split(?&).sort.join(?&) if uri.query
56
- end
57
-
58
62
  def process_remove_hash
59
63
  uri.fragment = nil
60
64
  end
@@ -63,32 +67,16 @@ module NormalizeUrl
63
67
  uri.path = uri.path.squeeze(?/) if uri.host
64
68
  end
65
69
 
66
- def process_remove_tracking
67
- remove_params TRACKING_QUERY_PARAMS
68
- end
69
-
70
- def process_remove_params
71
- remove_params Array(@options.fetch(:remove_params, nil)).map(&:to_s)
72
- end
73
-
74
- def process_reparse_query
75
- uri.query_values = uri.query_values.to_a
76
- end
77
-
78
- def remove_params(params)
79
- return unless uri.query_values
80
- original = uri.query_values
81
- cleaned = original.reject{ |key, _| params.include?(key) }
82
-
83
- if cleaned.empty?
84
- uri.query_values = nil
85
- elsif cleaned != original
86
- uri.query_values = cleaned
87
- end
70
+ def remove_params(query_values, params)
71
+ query_values.reject{ |key, _| params.include?(key) }
88
72
  end
89
73
 
90
74
  def fail_uri(message)
91
75
  fail ArgumentError, message
92
76
  end
77
+
78
+ def params_to_remove
79
+ Array(options.fetch(:remove_params, nil)).map(&:to_s)
80
+ end
93
81
  end
94
82
  end
@@ -1,3 +1,3 @@
1
1
  module NormalizeUrl
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: normalize_url
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Pravosud
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-20 00:00:00.000000000 Z
11
+ date: 2014-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable