forked from leereilly/swot
-
Notifications
You must be signed in to change notification settings - Fork 17.6k
/
swot.rb
206 lines (194 loc) · 3.85 KB
/
swot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
require 'public_suffix'
module Swot
VERSION = "0.1.0"
# These top-level domains are guaranteed to be academic instituions.
ACADEMIC_TLDS = [
'ac.ae',
'ac.at',
'ac.bd',
'ac.be',
'ac.cr',
'ac.cy',
'ac.fj',
'ac.id',
'ac.il',
'ac.ir',
'ac.jp',
'ac.ke',
'ac.kr',
'ac.ma',
'ac.mu',
'ac.mw',
'ac.mz',
'ac.nz',
'ac.pa',
'ac.pg',
'ac.rs',
'ac.ru',
'ac.rw',
'ac.th',
'ac.tz',
'ac.ug',
'ac.uk',
'ac.yu',
'ac.za',
'ac.zm',
'ac.zw',
'edu',
'edu.af',
'edu.al',
'edu.ar',
'edu.au',
'edu.az',
'edu.ba',
'edu.bb',
'edu.bd',
'edu.bh',
'edu.bi',
'edu.bn',
'edu.bo',
'edu.br',
'edu.bs',
'edu.bt',
'edu.bz',
'edu.cn',
'edu.co',
'edu.cu',
'edu.do',
'edu.dz',
'edu.ec',
'edu.ee',
'edu.eg',
'edu.er',
'edu.es',
'edu.et',
'edu.ge',
'edu.gh',
'edu.gr',
'edu.gt',
'edu.hk',
'edu.hn',
'edu.ht',
'edu.iq',
'edu.jm',
'edu.jo',
'edu.kg',
'edu.kh',
'edu.kn',
'edu.kw',
'edu.ky',
'edu.kz',
'edu.la',
'edu.lb',
'edu.lv',
'edu.ly',
'edu.mk',
'edu.mm',
'edu.mn',
'edu.mo',
'edu.mt',
'edu.mx',
'edu.my',
'edu.ni',
'edu.np',
'edu.om',
'edu.pa',
'edu.pe',
'edu.ph',
'edu.pk',
'edu.pl',
'edu.pr',
'edu.ps',
'edu.pt',
'edu.py',
'edu.qa',
'edu.rs',
'edu.ru',
'edu.sa',
'edu.sd',
'edu.sg',
'edu.sv',
'edu.sy',
'edu.tr',
'edu.tt',
'edu.tw',
'edu.ua',
'edu.uy',
'edu.ve',
'edu.vn',
'edu.ws',
'edu.ye',
'edu.zm',
'vic.edu.au'
]
class << self
# Figure out if an email or domain belongs to academic instituion.
#
# Returns true if the domain name belongs to an academic institution;
# false oterwise.
def is_academic?(text)
return false if text.nil?
text.strip!
begin
domain = get_domain(text)
return false if domain.nil?
if ACADEMIC_TLDS.include?(domain.tld)
true
elsif match_academic_domain?(domain)
true
else
false
end
rescue PublicSuffix::DomainInvalid => di
return false
rescue PublicSuffix::DomainNotAllowed => dna
return false
end
end
# Figure out the institution name based on the email address/domain.
#
# Returns a string with the instution name; nil if nothing is found.
def get_institution_name(text)
text.strip!
text.downcase!
text = text.split("@")[1] if text.include? "@"
domain = PublicSuffix.parse(text)
name_from_academic_domain(domain)
end
# Figure out if a domain name is a know academic institution.
#
# Returns true if the domain name belongs to a known academic instition;
# false otherwise.
def match_academic_domain?(domain)
File.exists?("#{File.expand_path(__FILE__+'/..')}/domains/#{domain.tld}/#{domain.sld}")
end
# Figure out the insitutions' name based on the domain name.
#
# Return the institution name, or nil if not found.
def name_from_academic_domain(domain)
begin
file = File.open("#{File.expand_path(__FILE__+'/..')}/domains/#{domain.tld}/#{domain.sld}", "rb")
contents = file.read
contents.strip!
return contents
rescue
return nil
end
end
# Get the FQDN name from a URL or email address.
#
# Returns a string with the FQDN; nil if there's an error.
def get_domain(text)
text.strip!
return false if text.nil?
text.downcase!
text = text.split("@")[1] if text.include? "@"
begin
domain = PublicSuffix.parse(text)
return domain
rescue
return nil
end
end
end
end