addressabler 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Flip Sasser
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,39 @@
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 addresabler
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 #=> "google.com"
27
+
28
+ Addressabler will add the following properties:
29
+
30
+ @uri.tld #=> "com"
31
+ @uri.domain #=> "google.com"
32
+ @uri.subdomain #=> "www"
33
+
34
+ Addressabler also makes editing queries a little bit easier:
35
+
36
+ @uri.query_hash[:foo] => :bar
37
+ @uri.to_s #=> http://www.google.com/?foo=bar
38
+
39
+ That's it. Enjoy.
@@ -0,0 +1,23 @@
1
+ module Addressabler
2
+ class Query < ::Hash
3
+ def delete(value)
4
+ super
5
+ update_uri
6
+ end
7
+
8
+ def []=(key, value)
9
+ super
10
+ update_uri
11
+ end
12
+
13
+ def uri=(uri)
14
+ @uri = uri
15
+ update_uri
16
+ end
17
+
18
+ private
19
+ def update_uri
20
+ @uri.query_values = empty? ? nil : to_hash
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,80 @@
1
+ require 'rubygems'
2
+ require 'addressable/uri'
3
+ require 'addressabler/query'
4
+
5
+ module Addressabler
6
+ module ClassMethods
7
+ def parse_tld(host)
8
+ host = host.to_s.split('.')
9
+ tlds = []
10
+ sub_hash = Addressabler::TLDS
11
+ while sub_hash = sub_hash[tld = host.pop]
12
+ tlds.unshift(tld)
13
+ if sub_hash.has_key? '*'
14
+ tlds.unshift(host.pop)
15
+ end
16
+ end
17
+ tlds.join('.')
18
+ end
19
+ end
20
+
21
+ module InstanceMethods
22
+ def domain
23
+ @domain ||= parse_domain_parts[:domain]
24
+ end
25
+
26
+ def query_hash
27
+ @query_hash ||= query_hash_for(query_values || {})
28
+ end
29
+
30
+ def query_hash=(new_query_hash)
31
+ @query_hash = query_hash_for(new_query_hash || {})
32
+ end
33
+
34
+ def subdomain
35
+ @subdomain ||= parse_domain_parts[:subdomain]
36
+ end
37
+
38
+ def tld
39
+ @tld ||= parse_domain_parts[:tld]
40
+ end
41
+
42
+ private
43
+ def query_hash_for(contents)
44
+ hash = Addressabler::Query[contents]
45
+ hash.uri = self
46
+ hash
47
+ end
48
+
49
+ def parse_domain_parts
50
+ return @domain_parts if defined? @domain_parts
51
+ tld = self.class.parse_tld(host)
52
+ begin
53
+ subdomain_parts = host.gsub(/\.#{tld}$/, '').split('.')
54
+ rescue
55
+ raise host.inspect
56
+ end
57
+ @domain_parts = {
58
+ :domain => subdomain_parts.pop,
59
+ :subdomain => subdomain_parts.join('.'),
60
+ :tld => tld
61
+ }
62
+ end
63
+ end
64
+
65
+ # Thanks Domainatrix for the parsing logic!
66
+ tlds = {}
67
+ File.readlines(File.join(File.dirname(__FILE__), 'tlds')).each do |line|
68
+ unless !line.strip! == ''
69
+ parts = line.split(".").reverse
70
+ sub_hash = tlds
71
+ parts.each do |part|
72
+ sub_hash = (sub_hash[part] ||= {})
73
+ end
74
+ end
75
+ end
76
+ TLDS = tlds
77
+ end
78
+
79
+ Addressable::URI.extend Addressabler::ClassMethods
80
+ Addressable::URI.send :include, Addressabler::InstanceMethods