@@ -10,37 +10,45 @@ require 'yaml'
10
10
require 'typhoeus'
11
11
12
12
def org_exists? ( org )
13
- Typhoeus . head ( "https://github.com/#{ org } " , accept_encoding : 'gzip' ) . success?
13
+ Typhoeus . head ( "https://github.com/orgs/ #{ org } /people " , accept_encoding : 'gzip' ) . success?
14
14
rescue TyphoeusError
15
15
false
16
16
end
17
17
18
- org_file = YAML . load_file ( './_data/governments.yml' )
18
+ def org_file_path
19
+ File . expand_path '../_data/governments.yml' , File . dirname ( __FILE__ )
20
+ end
21
+
22
+ def org_file
23
+ @org_file ||= YAML . load_file org_file_path
24
+ end
25
+
19
26
gov = org_file . values . flatten
20
27
us_gov = org_file [ 'U.S. Federal' ] . map ( &:downcase )
21
28
research = YAML . load_file ( './_data/research.yml' ) . values . flatten
22
29
existing = ( gov + research ) . flatten . map ( &:downcase )
23
30
24
- orgs = JSON . parse open ( 'https://usdigitalregistry.digitalgov.gov/api/v1/social_media.json?services=github' ) . read
31
+ orgs = JSON . parse URI . open ( 'https://usdigitalregistry.digitalgov.gov/api/v1/social_media.json?services=github' ) . read
25
32
orgs = orgs [ 'results' ] . collect { |data | data [ 'account' ] }
33
+ orgs . delete ( '' )
26
34
27
35
orgs . each do |org |
28
- next if existing . include? ( org . downcase )
29
- next unless org_exists? org
30
-
31
- org_file [ 'U.S. Federal' ] . push ( org . downcase )
36
+ next if existing . any? { |s | s . casecmp ( org ) . zero? }
37
+ if org_exists? org
38
+ org_file [ 'U.S. Federal' ] . push ( org )
39
+ else
40
+ puts "\n \n In the registry, but not valid '#{ org } '"
41
+ end
32
42
end
33
43
34
- output = {
35
- 'U.S. Federal' => org_file [ 'U.S. Federal' ] . uniq . sort_by ( &:upcase )
36
- }
37
- puts 'To be pasted into _data/governments.yml'
38
- puts output . to_yaml
39
-
40
- orgs . map! ( &:downcase )
44
+ org_file [ 'U.S. Federal' ] = org_file [ 'U.S. Federal' ] . uniq . sort_by ( &:upcase )
41
45
42
46
puts "\n \n Missing from registry:"
43
- puts ( ( us_gov - orgs ) . to_yaml )
47
+ puts ( ( us_gov - orgs . map ( &:downcase ) ) . to_yaml )
48
+
49
+ comment = File . open ( org_file_path ) . read . lines . first
50
+ output = comment
51
+ output << "\n "
52
+ output << org_file . to_yaml . sub ( /\A ---\n / , '' ) . gsub ( /^-/ , ' -' ) . gsub ( /\n ([^ ])/ , "\n \n \\ 1" )
44
53
45
- puts "\n \n In the registry, but not valid"
46
- puts ( ( orgs - existing ) . to_yaml )
54
+ File . write ( org_file_path , output )
0 commit comments