Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to specify custom TLDs #2

Merged
merged 7 commits into from
Jul 2, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 44 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
# Addressabler

**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.
**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.

## Install

Install using Rubygems:

gem install addressabler
gem install addressabler

Then:

require 'rubygems'
require 'addressabler'
require 'rubygems'
require 'addressabler'

Addressabler will automatically require `addressable/uri`.

Expand All @@ -21,22 +24,22 @@ Addressabler will automatically require `addressable/uri`.

Use `Addressable::URI` like you normally would:

```
```ruby
@uri = Addressable::URI.parse("http://www.google.com/")
@uri.host #=> "www.google.com"
```

Addressabler will add the following properties:

```
```ruby
@uri.tld #=> "com"
@uri.domain #=> "google"
@uri.subdomain #=> "www"
```

You can set these values, as well:

```
```ruby
@uri.tld = "org"
@uri.host #=> "www.google.org"
@uri.domain = "amazon"
Expand All @@ -46,37 +49,61 @@ You can set these values, as well:
```

#### Complex TLD support (thanks to Paul Dix!)
Addressabler copies some of Paul Dix's [Domaintrix](https://github.com/pauldix/domainatrix) TLD code to support fancy TLDs, as well:
Addressabler copies some of Paul Dix's
[Domaintrix](https://github.com/pauldix/domainatrix) TLD code to support fancy
TLDs, as well:

```
```ruby
@uri.host = "www.google.co.uk"
@uri.tld #=> "co.uk"
```

#### Custom TLDs
You can specify custom TLDs - which aren't actually working TLD's on the
internet - for internal usage. One example would be a custom development TLD:

```ruby
Addressable::URI.custom_tlds = {
'dev' => {}, # mydomain.dev
'bar' => { 'foo' => {} } # mydomain.foo.bar
}
```

### Query interface

Addressabler adds a `query_hash` method to `Addressable::URI`s. This makes editing query strings a lot simpler, using a familiar Hash syntax:
Addressabler adds a `query_hash` method to `Addressable::URI`s. This makes
editing query strings a lot simpler, using a familiar Hash syntax:

```
```ruby
@uri.query_hash[:foo] = :bar
@uri.to_s #=> "http://www.google.co.uk/?foo=bar"
```

#### Nested hashes in query strings

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.
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.

Frankly, I don't disagree with anything he has to say on the issue, but it is a problem many people have experienced.
Frankly, I don't disagree with anything he has to say on the issue, but it is a
problem many people have experienced.

*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:
*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:

```
```ruby
@uri.query_hash = {:foo => {:bar => :baz}}
@uri.to_s #=> "http://www.google.co.uk/?foo[bar]=baz"
```

**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.
**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.

That's it. Enjoy.

#### Copyright © 2013 Flip Sasser
#### Copyright © 2013 Flip Sasser
9 changes: 8 additions & 1 deletion lib/addressabler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
require 'addressable/uri'
require 'addressabler/query'

module Addressable
class URI
class << self; attr_accessor :custom_tlds; end
@custom_tlds = {}
end
end

module Addressabler
module ClassMethods
def parse_tld(host)
host = host.to_s.split('.')
tlds = []
sub_hash = Addressabler::TLDS
sub_hash = Addressabler::TLDS.merge(@custom_tlds)
while sub_hash = sub_hash[tld = host.pop]
tlds.unshift(tld)
if sub_hash.has_key? '*'
Expand Down
17 changes: 17 additions & 0 deletions spec/addressabler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@
uri = Addressable::URI.parse("http://www.bart-blabla.cc")
uri.tld.should == 'cc'
end

it "doesn't know non-existing TLDs" do
uri = Addressable::URI.parse("http://www.bart-blabla.foobar")
uri.tld.should == ''
end

it "accepts custom TLDs" do
Addressable::URI.custom_tlds = { 'foobar' => {} }
uri = Addressable::URI.parse("http://www.bart-blabla.foobar")
uri.tld.should == 'foobar'
end

it "accepts nested custom TLDs" do
Addressable::URI.custom_tlds = { 'bar' => { 'foo' => {} } }
uri = Addressable::URI.parse("http://www.bart-blabla.foo.bar")
uri.tld.should == 'foo.bar'
end
end

it "should support adding keys to the query" do
Expand Down