Skip to content

Commit

Permalink
Merge pull request #831 from pulibrary/805-ignore-empty-rows
Browse files Browse the repository at this point in the history
805 ignore empty rows
  • Loading branch information
christinach authored Aug 13, 2024
2 parents f490935 + 57f877f commit 70ed760
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
7 changes: 6 additions & 1 deletion app/models/air_table_staff/record_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ def base_url
def to_a(offset: nil)
@as_array ||= begin
json = get_json(offset:)
records = json[:records].map do |row|
json_records = json[:records].select do |record|
record_present = record[:fields].present? && (record[:fields][:fldvENk2uiLDHmYSw] || record[:fields][:fldnKprqGraSvNTJK])
Rails.logger.error("This record #{record[:fields]} is missing first and/or last name. It will not be included.") unless record_present
record_present
end
records = json_records.map do |row|
AirTableStaff::StaffDirectoryPerson.new(row[:fields])
end
offset = json[:offset]
Expand Down
42 changes: 42 additions & 0 deletions spec/fixtures/files/air_table/records_no_offset_empty_records.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"records": [
{
"id": "recrhIUhcJw3Owio92",
"createdTime": "2024-01-30T22:16:16.000Z",
"fields": {
}
},
{
"id": "recrhIUhcJwsoiw21",
"createdTime": "2024-01-30T22:16:16.000Z",
"fields": {
"fldbnDHHhDNlc2Lx8": "[email protected]",
"fld4JloN0LxiFaTiw": "Hello\nMy research interests\nare\n\nfantastic!",
"fldULoOUDSpoEpdAP": "https://example.com",
"fldXw9janMHvhBWvO": ["Industrial Relations", "James Madison Program"]
}
},
{
"id": "recrhIUhcJw3dW022",
"createdTime": "2024-01-30T22:16:16.000Z",
"fields": {
"fldKZxmtofNbXW4qS": "123 Stokes",
"fldCCTbVNKKBFXxrp": ["Virtual Reality"],
"fldxpCzkJmhEkVqZt": "Special and Distinctive Collections",
"fld9NYFQePrPxbJJW": "Special Collections",
"fldz6yBenvTjdClXZ": "Stokes",
"fldvENk2uiLDHmYSw": "Librarian",
"fldnKprqGraSvNTJK": "Phillip",
"fldw0mjDdB48HstnB": "Library Collections Specialist V",
"fldbquJ6Hn2eq1V2h": "123",
"fldgarsg3FzD8xpE4": "ab123",
"fldqulY6ehd5aIbR1": "(123) 123-1234",
"fldL7tm4jVvYksIwl": "Phillip Librarian",
"fldbnDHHhDNlc2Lx8": "[email protected]",
"fld4JloN0LxiFaTiw": "Hello\nMy research interests\nare\n\nfantastic!",
"fldULoOUDSpoEpdAP": "https://example.com",
"fldXw9janMHvhBWvO": ["Industrial Relations", "James Madison Program"]
}
}
]
}
10 changes: 10 additions & 0 deletions spec/models/air_table_staff/record_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,14 @@
end
end
end
context 'when the airtable contains empty records' do
before do
stub_airtable(empty: true)
end
it 'does not include empty records' do
list = described_class.new.to_a

expect(list.length).to eq(1)
end
end
end
13 changes: 12 additions & 1 deletion spec/support/stub_airtable.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# frozen_string_literal: true
module AirtableStubbing
BASE_AIRTABLE_URL = "https://api.airtable.com/v0/appv7XA5FWS7DG9oe/tblM0iymGN5oqDUVm?fields%5B%5D=fld0MfgMlZd364YTR&fields%5B%5D=fld4JloN0LxiFaTiw&fields%5B%5D=fld9NYFQePrPxbJJW&fields%5B%5D=fldCCTbVNKKBFXxrp&fields%5B%5D=fldGzh0SHZqlFk3aU&fields%5B%5D=fldKZxmtofNbXW4qS&fields%5B%5D=fldnKprqGraSvNTJK&fields%5B%5D=fldL7tm4jVvYksIwl&fields%5B%5D=fldULoOUDSpoEpdAP&fields%5B%5D=fldXw9janMHvhBWvO&fields%5B%5D=fldbnDHHhDNlc2Lx8&fields%5B%5D=fldbquJ6Hn2eq1V2h&fields%5B%5D=fldvENk2uiLDHmYSw&fields%5B%5D=fldgarsg3FzD8xpE4&fields%5B%5D=fldqulY6ehd5aIbR1&fields%5B%5D=fldusiuPpfSql6vSk&fields%5B%5D=fldw0mjDdB48HstnB&fields%5B%5D=fldxpCzkJmhEkVqZt&fields%5B%5D=fldypTXdkQGpYgVDC&fields%5B%5D=fldz6yBenvTjdClXZ&returnFieldsByFieldId=true"
def stub_airtable(offset: false)
def stub_airtable(offset: false, empty: false)
if offset
stub_airtable_with_offset
elsif empty
stub_airtable_without_offset_empty_records
else
stub_airtable_without_offset
end
Expand Down Expand Up @@ -33,6 +35,15 @@ def stub_airtable_without_offset
})
.to_return(status: 200, body: File.read(airtable_path))
end

def stub_airtable_without_offset_empty_records
airtable_path = Pathname.new(file_fixture_path).join("air_table", 'records_no_offset_empty_records.json')
stub_request(:get, BASE_AIRTABLE_URL)
.with(headers: {
'Authorization' => 'Bearer FAKE_AIRTABLE_TOKEN'
})
.to_return(status: 200, body: File.read(airtable_path))
end
end

RSpec.configure do |config|
Expand Down

0 comments on commit 70ed760

Please sign in to comment.