Skip to content

Commit ebed5ef

Browse files
authored
Merge pull request #427 from b123400/fix-425
Get FHIR url properly
2 parents b8d1ac0 + 745dd16 commit ebed5ef

File tree

1 file changed

+7
-31
lines changed

1 file changed

+7
-31
lines changed

src/plugin/bsv_fhir.rb

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
require "drb"
1313
require "fileutils"
1414
require "json"
15+
require "mechanize"
1516
require "net/http"
1617
require "uri"
1718
require "plugin/bsv_xml"
@@ -77,7 +78,7 @@ def initialize(*args)
7778
def update
7879
LogFile.append("oddb/debug", " bsv_fhir: getting BsvFhirPlugin.update", Time.now)
7980

80-
target_url = find_latest_fhir_export_url
81+
target_url = latest_fhir_export_url
8182
unless target_url
8283
LogFile.append("oddb/debug", " bsv_fhir: no FHIR export URL found, aborting", Time.now)
8384
return nil
@@ -141,36 +142,11 @@ def update_preparations_from_fhir(io, opts = {})
141142

142143
private
143144

144-
def fhir_export_url_for_date(date)
145-
date_str = date.strftime("%Y%m%d")
146-
"#{FHIR_BASE_URL}foph-sl-export-#{date_str}.ndjson"
147-
end
148-
149-
# BAG publishes NDJSON exports on irregular dates (not daily).
150-
# Try today first, then go back day-by-day up to 30 days to find
151-
# the latest available export.
152-
def find_latest_fhir_export_url
153-
today = @@today.respond_to?(:to_date) ? @@today.to_date : Date.today
154-
30.times do |offset|
155-
candidate_date = today - offset
156-
url = fhir_export_url_for_date(candidate_date)
157-
uri = URI.parse(url)
158-
http = Net::HTTP.new(uri.host, uri.port)
159-
http.use_ssl = (uri.scheme == "https")
160-
http.open_timeout = 10
161-
http.read_timeout = 10
162-
begin
163-
response = http.request_head(uri.request_uri)
164-
if response.is_a?(Net::HTTPSuccess)
165-
LogFile.append("oddb/debug", " bsv_fhir: found export at #{url}", Time.now)
166-
return url
167-
end
168-
rescue Net::OpenTimeout, Net::ReadTimeout, Errno::ECONNRESET => e
169-
LogFile.append("oddb/debug", " bsv_fhir: HEAD #{url} failed: #{e.message}", Time.now)
170-
end
171-
end
172-
LogFile.append("oddb/debug", " bsv_fhir: no FHIR export found in last 30 days", Time.now)
173-
nil
145+
def latest_fhir_export_url
146+
agent = Mechanize.new
147+
response = agent.get "https://epl.bag.admin.ch/api/sl/public/resources/current"
148+
resources = JSON.parse(response.body)
149+
"https://epl.bag.admin.ch/static/" + resources["fhir"]["fileUrl"]
174150
end
175151

176152
def download_ndjson(target_url, save_dir, file_name)

0 commit comments

Comments
 (0)