gman 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,22 @@
1
+ # Contributing to Gman
2
+
3
+ ## How to contribute
4
+
5
+ 1. Fork the project
6
+ 2. Create a descriptive branch
7
+ 3. Make your change
8
+ 4. Submit a pull request
9
+
10
+ ## Code
11
+
12
+ Open an issue, or submit a pull request
13
+
14
+ ## Domains
15
+
16
+ Domains live in `./lib/domains.yml` as a bulleted list of TLDs and SLD+TLDs.
17
+
18
+ Right now, the only valid government top level domains (TLDs), represent the US government and are `.gov`, and `.mil`.
19
+
20
+ Secondary domains (e.g., `gov.uk`, or `mil.au`) detect non-US government entities.
21
+
22
+ To add or remove a domain from the list of known government domains, simply edit the `domains.yml` file.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'public_suffix'
4
+
5
+ group :development do
6
+ gem "rake"
7
+ gem "shoulda"
8
+ gem "rdoc"
9
+ gem "bundler"
10
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Ben Balter
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Gman Gem
2
+
3
+ A ruby gem to check if the owner of a given email address or website is working for THE MAN (a.k.a verifies government domains).
4
+
5
+ ## Installation
6
+
7
+ Gman is a Ruby gem, so you'll need a little Ruby-fu to get it working. Simply
8
+
9
+ `gem install gman`
10
+
11
+ Or add this to your `Gemfile` before doing a `bundle install`:
12
+
13
+ `gem 'gman'`
14
+
15
+ ## Usage
16
+
17
+ ### Verify email addresses
18
+
19
+ ```ruby
20
+ Gman::is_government? "foo@bar.gov" #true
21
+ Gman::is_government? "foo@bar.com" #false
22
+ ```
23
+ ### Verify domain
24
+
25
+ ```ruby
26
+ Gman::is_government? "http://foo.bar.gov" #true
27
+ Gman::is_government? "foo.bar.gov" #true
28
+ Gman::is_government? "foo.gov" #true
29
+ Gman::is_government? "foo.biz" #false
30
+ ```
31
+
32
+ ## Contributing
33
+
34
+ Contributions welcome! Please see [the contribution guidelines](contributing.md) for code contributions or for details on how to add, update, or delete government domains.
35
+
36
+ ## Credits
37
+
38
+ Heavily inspired by [swot](https://github.com/leereilly/swot). Thanks @leereily!
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'rake/testtask'
13
+ Rake::TestTask.new(:test) do |test|
14
+ test.libs << 'lib' << 'test'
15
+ test.pattern = 'test/**/test_*.rb'
16
+ test.verbose = true
17
+ end
18
+
19
+ task :console do
20
+ exec "irb -r ./lib/gman.rb"
21
+ end
data/gman.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "gman"
3
+ s.summary = "Check if a given domain or email address belong to a governemnt entity"
4
+ s.description = "A ruby gem to check if the owner of a given email address is working for THE MAN."
5
+ s.version = "0.0.1"
6
+ s.authors = ["Ben Balter"]
7
+ s.date = "2013-08-23"
8
+ s.email = "ben.balter@github.com"
9
+ s.homepage = "https://github.com/benbalter/gman"
10
+ s.licenses = ["MIT"]
11
+
12
+ s.files = [
13
+ "Gemfile",
14
+ "README.md",
15
+ "CONTRIBUTING.md",
16
+ "LICENSE",
17
+ "lib/gman.rb",
18
+ "lib/domains.yml",
19
+ "script/build",
20
+ "script/release",
21
+ "gman.gemspec",
22
+ "test/helper.rb",
23
+ "test/test_gman.rb",
24
+ "Rakefile",
25
+ ".gitignore"
26
+ ]
27
+ s.require_paths = ["lib"]
28
+ s.add_dependency( "public_suffix" )
29
+ end
data/lib/domains.yml ADDED
@@ -0,0 +1,184 @@
1
+ ---
2
+ - fed.us
3
+ - gc.ca
4
+ - gob.bo
5
+ - gob.cl
6
+ - gob.do
7
+ - gob.ec
8
+ - gob.es
9
+ - gob.gt
10
+ - gob.hn
11
+ - gob.mx
12
+ - gob.pa
13
+ - gob.pe
14
+ - gob.pk
15
+ - gouv.bj
16
+ - gouv.ci
17
+ - gouv.fr
18
+ - gouv.ht
19
+ - gouv.km
20
+ - gouv.ml
21
+ - gouv.rw
22
+ - gouv.sn
23
+ - gov
24
+ - gov.ac
25
+ - gov.ae
26
+ - gov.af
27
+ - gov.al
28
+ - gov.as
29
+ - gov.au
30
+ - gov.az
31
+ - gov.ba
32
+ - gov.bb
33
+ - gov.bf
34
+ - gov.bh
35
+ - gov.bm
36
+ - gov.bo
37
+ - gov.br
38
+ - gov.bs
39
+ - gov.bt
40
+ - gov.by
41
+ - gov.bz
42
+ - gov.cd
43
+ - gov.cl
44
+ - gov.cm
45
+ - gov.cn
46
+ - gov.co
47
+ - gov.cu
48
+ - gov.cx
49
+ - gov.dm
50
+ - gov.do
51
+ - gov.dz
52
+ - gov.ec
53
+ - gov.ee
54
+ - gov.eg
55
+ - gov.ge
56
+ - gov.gg
57
+ - gov.gh
58
+ - gov.gi
59
+ - gov.gn
60
+ - gov.gr
61
+ - gov.hk
62
+ - gov.ie
63
+ - gov.im
64
+ - gov.in
65
+ - gov.iq
66
+ - gov.ir
67
+ - gov.is
68
+ - gov.it
69
+ - gov.je
70
+ - gov.jo
71
+ - gov.kg
72
+ - gov.ki
73
+ - gov.km
74
+ - gov.kn
75
+ - gov.kp
76
+ - gov.ky
77
+ - gov.kz
78
+ - gov.la
79
+ - gov.lb
80
+ - gov.lc
81
+ - gov.lk
82
+ - gov.lr
83
+ - gov.lt
84
+ - gov.lv
85
+ - gov.ly
86
+ - gov.ma
87
+ - gov.me
88
+ - gov.mg
89
+ - gov.mk
90
+ - gov.ml
91
+ - gov.mn
92
+ - gov.mo
93
+ - gov.mr
94
+ - gov.mu
95
+ - gov.mv
96
+ - gov.mw
97
+ - gov.my
98
+ - gov.ng
99
+ - gov.nr
100
+ - gov.ph
101
+ - gov.pk
102
+ - gov.pl
103
+ - gov.pn
104
+ - gov.pr
105
+ - gov.ps
106
+ - gov.pt
107
+ - gov.py
108
+ - gov.qa
109
+ - gov.rs
110
+ - gov.ru
111
+ - gov.rw
112
+ - gov.sa
113
+ - gov.sb
114
+ - gov.sc
115
+ - gov.sd
116
+ - gov.sg
117
+ - gov.sh
118
+ - gov.sl
119
+ - gov.st
120
+ - gov.sx
121
+ - gov.sy
122
+ - gov.tj
123
+ - gov.tl
124
+ - gov.tm
125
+ - gov.tn
126
+ - gov.to
127
+ - gov.tt
128
+ - gov.tw
129
+ - gov.ua
130
+ - gov.vc
131
+ - gov.ve
132
+ - gov.vn
133
+ - gov.ws
134
+ - gub.uy
135
+ - mil
136
+ - mil.ac
137
+ - mil.ae
138
+ - mil.al
139
+ - mil.az
140
+ - mil.ba
141
+ - mil.bo
142
+ - mil.br
143
+ - mil.by
144
+ - mil.cl
145
+ - mil.cn
146
+ - mil.co
147
+ - mil.do
148
+ - mil.ec
149
+ - mil.eg
150
+ - mil.ge
151
+ - mil.gh
152
+ - mil.gt
153
+ - mil.hn
154
+ - mil.id
155
+ - mil.in
156
+ - mil.iq
157
+ - mil.jo
158
+ - mil.kg
159
+ - mil.km
160
+ - mil.kr
161
+ - mil.kz
162
+ - mil.lv
163
+ - mil.mg
164
+ - mil.mv
165
+ - mil.my
166
+ - mil.no
167
+ - mil.pe
168
+ - mil.ph
169
+ - mil.pl
170
+ - mil.py
171
+ - mil.qa
172
+ - mil.ru
173
+ - mil.rw
174
+ - mil.sh
175
+ - mil.st
176
+ - mil.sy
177
+ - mil.tj
178
+ - mil.tm
179
+ - mil.to
180
+ - mil.tw
181
+ - mil.tz
182
+ - mil.uy
183
+ - mil.vc
184
+ - mil.ve
data/lib/gman.rb ADDED
@@ -0,0 +1,69 @@
1
+ require 'public_suffix'
2
+ require 'yaml'
3
+
4
+ module Gman
5
+
6
+ VERSION='0.0.1'
7
+
8
+ class << self
9
+
10
+ # Normalizes and checks if a given string represents a governemnt domain
11
+ # Possible strings to test:
12
+ # ".gov"
13
+ # "foo.gov"
14
+ # "foo@bar.gov"
15
+ # "foo.gov.uk"
16
+ # "http://foo.bar.gov"
17
+ #
18
+ # Returns boolean true if a government domain
19
+ def is_government?(text)
20
+ return false if text.nil?
21
+ text.strip!
22
+
23
+ begin
24
+ domain = get_domain(text)
25
+ return false if domain.nil?
26
+
27
+ match_government_domain?(domain)
28
+
29
+ rescue PublicSuffix::DomainInvalid => di
30
+ false
31
+
32
+ rescue PublicSuffix::DomainNotAllowed => dna
33
+ false
34
+ end
35
+
36
+ end
37
+
38
+ # Checks the TLD (e.g., .gov) or TLD+SLD (e.g., .gov.uk)
39
+ # Against list of known government domains
40
+ def match_government_domain?(domain)
41
+ domains.include?(domain.tld) || domains.include?("#{domain.tld}.#{domain.sld}")
42
+ end
43
+
44
+ # Parses YML domain list into an array
45
+ # TODO: Cache this
46
+ #
47
+ # Returns an array of TLD and TLD+SLD known government domains
48
+ def domains
49
+ YAML.load_file File.expand_path("../domains.yml", __FILE__)
50
+ end
51
+
52
+ # Get the FQDN name from a URL or email address.
53
+ #
54
+ # Returns a string with the FQDN; nil if there's an error.
55
+ # Source: https://github.com/leereilly/swot/blob/master/lib/swot.rb#L190
56
+ def get_domain(text)
57
+ PublicSuffix.parse text.downcase.match(domain_regex).captures.first
58
+ rescue
59
+ return nil
60
+ end
61
+
62
+ private
63
+
64
+ # Source: https://github.com/leereilly/swot/blob/master/lib/swot.rb#L202
65
+ def domain_regex
66
+ /([^@\/:]+)[:\d]*$/
67
+ end
68
+ end
69
+ end
data/script/build ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ # Propegates an initial list of best-guess government domains
3
+
4
+ require "public_suffix"
5
+ require "yaml"
6
+
7
+ # https://gist.github.com/benbalter/6147066
8
+ REGEX = /(\.g[ou]{1,2}(v|b|vt)|\.mil|\.gc|\.fed)(\.[a-z]{2})?$/i
9
+
10
+ YAML_FILE = File.dirname(__FILE__) + "/../lib/domains.yml"
11
+ domains = YAML.load_file YAML_FILE
12
+ domains = [] unless domains
13
+
14
+ PublicSuffix::List.default.each do |rule|
15
+ domain = nil
16
+
17
+ if rule.parts.length == 1
18
+ domain = rule.parts.first if ".#{rule.value}" =~ REGEX
19
+ else
20
+ domain = rule.parts.pop(2).join(".") if ".#{rule.value}" =~ REGEX
21
+ end
22
+
23
+ domains.push domain unless domain.nil? or domains.include? domain
24
+ end
25
+
26
+ domains = domains.sort
27
+ File.open(YAML_FILE, 'w+') {|f| f.write(domains.to_yaml)}
data/script/release ADDED
@@ -0,0 +1,38 @@
1
+ #!/bin/sh
2
+ # Tag and push a release.
3
+
4
+ set -e
5
+
6
+ # Make sure we're in the project root.
7
+
8
+ cd $(dirname "$0")/..
9
+
10
+ # Build a new gem archive.
11
+
12
+ rm -rf gman-*.gem
13
+ gem build -q gman.gemspec
14
+
15
+ # Make sure we're on the master branch.
16
+
17
+ (git branch | grep -q '* master') || {
18
+ echo "Only release from the master branch."
19
+ exit 1
20
+ }
21
+
22
+ # Figure out what version we're releasing.
23
+
24
+ tag=v`ls gman-*.gem | sed 's/^gman-\(.*\)\.gem$/\1/'`
25
+
26
+ # Make sure we haven't released this version before.
27
+
28
+ git fetch -t origin
29
+
30
+ (git tag -l | grep -q "$tag") && {
31
+ echo "Whoops, there's already a '${tag}' tag."
32
+ exit 1
33
+ }
34
+
35
+ # Tag it and bag it.
36
+
37
+ gem push gman-*.gem && git tag "$tag" &&
38
+ git push origin master && git push origin "$tag"
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'gman'
16
+
17
+ class Test::Unit::TestCase
18
+ end
data/test/test_gman.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'helper'
2
+
3
+ VALID = ["foo.gov", "http://foo.mil", "foo@bar.gc.ca", "foo.gov.au", "http://www.foo.gouv.fr"]
4
+ INVALID = ["foo.bar.com", "bar@foo.biz", "http://www.foo.biz", "foo.uk", "gov"]
5
+
6
+ class TestGman < Test::Unit::TestCase
7
+ should "recognize government email addresses and domains" do
8
+ VALID.each do |test|
9
+ assert_equal Gman::is_government?(test), true
10
+ end
11
+ INVALID.each do |test|
12
+ assert_equal Gman::is_government?(test), false
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gman
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ben Balter
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: public_suffix
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: A ruby gem to check if the owner of a given email address is working
31
+ for THE MAN.
32
+ email: ben.balter@github.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - Gemfile
38
+ - README.md
39
+ - CONTRIBUTING.md
40
+ - LICENSE
41
+ - lib/gman.rb
42
+ - lib/domains.yml
43
+ - script/build
44
+ - script/release
45
+ - gman.gemspec
46
+ - test/helper.rb
47
+ - test/test_gman.rb
48
+ - Rakefile
49
+ - .gitignore
50
+ homepage: https://github.com/benbalter/gman
51
+ licenses:
52
+ - MIT
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 1.8.23
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Check if a given domain or email address belong to a governemnt entity
75
+ test_files: []