lazy_domain 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.
- checksums.yaml +7 -0
- data/LICENSE.md +19 -0
- data/README.md +101 -0
- data/Rakefile +7 -0
- data/lib/lazy_domain/version.rb +3 -0
- data/lib/lazy_domain.rb +75 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 22211abf594c3ca41c920dedf2556c9c8181fb06
|
4
|
+
data.tar.gz: 2bb691ae29b03dfb5c262c235f23c663c65b65c4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ff14b0436b383ca2266a878246e9b9ba43efe424b2e5a165ac0bd4c462436564254cc7f2c0a253c2056a1e87e80fc4986020ee7edfa52b61f4517fd157f1bf33
|
7
|
+
data.tar.gz: 2212be8528f9e956ee427e393a00c8307d73baf3f4a52a3dc98c0359b548ac26bc586b5d0dff6af7a78f4bb913a487f7d70c963e4b3bd7590262f0cd13778d42
|
data/LICENSE.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
Copyright (c) 2015 John Mason
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
6
|
+
in the Software without restriction, including without limitation the rights
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
9
|
+
furnished to do so, subject to the following conditions:
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
# LazyDomain
|
2
|
+
|
3
|
+
Parse varying urls with [PublicSuffix](https://github.com/weppos/publicsuffix-ruby) the easy way.
|
4
|
+
|
5
|
+
## Description
|
6
|
+
|
7
|
+
The PublicSuffix gem works great if you give it nice, clean urls WITHOUT a transfer protocol. But what if your application has one (or multiple) urls with or without protocols?
|
8
|
+
|
9
|
+
LazyDomain assumes it could be either way and uses URI to make up the difference. Because URI can only parse a url WITH a protocol, Lazy Domain automatically adds 'http://' if it is not already present in a url. Now, you can easily use PublicSuffix for varying urls.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
```
|
14
|
+
gem install lazy_domain
|
15
|
+
|
16
|
+
```
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
Because LazyDomain is simply a wrapper for [PublicSuffix](https://github.com/weppos/publicsuffix-ruby), the usage is exactly the same.
|
21
|
+
|
22
|
+
## Basic Usage
|
23
|
+
|
24
|
+
Example domain without subdomains.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
domain = LazyDomain.parse("google.com")
|
28
|
+
# => #<PublicSuffix::Domain>
|
29
|
+
domain.tld
|
30
|
+
# => "com"
|
31
|
+
domain.sld
|
32
|
+
# => "google"
|
33
|
+
domain.trd
|
34
|
+
# => nil
|
35
|
+
domain.domain
|
36
|
+
# => "google.com"
|
37
|
+
domain.subdomain
|
38
|
+
# => nil
|
39
|
+
```
|
40
|
+
|
41
|
+
Example domain with subdomains.
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
domain = LazyDomain.parse("www.google.com")
|
45
|
+
# => #<PublicSuffix::Domain>
|
46
|
+
domain.tld
|
47
|
+
# => "com"
|
48
|
+
domain.sld
|
49
|
+
# => "google"
|
50
|
+
domain.trd
|
51
|
+
# => "www"
|
52
|
+
domain.domain
|
53
|
+
# => "google.com"
|
54
|
+
domain.subdomain
|
55
|
+
# => "www.google.com"
|
56
|
+
```
|
57
|
+
|
58
|
+
Simple validation example.
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
LazyDomain.valid?("google.com")
|
62
|
+
# => true
|
63
|
+
|
64
|
+
LazyDomain.valid?("www.google.com")
|
65
|
+
# => true
|
66
|
+
|
67
|
+
LazyDomain.valid?("x.yz")
|
68
|
+
# => false
|
69
|
+
```
|
70
|
+
|
71
|
+
## Advanced Usage
|
72
|
+
|
73
|
+
You can do everything mentioned above, but the magic of LazyDomain is you can now pass different types of urls!
|
74
|
+
|
75
|
+
For example, all of this...
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
domain = LazyDomain.parse("google.com")
|
79
|
+
|
80
|
+
domain = LazyDomain.parse("http://google.com")
|
81
|
+
|
82
|
+
domain = LazyDomain.parse("ftp://google.com")
|
83
|
+
|
84
|
+
domain = LazyDomain.parse("somethingcrazy://google.com")
|
85
|
+
|
86
|
+
domain = LazyDomain.parse("http://abc.defg.hijk.google.com")
|
87
|
+
|
88
|
+
domain = LazyDomain.parse("http://abc.defg.hijk.google.com/asubdirectory/andanother")
|
89
|
+
|
90
|
+
```
|
91
|
+
|
92
|
+
...returns something like this.
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
|
96
|
+
=> #<PublicSuffix::Domain:0x0055c2344f0168 @sld="google", @tld="com", @trd="abc.defg.hijk">
|
97
|
+
|
98
|
+
|
99
|
+
```
|
100
|
+
|
101
|
+
Enjoy!
|
data/Rakefile
ADDED
data/lib/lazy_domain.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'public_suffix'
|
3
|
+
|
4
|
+
# Add http:// if a protocol isn't already there.
|
5
|
+
module AutoHTTP
|
6
|
+
# Looks for any character before '://'
|
7
|
+
TP_REGEX = %r{[\w]+(\b:\/\/)}
|
8
|
+
|
9
|
+
# Returns true if there is any character before '://'' in a url.
|
10
|
+
# ie. 'something://google.com' or 'xyz://google.com'
|
11
|
+
#
|
12
|
+
# Returns false if somehting like '://google.com' or 'google.com'.
|
13
|
+
def hastp(str)
|
14
|
+
!(str =~ TP_REGEX).nil?
|
15
|
+
end
|
16
|
+
|
17
|
+
# Add 'http://' if something is not already present.
|
18
|
+
def autohttp(str)
|
19
|
+
str.prepend('http://') unless hastp(str)
|
20
|
+
str
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Make parsing PublicSuffix domains easier.
|
25
|
+
module LazyDomain
|
26
|
+
class << self
|
27
|
+
include AutoHTTP
|
28
|
+
|
29
|
+
# Because URI cannot parse anything without a transfer protocol,
|
30
|
+
# this automagically adds one if not present.
|
31
|
+
#
|
32
|
+
# This method always PublicSuffix to parse varying urls that may or
|
33
|
+
# may not have a protocol.
|
34
|
+
def uri_host(str)
|
35
|
+
URI.parse(autohttp(str)).host
|
36
|
+
end
|
37
|
+
|
38
|
+
# All PublicSuffix methods are available.
|
39
|
+
# PublicSuffix reference is here: http://bit.ly/1WTiwGR
|
40
|
+
# -----------------------------------------------------------
|
41
|
+
|
42
|
+
# Example:
|
43
|
+
# domain = LazyDomain.parse(google.com)
|
44
|
+
#
|
45
|
+
# domain.tld
|
46
|
+
# => 'com'
|
47
|
+
#
|
48
|
+
# domain.sld
|
49
|
+
# => 'google'
|
50
|
+
#
|
51
|
+
# domain.trd
|
52
|
+
# => nil
|
53
|
+
#
|
54
|
+
# domain.domain
|
55
|
+
# => 'google.com'
|
56
|
+
#
|
57
|
+
# domain.subdomain # => nil
|
58
|
+
def parse(str)
|
59
|
+
PublicSuffix.parse(uri_host(str))
|
60
|
+
end
|
61
|
+
|
62
|
+
# Example:
|
63
|
+
# LazyDomain.valid?("google.com")
|
64
|
+
# => true
|
65
|
+
#
|
66
|
+
# LazyDomain.valid?("www.google.com")
|
67
|
+
# => true
|
68
|
+
|
69
|
+
# LazyDomain.valid?("x.yz")
|
70
|
+
# => false
|
71
|
+
def valid?(str)
|
72
|
+
PublicSuffix.valid?(uri_host(str))
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lazy_domain
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Mason
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: public_suffix
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.9'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.3'
|
69
|
+
description: The PublicSuffix gem works great if you give it nice, clean domains WITHOUT
|
70
|
+
a transfer protocol. But what if your application has one (or multiple) urls with
|
71
|
+
or without protocols? LazyDomain assumes it could be either way and uses URI to
|
72
|
+
make up the difference. Because URI can only parse a url WITH a protocol, Lazy Domain
|
73
|
+
automatically adds 'http://' if it is not already present in a url. Now you can
|
74
|
+
easily use PublicSuffix for varying urls.
|
75
|
+
email: mace2345@gmail.com
|
76
|
+
executables: []
|
77
|
+
extensions: []
|
78
|
+
extra_rdoc_files: []
|
79
|
+
files:
|
80
|
+
- LICENSE.md
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- lib/lazy_domain.rb
|
84
|
+
- lib/lazy_domain/version.rb
|
85
|
+
homepage: http://github.com/m8ss/lazy_domain
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.4.5
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Parse multiple URIs with PublicSuffix the easy way.
|
109
|
+
test_files: []
|