dominatrix 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c6eb591b5b1bce5de59315fa2c8d6e0fa6266a89
4
- data.tar.gz: e0cf4c3e765bd887a73a73af9d50310bac038fdd
3
+ metadata.gz: 6f7dda304ebe0f6acbb19b84cc811183cc9d1ab7
4
+ data.tar.gz: 77c378c2062ebf1c670c7c903c4c4d613cd761d4
5
5
  SHA512:
6
- metadata.gz: 53ec7aa73d8e32bbd84289fed5fb3d8af09b552e1f547df2e886615a5119333e4dff9d23971041448931736cfeb9f9fc617d8a3f8025e322b84beb9c7bdf0fc1
7
- data.tar.gz: bf458fa755a50bf1c852305fba671ba4a63ee314ade92c97b703b952891bfd72fa606ed0e805533185c271d58b512450570fb08ae19586ae2c9a0e074dec32ee
6
+ metadata.gz: f39a2448d7690076771e0f96167bc40f9f70df40e2acb63ac6842551588772c984cf9765eca4719b3cc8f0d29d852a090942f9bae431282444bab55bb84f9cf5
7
+ data.tar.gz: ef9561efae2e81a8e4de83f1752bb87a8c90828460c4bab3e570694db3fa743e90e2af0479bf51f585aeecff988bb30f62003e93274e754ec1650523b5c89cc3
data/README.md CHANGED
@@ -22,8 +22,12 @@ Or install it yourself as:
22
22
 
23
23
  ### Parsing Domains
24
24
 
25
- To parse a domain name from a URI, call the `Dominatrix.parse` method with a
26
- URI object or parseable URI string.
25
+ There are two approaches to parsing domains with Dominatrix: `Dominatrix.parse`
26
+ and `Dominatrix#parse`.
27
+
28
+ #### Dominatrix.parse
29
+
30
+ Call the `Dominatrix.parse` method with a URI object or parseable URI string.
27
31
 
28
32
  ```ruby
29
33
  Dominatrix.parse('http://www.google.com')
@@ -62,6 +66,30 @@ following errors will be raised:
62
66
  parsed.
63
67
  * `Dominatrix::NotFoundError`: the URI does not contain a valid domain name.
64
68
 
69
+ #### Dominatrix#parse
70
+
71
+ Instantiate a `Dominatrix` object by passing in an optional set of valid domain
72
+ extensions. By default, this value is set to `Dominatrix.default_extensions`.
73
+
74
+ ```ruby
75
+ dominatrix = Dominatrix.new
76
+ # => #<Dominatrix:0x007fda8428a8f0 @extensions=#<Set: {".ac", ".com.ac", ".gov.ac", ".mil.ac", ... }>>
77
+
78
+ dominatrix = Dominatrix.new(%w[.com .net].to_set)
79
+ # => #<Dominatrix:0x007fda823861c0 @extensions=#<Set: {".com", ".net"}>>
80
+ ```
81
+
82
+ Once instantiated, call the `#parse` method with a URI object or parseable URI
83
+ string.
84
+
85
+ ```ruby
86
+ dominatrix.parse('http://www.google.com')
87
+ # => "google.com"
88
+
89
+ dominatrix.parse('http://www.google.foo')
90
+ # => Dominatrix::NotFoundError: no matching domain for "http://www.google.foo"
91
+ ```
92
+
65
93
  ### Domain Extensions
66
94
 
67
95
  To see the list of domain extensions built into the gem, call the
@@ -2,49 +2,62 @@ require 'set'
2
2
  require 'uri'
3
3
  require 'dominatrix/version'
4
4
 
5
- module Dominatrix
5
+ class Dominatrix
6
6
  class NotFoundError < StandardError; end
7
7
 
8
- class << self
9
- def parse(uri, extensions = default_extensions)
10
- uri = URI.parse(uri) if uri.is_a?(String)
11
- raise ArgumentError, '`uri` must be a String or URI' unless uri.is_a?(URI)
12
- raise ArgumentError, '`uri` must include a host' if uri.host.nil?
13
- raise ArgumentError, '`extensions` must be Enumerable' unless extensions.is_a?(Enumerable)
8
+ attr_reader :extensions
14
9
 
15
- domain = extract_domain(uri.host, extensions)
16
- raise NotFoundError, "no matching domain for \"#{uri}\"" if domain.nil?
17
- domain
18
- end
10
+ def initialize(extensions = self.class.default_extensions)
11
+ self.extensions = extensions
12
+ end
19
13
 
20
- def default_extensions
21
- @default_domains ||= begin
22
- file = File.join(File.dirname(__FILE__), 'extensions.txt')
23
- File.readlines(file).map(&:strip).delete_if(&:empty?).to_set
24
- end
25
- end
14
+ def parse(uri)
15
+ uri = URI.parse(uri) if uri.is_a?(String)
16
+ raise ArgumentError, '`uri` must be a String or URI' unless uri.is_a?(URI)
17
+ raise ArgumentError, '`uri` must include a host' if uri.host.nil?
26
18
 
27
- private
19
+ domain = extract_domain(uri.host)
20
+ raise NotFoundError, "no matching domain for \"#{uri}\"" if domain.nil?
21
+ domain
22
+ end
28
23
 
29
- def extract_domain(host, extensions)
30
- parts = host.split('.')
31
- last_found_domain = nil
24
+ private
32
25
 
33
- parts.size.times.each do |n|
34
- offset = parts.size - n - 1
35
- lookup = '.' + parts[offset..-1].join('.')
26
+ def extensions=(extensions)
27
+ raise ArgumentError, '`extensions` must be Enumerable' unless extensions.is_a?(Enumerable)
28
+ raise ArgumentError, '`extensions` must all start with "."' unless extensions.all? { |s| s.start_with?('.') }
29
+ @extensions = extensions
30
+ end
36
31
 
37
- if extensions.include?(lookup)
38
- last_found_domain = lookup
39
- elsif last_found_domain
40
- last_found_domain = parts[offset] + last_found_domain
41
- break
42
- else
43
- break
44
- end
32
+ def extract_domain(host)
33
+ parts = host.split('.')
34
+ last_found_domain = nil
35
+
36
+ parts.size.times.each do |n|
37
+ offset = parts.size - n - 1
38
+ lookup = '.' + parts[offset..-1].join('.')
39
+
40
+ if extensions.include?(lookup)
41
+ last_found_domain = lookup
42
+ elsif last_found_domain
43
+ last_found_domain = parts[offset] + last_found_domain
44
+ break
45
+ else
46
+ break
45
47
  end
48
+ end
49
+
50
+ last_found_domain
51
+ end
52
+
53
+ def self.parse(uri, extensions = default_extensions)
54
+ new(extensions).parse(uri)
55
+ end
46
56
 
47
- last_found_domain
57
+ def self.default_extensions
58
+ @default_domains ||= begin
59
+ file = File.join(File.dirname(__FILE__), 'extensions.txt')
60
+ File.readlines(file).map(&:strip).delete_if(&:empty?).to_set
48
61
  end
49
62
  end
50
63
  end
@@ -1,3 +1,3 @@
1
- module Dominatrix
2
- VERSION = '1.0.0'
1
+ class Dominatrix
2
+ VERSION = '1.1.0'
3
3
  end
@@ -1,4 +1,4 @@
1
- .ac
1
+ .ac
2
2
  .com.ac
3
3
  .gov.ac
4
4
  .mil.ac
@@ -9,7 +9,7 @@
9
9
  .ad
10
10
  .nom.ad
11
11
  .ae
12
- امارات.
12
+ .امارات
13
13
  .co.ae
14
14
  .net.ae
15
15
  .org.ae
@@ -550,7 +550,7 @@
550
550
  .web.do
551
551
  .domains
552
552
  .dz
553
- الجزائر.
553
+ .الجزائر
554
554
  .com.dz
555
555
  .art.dz
556
556
  .asso.dz
@@ -582,7 +582,7 @@
582
582
  .org.ee
583
583
  .pri.ee
584
584
  .eg
585
- مصر.
585
+ .مصر
586
586
  .com.eg
587
587
  .edu.eg
588
588
  .eun.eg
@@ -868,7 +868,7 @@
868
868
  .in
869
869
  .ભારત
870
870
  .भारत
871
- بھارت.
871
+ .بھارت
872
872
  .ਭਾਰਤ
873
873
  .இந்தியா
874
874
  .co.in
@@ -905,7 +905,7 @@
905
905
  .org.iq
906
906
  .tv.iq
907
907
  .ir
908
- ایران.
908
+ .ایران
909
909
  .ac.ir
910
910
  .co.ir
911
911
  .id.ir
@@ -930,7 +930,7 @@
930
930
  .jm
931
931
  .com.jm
932
932
  .jo
933
- الاردن.
933
+ .الاردن
934
934
  .com.jo
935
935
  .edu.jo
936
936
  .gov.jo
@@ -1140,7 +1140,7 @@
1140
1140
  .sch.ly
1141
1141
  .mex.com
1142
1142
  .ma
1143
- المغرب.
1143
+ .المغرب
1144
1144
  .co.ma
1145
1145
  .net.ma
1146
1146
  .org.ma
@@ -1268,7 +1268,7 @@
1268
1268
  .edu.mx
1269
1269
  .gob.mx
1270
1270
  .my
1271
- مليسيا.
1271
+ .مليسيا
1272
1272
  .com.my
1273
1273
  .net.my
1274
1274
  .org.my
@@ -1377,7 +1377,7 @@
1377
1377
  .health.nz
1378
1378
  .parliament.nz
1379
1379
  .om
1380
- عمان.
1380
+ .عمان
1381
1381
  .co.om
1382
1382
  .com.om
1383
1383
  .ac.om
@@ -1438,7 +1438,7 @@
1438
1438
  .pictures
1439
1439
  .pink
1440
1440
  .pk
1441
- پاکستان.
1441
+ .پاکستان
1442
1442
  .com.pk
1443
1443
  .net.pk
1444
1444
  .org.pk
@@ -1665,7 +1665,7 @@
1665
1665
  .productions
1666
1666
  .properties
1667
1667
  .ps
1668
- فلسطين.
1668
+ .فلسطين
1669
1669
  .com.ps
1670
1670
  .net.ps
1671
1671
  .org.ps
@@ -1697,7 +1697,7 @@
1697
1697
  .org.py
1698
1698
  .qc.com
1699
1699
  .qa
1700
- قطر.
1700
+ .قطر
1701
1701
  .com.qa
1702
1702
  .net.qa
1703
1703
  .org.qa
@@ -1781,7 +1781,7 @@
1781
1781
  .org.sa
1782
1782
  .pub.sa
1783
1783
  .sch.sa
1784
- السعودية.
1784
+ .السعودية
1785
1785
  .com.sb
1786
1786
  .net.sb
1787
1787
  .org.sb
@@ -1911,7 +1911,7 @@
1911
1911
  .red.sv
1912
1912
  .sx
1913
1913
  .sy
1914
- سوريا.
1914
+ .سوريا
1915
1915
  .systems
1916
1916
  .co.sz
1917
1917
  .sz
@@ -1965,7 +1965,7 @@
1965
1965
  .org.tl
1966
1966
  .tm
1967
1967
  .tn
1968
- تونس.
1968
+ .تونس
1969
1969
  .com.tn
1970
1970
  .net.tn
1971
1971
  .org.tn
@@ -2227,7 +2227,7 @@
2227
2227
  .xxx
2228
2228
  .xyz
2229
2229
  .ye
2230
- اليمن.
2230
+ .اليمن
2231
2231
  .co.ye
2232
2232
  .com.ye
2233
2233
  .gov.ye
@@ -2,9 +2,7 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  RSpec.describe Dominatrix do
5
- subject { described_class }
6
-
7
- describe '.parse' do
5
+ describe '#parse' do
8
6
  shared_examples_for 'a valid URL' do |domain|
9
7
  it 'returns the domain when given a string' do
10
8
  expect(subject.parse(url)).to eq domain
@@ -48,24 +46,49 @@ RSpec.describe Dominatrix do
48
46
  end
49
47
 
50
48
  describe 'when given a custom list of extensions' do
49
+ subject { described_class.new(extensions) }
50
+
51
51
  let(:extensions) { %w[.uk .co.uk].to_set }
52
52
 
53
53
  it 'returns the domain for a valid URI' do
54
- expect(subject.parse('http://www.example.co.uk', extensions)).to eq 'example.co.uk'
54
+ expect(subject.parse('http://www.example.co.uk')).to eq 'example.co.uk'
55
55
  end
56
56
 
57
57
  it 'raises an error for an invalid URI' do
58
- expect { subject.parse('http://www.example.com', extensions) }.to raise_error Dominatrix::NotFoundError
58
+ expect { subject.parse('http://www.example.com') }.to raise_error Dominatrix::NotFoundError
59
59
  end
60
60
  end
61
61
  end
62
62
 
63
+ describe '.parse' do
64
+ subject { described_class }
65
+
66
+ let(:url) { 'http://www.google.com' }
67
+
68
+ it 'delegates to a new instance' do
69
+ instance = subject.new
70
+ allow(subject).to receive(:new).and_return(instance)
71
+ expect(instance).to receive(:parse).with(url)
72
+ subject.parse(url)
73
+ end
74
+
75
+ it 'includes the provided extension list' do
76
+ extensions = %w[.uk .co.uk].to_set
77
+ instance = subject.new(extensions)
78
+ allow(subject).to receive(:new).with(extensions).and_return(instance)
79
+ expect(instance).to receive(:parse).with(url)
80
+ subject.parse(url, extensions)
81
+ end
82
+ end
83
+
63
84
  describe '.default_extensions' do
85
+ subject { described_class }
86
+
64
87
  it 'returns a set' do
65
88
  expect(subject.default_extensions).to be_a Set
66
89
  end
67
90
 
68
- it 'prefixes each extension with a dot', skip: 'RTL extensions not working' do
91
+ it 'prefixes each extension with a dot' do
69
92
  expect(subject.default_extensions).to all start_with('.')
70
93
  end
71
94
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dominatrix
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Huggins
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-24 00:00:00.000000000 Z
11
+ date: 2014-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler