diff --git a/app/controllers/concerns/facetable.rb b/app/controllers/concerns/facetable.rb index 52b986f0b..f73c6decf 100644 --- a/app/controllers/concerns/facetable.rb +++ b/app/controllers/concerns/facetable.rb @@ -99,7 +99,7 @@ def facet_by_affiliation(arr) # generate hash with id and name for each provider in facet ids = arr.map { |hsh| "\"#{hsh["key"]}\"" }.join(" ") - affiliations = Organization.query(ids, size: 1000)[:data] + affiliations = Organization.query(ids, size: 1000)[:data] || [] arr.map do |hsh| { "id" => hsh["key"], diff --git a/app/models/organization.rb b/app/models/organization.rb index 78754df20..08042268c 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -27,8 +27,8 @@ def self.query(query, options={}) response = Maremma.get(url, host: true) - return [] if response.status != 200 - + return {} if response.status != 200 + data = Array.wrap(response.body.dig("data", "items")).map do |message| parse_message(id: message["id"], message: message) end diff --git a/spec/models/organization_spec.rb b/spec/models/organization_spec.rb index 1912a1a96..1297e64ab 100644 --- a/spec/models/organization_spec.rb +++ b/spec/models/organization_spec.rb @@ -51,5 +51,15 @@ organizations = Organization.query(query) expect(organizations[:data]).to be_empty end + + it "status code not 200" do + url = "https://api.ror.org/organizations?query=lincoln%20university" + stub = stub_request(:get, url).and_return(status: [408]) + + query = "lincoln university" + organizations = Organization.query(query) + expect(organizations).to be_empty + expect(stub).to have_been_requested + end end end \ No newline at end of file