GithubHelp home page GithubHelp logo

flipsasser / addressabler Goto Github PK

View Code? Open in Web Editor NEW
21.0 2.0 10.0 317 KB

Addressabler extends the Addressable::URI class to provide information about, and manipulation of, URI strings.

License: MIT License

Ruby 100.00%

addressabler's Introduction

Addressabler

Addressabler extends the Addressable::URI class by adding TLD parsing, domain and subdomain parsing, query modification, and restoring setting of nested hashes to query strings.

Install

Add Addressabler to your Gemfile:

gem "addressabler", ">= 0.1"

Addressabler will automatically require addressable/uri.

Usage

Domain, TLD, and subdomain parsing

Use Addressable::URI like you normally would:

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

Addressabler will add the following properties:

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

You can set these values, as well:

@uri.tld = "org"
@uri.host #=> "www.google.org"
@uri.domain = "amazon"
@uri.host #=> "www.amazon.org"
@uri.subdomain = "developers"
@uri.host #=> "developers.amazon.org"

Complex TLD support (thanks to Paul Dix!)

Addressabler copies some of Paul Dix's Domaintrix TLD code to support fancy TLDs, as well:

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

Private / Public TLDs

By default, Addressabler knows about ICANN public TLDs. There are, however, lots and lots of private TLDs that companies have registered. For example, as Dom Hodgson points out, "blogspot.com" is a TLD by private, non-ICANN standards which are applied by the Mozilla foundation to the TLD list.

As such, Addressabler defaults to parsing the ICANN public TLDS (Addressabler.public_tlds) but can easily be instructed to look at private TLDs like so:

Addressabler.use_private_tlds = true

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:

Addressabler.custom_tlds = {
  'dev' => {},              # mydomain.dev
  'bar' => { 'foo' => {} }  # mydomain.foo.bar
}

Query interface

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

@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, feels rather strongly that Rails got it wrong 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.

As such, since Rack already supports building nested hashes "the Rails Way" (shudder), I added support for assigning nested hashes to Addressable::URIs 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:

@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, there's a better alternative to nested hashes in query strings, so try that before you install this library.

That's it. Enjoy.

Contributors THANKS GUYS

Super special thankses to

Copyright © 2013 Flip Sasser

addressabler's People

Contributors

jeanmertz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

addressabler's Issues

How do I run the specs?

I wanted to submit my contribution to the gem, but I can't get the specs to run. I've tried rspec spec/, rake spec and rake test, but none seams to work. Thanks.

.cc domains not parsed properly

[50] pry(main)> uri = Addressable::URI.parse('http://www.bart-blabla.cc')
=> #<Addressable::URI:0x25d2c38 URI:http://www.bart-blabla.cc>
[51] pry(main)> uri.tld
=> ""
[52] pry(main)> uri.domain
=> "cc"

But I expected tld to be cc and domain to be bart-blabla.cc

Another example:

[53] pry(main)> uri = Addressable::URI.parse('http://www.abc.cc')
=> #<Addressable::URI:0x2d99980 URI:http://www.abc.cc>
[54] pry(main)> uri.tld
=> ""
[55] pry(main)> uri.domain
=> "cc"

Is this a known issue?

blogspot domains as tld's

When I try to parse a domain with blogspot it gives me the blogspot url as a tld (this also happens with amazonaws sometimes.

irb(main):015:0> url = 'http://fwefweewf.blogspot.com'
=> "http://fwefweewf.blogspot.com"
irb(main):016:0> @uri = Addressable::URI.parse(url)
=> #<Addressable::URI:0x3fcd6d92e828 URI:http://fwefweewf.blogspot.com>
irb(main):017:0> @uri.tld
=> "blogspot.com"

Hope you can debug this :)

Version 0.1.0?

It looks like version 0.1.0 of addressabler was never released to rubygems even though the documentation references it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.