addressabler 0.0.1 → 0.0.2

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/README.markdown CHANGED
@@ -23,17 +23,31 @@ Usage
23
23
  Use Addressable::URI like you normally would:
24
24
 
25
25
  @uri = Addressable::URI.parse("http://www.google.com/")
26
- @uri.host #=> "google.com"
26
+ @uri.host #=> "www.google.com"
27
27
 
28
28
  Addressabler will add the following properties:
29
29
 
30
30
  @uri.tld #=> "com"
31
- @uri.domain #=> "google.com"
31
+ @uri.domain #=> "google"
32
32
  @uri.subdomain #=> "www"
33
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
+
34
48
  Addressabler also makes editing queries a little bit easier:
35
49
 
36
- @uri.query_hash[:foo] => :bar
37
- @uri.to_s #=> http://www.google.com/?foo=bar
50
+ @uri.query_hash[:foo] = :bar
51
+ @uri.to_s #=> "http://www.google.co.uk/?foo=bar"
38
52
 
39
53
  That's it. Enjoy.
data/lib/addressabler.rb CHANGED
@@ -20,7 +20,11 @@ module Addressabler
20
20
 
21
21
  module InstanceMethods
22
22
  def domain
23
- @domain ||= parse_domain_parts[:domain]
23
+ parse_domain_parts[:domain]
24
+ end
25
+
26
+ def domain=(new_domain)
27
+ self.host = "#{subdomain}.#{new_domain}.#{tld}"
24
28
  end
25
29
 
26
30
  def query_hash
@@ -32,11 +36,19 @@ module Addressabler
32
36
  end
33
37
 
34
38
  def subdomain
35
- @subdomain ||= parse_domain_parts[:subdomain]
39
+ parse_domain_parts[:subdomain]
40
+ end
41
+
42
+ def subdomain=(new_subdomain)
43
+ self.host = "#{new_subdomain}.#{domain}.#{tld}"
36
44
  end
37
45
 
38
46
  def tld
39
- @tld ||= parse_domain_parts[:tld]
47
+ self.class.parse_tld(host)
48
+ end
49
+
50
+ def tld=(new_tld)
51
+ self.host = "#{subdomain}.#{domain}.#{new_tld}"
40
52
  end
41
53
 
42
54
  private
@@ -47,8 +59,7 @@ module Addressabler
47
59
  end
48
60
 
49
61
  def parse_domain_parts
50
- return @domain_parts if defined? @domain_parts
51
- tld = self.class.parse_tld(host)
62
+ tld = self.tld
52
63
  begin
53
64
  subdomain_parts = host.gsub(/\.#{tld}$/, '').split('.')
54
65
  rescue
@@ -43,4 +43,33 @@ describe Addressabler do
43
43
  uri = Addressable::URI.heuristic_parse("i.am.a.subdomain.co.uk")
44
44
  uri.domain.should == "subdomain"
45
45
  end
46
+
47
+ it "should change stuff when the host changes" do
48
+ uri = Addressable::URI.heuristic_parse("www.google.com")
49
+ uri.tld.should == 'com'
50
+ uri.domain.should == 'google'
51
+ uri.subdomain.should == 'www'
52
+ uri.host = 'www2.google.co.uk'
53
+ uri.tld.should == 'co.uk'
54
+ uri.domain.should == 'google'
55
+ uri.subdomain.should == 'www2'
56
+ end
57
+
58
+ it "should let me change the subdomain" do
59
+ uri = Addressable::URI.heuristic_parse("www.google.com")
60
+ uri.subdomain = 'www2'
61
+ uri.to_s.should == 'http://www2.google.com'
62
+ end
63
+
64
+ it "should let me change the domain" do
65
+ uri = Addressable::URI.heuristic_parse("www.google.com")
66
+ uri.domain = 'amazon'
67
+ uri.to_s.should == 'http://www.amazon.com'
68
+ end
69
+
70
+ it "should let me change the tld" do
71
+ uri = Addressable::URI.heuristic_parse("www.google.com")
72
+ uri.tld = 'co.uk'
73
+ uri.to_s.should == 'http://www.google.co.uk'
74
+ end
46
75
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: addressabler
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Flip Sasser