Skip to content

Commit

Permalink
add lookup result
Browse files Browse the repository at this point in the history
  • Loading branch information
mnyrop committed Nov 13, 2024
1 parent 3c95a5f commit 74c155e
Show file tree
Hide file tree
Showing 3 changed files with 777 additions and 11 deletions.
34 changes: 24 additions & 10 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,43 @@ OG_IDS = [
'OG-2021-USCIS'
]
INDEX_PATH = './build/index.json'
LOOKUP_PATH = './build/anum_lookup.csv'
HTML_PATH = './build/index.html'
TEMPLATE_PATH = './lib/template.html'

FETCHED_OG_INDEXES = OG_IDS.map { |id| { id => JSON.parse(HTTParty.get("https://migrants-and-the-state.github.io/#{id.downcase}/index.json").body) } }.inject(:merge)

def formatted_datetime(time = Time.now)
fmt_date = "#{time.month}/#{time.day}/#{time.year}"
fmt_time = "#{time.hour}:#{time.min.to_s.rjust(2, '0')}#{time.zone}"
"#{fmt_date} at #{fmt_time}"
end

namespace :build do
desc 'build aggregated list of anums and order_group ids'
task :anums_lookup do
lookup = []
FETCHED_OG_INDEXES.each { |og_id,index| index.each { |afile| lookup << [afile['id'],og_id]} }
puts "Writing anum lookup table to #{LOOKUP_PATH}"
File.open(LOOKUP_PATH, 'w') do |file|
file.puts "anumber,og_id"
lookup.each { |pair| file.puts pair.join(",")}
end
puts "\nDone ✓\n"
end

desc 'build aggregated index'
task :index do
index = []

OG_IDS.each do |og_id|
url = "https://migrants-and-the-state.github.io/#{og_id.downcase}/index.json"
hashes = JSON.parse(HTTParty.get(url).body)
hashes.map! { |h| h['order_group'] = og_id; h }
puts "Adding #{hashes.length} items from #{og_id} to the index"
hashes.each { |hash| index << hash }
aggregate_index = []

FETCHED_OG_INDEXES.each do |og_id, index|
index.map! { |hash| hash['order_group'] = og_id; hash }
puts "Adding #{index.length} items from #{og_id} to the aggregate index"
index.each { |hash| aggregate_index << hash }
end

puts "Writing aggregated index with #{index.length} items to #{INDEX_PATH}"
File.open(INDEX_PATH, 'w') { |file| file.write JSON.pretty_generate(index) }
puts "Writing aggregated index with #{aggregate_index.length} items to #{INDEX_PATH}"
File.open(INDEX_PATH, 'w') { |file| file.write JSON.pretty_generate(aggregate_index) }
puts "\nDone ✓\n"
end

Expand All @@ -53,6 +66,7 @@ namespace :build do

desc 'build everything'
task :all do
Rake::Task['build:anums_lookup'].execute
Rake::Task['build:index'].execute
Rake::Task['build:html'].execute
end
Expand Down
Loading

0 comments on commit 74c155e

Please sign in to comment.