This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
133 lines (111 loc) · 3.26 KB
/
app.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
require 'httparty'
require 'dotenv'
require 'pry'
require 'sinatra'
require 'airrecord'
include Gem::Text
Dotenv.load
Airrecord.api_key = ENV['AIRTABLE_API_KEY']
USER = ENV.fetch('USERNAME', 'admin')
PASS = ENV.fetch('PASSWORD', 'admin')
use Rack::Auth::Basic, "Restricted Area" do |user, pass|
user == USER && pass == PASS
end
class Hub < Airrecord::Table
self.base_key = ENV['AIRTABLE_APP_KEY']
self.table_name = 'Hubs'
def should_appear_on_everyaction?
return false unless self['Map?'] == true
return false unless self['Latitude'] && self['Longitude']
return false unless self['City'] && self['Name']
true
end
end
get '/' do
custom_fields = HTTParty.get(
"https://api.securevan.com/v4/customFields",
basic_auth: {
username: "sunrise-movement",
password: "#{ENV['EA_C3_KEY']}|1"
},
headers: { "Content-Type" => "application/json" }
)
hub_field = custom_fields.detect { |f| f['customFieldName'] == "Hub Affiliation" }
ea_names = hub_field['availableValues'].map{ |v| v['name'].strip }
airtable_hubs = Hub.all
def friendly_name(n)
n.to_s.strip.length > 0 ? n.sub(/^Sunrise\s/, '').strip : '(no name provided)'
end
at_names_ea = airtable_hubs.
select(&:should_appear_on_everyaction?).
map { |h| friendly_name(h['Name']) }
at_names_all = airtable_hubs.
map { |h| friendly_name(h['Name']) }
ea_set = Set.new(ea_names)
at_set = Set.new(at_names_ea)
at_all = Set.new(at_names_all)
issues = []
def closest(strings, s, suffix)
return if s == '(no name provided)'
matches = []
distances = strings.map { |s2| levenshtein_distance(s, s2) }
distances.each_with_index do |d, i|
if d <= 2 && strings[i] != '(no name provided)'
matches << strings[i]
end
end
if matches.any?
"<ul><li>Close #{suffix} matches: #{matches.map { |m| "<code>#{m}</code>" }.join(", ")}</li></ul>"
end
end
(ea_set - at_all).each do |name|
issues << "EveryAction contains <code>#{name}</code> but Airtable does not! #{closest(at_names_all, name, "AT")}"
end
(at_set - ea_set).each do |name|
issues << "Airtable contains <code>#{name}</code> (as an active hub) but EveryAction does not! #{closest(ea_names, name, "EA")}"
end
unless ea_names.size == ea_set.size
dupes = []
counts = Hash.new { |h,k| h[k] = 0 }
ea_names.each do |name|
counts[name] += 1
dupes << name if counts[name] > 1
end
dupes.each do |dupe|
issues << "<code>#{dupe}</code> is listed in EA #{counts[dupe]} times!"
end
end
if issues.length > 0
<<-HTML
<html>
<head>
<style>
code {
color: #d20600;
padding: 1px 5px;
background: #f8f8f8;
border-radius: 5px;
margin: 0 2px;
white-space: nowrap;
}
</style>
</head>
<body>
<h1>Airtable and EveryAction have some issues 😬</h1>
<ol>
#{issues.map{|i| %{<li>#{i}</li>} }.join("\n")}
</ol>
</body>
</html>
HTML
else
<<-HTML
<html>
<body>
<h1>Airtable and EveryAction are </h1>
<img src="/nsync.jpg" style="width: 100%">
</body>
</html>
HTML
end
end