Skip to content

Commit 5072727

Browse files
committed
Google sheet ingestor for events
1 parent 5dc63a2 commit 5072727

4 files changed

Lines changed: 34 additions & 14 deletions

File tree

lib/ingestors/event_csv_ingestor.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module Ingestors
55
class EventCsvIngestor < Ingestor
6-
include CsvIngestion
6+
include SpreadsheetIngestion
77

88
def self.config
99
{
@@ -16,6 +16,9 @@ def self.config
1616
def read(url)
1717
# parse csv file to table
1818
begin
19+
# Google spreadsheet convertor
20+
url = gsheet_to_csv(url)
21+
1922
# parse csv as table
2023
web_contents = open_url(url).read
2124
table = CSV.parse(web_contents, headers: true)

lib/ingestors/material_csv_ingestor.rb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
module Ingestors
66
class MaterialCsvIngestor < Ingestor
7-
include CsvIngestion
7+
include SpreadsheetIngestion
88

99
def self.config
1010
{
@@ -68,16 +68,5 @@ def read(url)
6868
def process_competency(row, header)
6969
row[header].nil? ? 'notspecified' : row[header]
7070
end
71-
72-
# if url is a raw google spreadsheet
73-
# it returns the Google spreadsheet CSV export
74-
# else it returns the url
75-
def gsheet_to_csv(url)
76-
return url unless url.include? 'docs.google.com/spreadsheets/d/'
77-
78-
spreadsheet_id = url.partition('d/').last.partition('/').first
79-
gid = CGI.parse(URI.parse(url).query)['gid']&.first
80-
"https://docs.google.com/spreadsheets/d/#{spreadsheet_id}/export?gid=#{gid}&exportFormat=csv"
81-
end
8271
end
8372
end
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Ingestors
2-
module CsvIngestion
2+
module SpreadsheetIngestion
33
def process_url(row, header)
44
row[header].to_s.lstrip unless row[header].nil?
55
end
@@ -21,5 +21,16 @@ def process_array(row, header)
2121
def get_column(row, header)
2222
row[header].to_s.lstrip unless row[header].nil?
2323
end
24+
25+
# if url is a raw google spreadsheet
26+
# it returns the Google spreadsheet CSV export
27+
# else it returns the url
28+
def gsheet_to_csv(url)
29+
return url unless url.include? 'docs.google.com/spreadsheets/d/'
30+
31+
spreadsheet_id = url.partition('d/').last.partition('/').first
32+
gid = CGI.parse(URI.parse(url).query)['gid']&.first
33+
"https://docs.google.com/spreadsheets/d/#{spreadsheet_id}/export?gid=#{gid}&exportFormat=csv"
34+
end
2435
end
2536
end

test/unit/ingestors/event_csv_ingestor_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,23 @@ def run
9595
event.tech_requirements
9696
end
9797

98+
test 'should change url to google sheet export if needed' do
99+
ingestor = Ingestors::EventCsvIngestor.new
100+
101+
url_random = 'https://this-might-not-be-reachable-at-all.com'
102+
url_not_spreadsheet = 'https://google.com'
103+
url_spreadsheet = 'https://docs.google.com/spreadsheets/d/abcde/edit?gid=12345#gid=12345'
104+
url_export_spreadsheet = 'https://docs.google.com/spreadsheets/d/abcde/export?gid=12345&exportFormat=csv'
105+
106+
# doesn't change url, because not google spreadsheet
107+
assert_equal ingestor.send(:gsheet_to_csv, url_random), url_random
108+
assert_equal ingestor.send(:gsheet_to_csv, url_not_spreadsheet), url_not_spreadsheet
109+
110+
# do change url, because google spreadsheet
111+
assert_equal ingestor.send(:gsheet_to_csv, url_spreadsheet), url_export_spreadsheet
112+
assert_not_equal url_spreadsheet, url_export_spreadsheet
113+
end
114+
98115
private
99116

100117
def check_array(collection, values, exclusions = [])

0 commit comments

Comments
 (0)