Ruby域名&地址解析

 

Public Suffix

 

## Installation

$ gem install public_suffix

 

## Basic Usage

```ruby
domain = PublicSuffix.parse("google.com")
# => #<PublicSuffix::Domain>
domain.tld
# => "com"
domain.sld
# => "google"
domain.trd
# => nil
domain.domain
# => "google.com"
domain.subdomain
# => nil
```

 

Example domain with subdomains.

```ruby
domain = PublicSuffix.parse("www.google.com")
# => #<PublicSuffix::Domain>
domain.tld
# => "com"
domain.sld
# => "google"
domain.trd
# => "www"
domain.domain
# => "google.com"
domain.subdomain
# => "www.google.com"
```

 

Simple validation example.

```ruby
PublicSuffix.valid?("google.com")
# => true

PublicSuffix.valid?("www.google.com")
# => true

PublicSuffix.valid?("x.yz")
# => false
```

 

```ruby
# Parse a standard domain name
domain = PublicSuffix.parse("www.google.com")
# => #<PublicSuffix::Domain>
domain.tld
# => "com"

 


## Private domains

This library has support for switching off support for private (non-ICANN) domains

```ruby
# Parse a domain on a private TLD
domain = PublicSuffix.parse("something.blogspot.com")
# => #<PublicSuffix::Domain>
domain.tld
# => "blogspot.com"

# Disable support for private TLDs
PublicSuffix::List.private_domains = false
# => #<PublicSuffix::List>
domain = PublicSuffix.parse("something.blogspot.com")
# => #<PublicSuffix::Domain>
domain.tld
# => "com"
```

 

# Addressable

 

# Install

$ sudo gem install addressable

$ sudo apt-get install idn # Debian/Ubuntu
$ sudo brew install libidn # OS X
$ sudo gem install idn-ruby

 

# Reference

- {Addressable::URI}
- {Addressable::Template}

# Example usage

```ruby
require "addressable/uri"

uri = Addressable::URI.parse("http://example.com/path/to/resource/")
uri.scheme
#=> "http"
uri.host
#=> "example.com"
uri.path
#=> "/path/to/resource/"

uri = Addressable::URI.parse("http://www.詹姆斯.com/")
uri.normalize
#=> #<Addressable::URI:0xc9a4c8 URI:http://www.xn--8ws00zhy3a.com/>
```
require "addressable/template"

template = Addressable::Template.new("http://example.com/{?query*}/")
template.expand({
  "query" => {
    'foo' => 'bar',
    'color' => 'red'
  }
})
#=> #<Addressable::URI:0xc9d95c URI:http://example.com/?foo=bar&color=red>

template = Addressable::Template.new("http://example.com/{?one,two,three}/")
template.partial_expand({"one" => "1", "three" => 3}).pattern
#=> "http://example.com/?one=1{&two}&three=3"

template = Addressable::Template.new(
  "http://{host}{/segments*}/{?one,two,bogus}{#fragment}"
)
uri = Addressable::URI.parse(
  "http://example.com/a/b/c/?one=1&two=2#foo"
)
template.extract(uri)
#=>
# {
#   "host" => "example.com",
#   "segments" => ["a", "b", "c"],
#   "one" => "1",
#   "two" => "2",
#   "fragment" => "foo"
# }

 

 

你可能感兴趣的:(uri,psuffix)