addressabler 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.
- data/CHANGELOG.md +4 -2
- data/README.md +82 -0
- data/lib/addressabler/query.rb +44 -1
- data/spec/addressabler_spec.rb +5 -11
- metadata +3 -3
- data/README.markdown +0 -53
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
## 0.0.
|
1
|
+
## 0.0.5
|
2
|
+
* Restored nested hash support **if Rack is installed**
|
3
|
+
|
4
|
+
## 0.0.4
|
2
5
|
* Updated to the latest TLD list from [Paul Dix](https://github.com/pauldix), thanks to [this unbelievably helpful user](https://github.com/flipsasser/addressabler/issues/1)
|
3
6
|
* Updated to latest Addressable spec
|
4
|
-
*
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# Addressabler
|
2
|
+
|
3
|
+
**Addressabler** extends the [Addressable::URI](https://github.com/sporkmonger/addressable) class by adding TLD parsing, domain and subdomain parsing, query modification, and restoring setting of nested hashes to query strings.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
Install using Rubygems:
|
8
|
+
|
9
|
+
gem install addressabler
|
10
|
+
|
11
|
+
Then:
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'addressabler'
|
15
|
+
|
16
|
+
Addressabler will automatically require `addressable/uri`.
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
### Domain, TLD, and subdomain parsing
|
21
|
+
|
22
|
+
Use `Addressable::URI` like you normally would:
|
23
|
+
|
24
|
+
```
|
25
|
+
@uri = Addressable::URI.parse("http://www.google.com/")
|
26
|
+
@uri.host #=> "www.google.com"
|
27
|
+
```
|
28
|
+
|
29
|
+
Addressabler will add the following properties:
|
30
|
+
|
31
|
+
```
|
32
|
+
@uri.tld #=> "com"
|
33
|
+
@uri.domain #=> "google"
|
34
|
+
@uri.subdomain #=> "www"
|
35
|
+
```
|
36
|
+
|
37
|
+
You can set these values, as well:
|
38
|
+
|
39
|
+
```
|
40
|
+
@uri.tld = "org"
|
41
|
+
@uri.host #=> "www.google.org"
|
42
|
+
@uri.domain = "amazon"
|
43
|
+
@uri.host #=> "www.amazon.org"
|
44
|
+
@uri.subdomain = "developers"
|
45
|
+
@uri.host #=> "developers.amazon.org"
|
46
|
+
```
|
47
|
+
|
48
|
+
#### Complex TLD support (thanks to Paul Dix!)
|
49
|
+
Addressabler copies some of Paul Dix's [Domaintrix](https://github.com/pauldix/domainatrix) TLD code to support fancy TLDs, as well:
|
50
|
+
|
51
|
+
```
|
52
|
+
@uri.host = "www.google.co.uk"
|
53
|
+
@uri.tld #=> "co.uk"
|
54
|
+
```
|
55
|
+
|
56
|
+
### Query interface
|
57
|
+
|
58
|
+
Addressabler adds a `query_hash` method to `Addressable::URI`s. This makes editing query strings a lot simpler, using a familiar Hash syntax:
|
59
|
+
|
60
|
+
```
|
61
|
+
@uri.query_hash[:foo] = :bar
|
62
|
+
@uri.to_s #=> "http://www.google.co.uk/?foo=bar"
|
63
|
+
```
|
64
|
+
|
65
|
+
#### Nested hashes in query strings
|
66
|
+
|
67
|
+
The current maintainer of Addressable, [Bob Aman](https://github.com/sporkmonger), feels rather strongly that [Rails got it wrong](https://github.com/sporkmonger/addressable/issues/77) in supporting nested hashes in query strings.
|
68
|
+
|
69
|
+
Frankly, I don't disagree with anything he has to say on the issue, but it is a problem many people have experienced.
|
70
|
+
|
71
|
+
*As such,* since Rack already supports building nested hashes "the Rails Way" (shudder), I added support for assigning nested hashes to `URI`s **only if Rack is available.** Addressabler will attempt to load `Rack::Utils` and, if it finds it, you can assign a nested hash in the `query_hash=` method like so:
|
72
|
+
|
73
|
+
```
|
74
|
+
@uri.query_hash = {:foo => {:bar => :baz}}
|
75
|
+
@uri.to_s #=> "http://www.google.co.uk/?foo[bar]=baz"
|
76
|
+
```
|
77
|
+
|
78
|
+
**HANDLE WITH CARE!** As [Bob explains in the discussion](https://github.com/sporkmonger/addressable/issues/77#issuecomment-8534480), there's a better alternative to nested hashes in query strings, so try that before you install this library.
|
79
|
+
|
80
|
+
That's it. Enjoy.
|
81
|
+
|
82
|
+
#### Copyright © 2013 Flip Sasser
|
data/lib/addressabler/query.rb
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
module Addressabler
|
2
2
|
class Query < ::Hash
|
3
|
+
class << self
|
4
|
+
def nested_hash_support?
|
5
|
+
return @nested_hash_support if defined?(@nested_hash_support)
|
6
|
+
begin
|
7
|
+
require 'rack/utils'
|
8
|
+
@nested_hash_support = true
|
9
|
+
rescue LoadError
|
10
|
+
@nested_hash_support = false
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
3
15
|
def delete(value)
|
4
16
|
super
|
5
17
|
update_uri
|
@@ -17,7 +29,38 @@ module Addressabler
|
|
17
29
|
|
18
30
|
private
|
19
31
|
def update_uri
|
20
|
-
|
32
|
+
if self.class.nested_hash_support?
|
33
|
+
@uri.query = Rack::Utils.build_nested_query(hash_with_string_values(self))
|
34
|
+
else
|
35
|
+
@uri.query_values = empty? ? nil : dup
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def array_with_string_values(array)
|
40
|
+
array.map do |value|
|
41
|
+
case value
|
42
|
+
when Array
|
43
|
+
array_with_string_values(value)
|
44
|
+
when Hash
|
45
|
+
hash_with_string_values(Hash)
|
46
|
+
else
|
47
|
+
value.to_s
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def hash_with_string_values(hash)
|
53
|
+
string_values = hash.map do |key, value|
|
54
|
+
case value
|
55
|
+
when Array
|
56
|
+
[key, array_with_string_values(value)]
|
57
|
+
when Hash
|
58
|
+
[key, hash_with_string_values(value)]
|
59
|
+
else
|
60
|
+
[key, value.to_s]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
Hash[*string_values.flatten(1)]
|
21
64
|
end
|
22
65
|
end
|
23
66
|
end
|
data/spec/addressabler_spec.rb
CHANGED
@@ -35,17 +35,11 @@ describe Addressabler do
|
|
35
35
|
uri.to_s.should == "http://www.foo.bar.baz.co.uk/gjadgsg?adg=f&foo=bar"
|
36
36
|
end
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
# time.
|
44
|
-
#it "should support adding nested values to the query" do
|
45
|
-
#uri = Addressable::URI.parse("http://www.amazon.ca")
|
46
|
-
#uri.query_hash[:foo] = {:bar => :baz}
|
47
|
-
#uri.to_s.should == "http://www.amazon.ca?foo[bar]=baz"
|
48
|
-
#end
|
38
|
+
it "should support adding nested values to the query" do
|
39
|
+
uri = Addressable::URI.parse("http://www.amazon.ca")
|
40
|
+
uri.query_hash[:foo] = {:bar => "baz", :sommat => [:else, 1, true, false]}
|
41
|
+
uri.to_s.should == "http://www.amazon.ca?foo[bar]=baz&foo[sommat][]=else&foo[sommat][]=1&foo[sommat][]=true&foo[sommat][]=false"
|
42
|
+
end
|
49
43
|
|
50
44
|
it "should support subdomains" do
|
51
45
|
uri = Addressable::URI.heuristic_parse("i.am.a.subdomain.co.uk")
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: addressabler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -68,14 +68,14 @@ executables: []
|
|
68
68
|
extensions: []
|
69
69
|
extra_rdoc_files:
|
70
70
|
- LICENSE
|
71
|
-
- README.
|
71
|
+
- README.md
|
72
72
|
files:
|
73
73
|
- CHANGELOG.md
|
74
74
|
- LICENSE
|
75
|
-
- README.markdown
|
76
75
|
- lib/addressabler.rb
|
77
76
|
- lib/addressabler/query.rb
|
78
77
|
- lib/tlds
|
78
|
+
- README.md
|
79
79
|
- spec/addressabler_spec.rb
|
80
80
|
- spec/spec_helper.rb
|
81
81
|
- spec/uri_spec.rb
|
data/README.markdown
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
Addressabler
|
2
|
-
=
|
3
|
-
|
4
|
-
**Addressabler** extends the Addressable::URI class by adding TLD parsing, domain and subdomain parsing, and query modification.
|
5
|
-
|
6
|
-
Install
|
7
|
-
==
|
8
|
-
|
9
|
-
Install using Rubygems:
|
10
|
-
|
11
|
-
gem install addressabler
|
12
|
-
|
13
|
-
Then:
|
14
|
-
|
15
|
-
require 'rubygems'
|
16
|
-
require 'addressabler'
|
17
|
-
|
18
|
-
Addressabler will automatically require `addressable/uri`.
|
19
|
-
|
20
|
-
Usage
|
21
|
-
==
|
22
|
-
|
23
|
-
Use Addressable::URI like you normally would:
|
24
|
-
|
25
|
-
@uri = Addressable::URI.parse("http://www.google.com/")
|
26
|
-
@uri.host #=> "www.google.com"
|
27
|
-
|
28
|
-
Addressabler will add the following properties:
|
29
|
-
|
30
|
-
@uri.tld #=> "com"
|
31
|
-
@uri.domain #=> "google"
|
32
|
-
@uri.subdomain #=> "www"
|
33
|
-
|
34
|
-
You can set these values, as well:
|
35
|
-
|
36
|
-
@uri.tld = "org"
|
37
|
-
@uri.host #=> "www.google.org"
|
38
|
-
@uri.domain = "amazon"
|
39
|
-
@uri.host #=> "www.amazon.org"
|
40
|
-
@uri.subdomain = "developers"
|
41
|
-
@uri.host #=> "developers.amazon.org"
|
42
|
-
|
43
|
-
Addressabler copies some of Paul Dix's [Domaintrix](https://github.com/pauldix/domainatrix) TLD code to support fancy TLDs, as well:
|
44
|
-
|
45
|
-
@uri.host = "www.google.co.uk"
|
46
|
-
@uri.tld #=> "co.uk"
|
47
|
-
|
48
|
-
Addressabler also makes editing queries a little bit easier:
|
49
|
-
|
50
|
-
@uri.query_hash[:foo] = :bar
|
51
|
-
@uri.to_s #=> "http://www.google.co.uk/?foo=bar"
|
52
|
-
|
53
|
-
That's it. Enjoy.
|