Skip to content

Commit

Permalink
Merge pull request #1138 from griffithlab/port-clinical-trial-scraper
Browse files Browse the repository at this point in the history
Auto update clinical trial names and descriptions
  • Loading branch information
acoffman authored Oct 25, 2024
2 parents c837092 + b3a4ad0 commit 67708ba
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
5 changes: 5 additions & 0 deletions server/app/jobs/populate_clinical_trial_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class PopulateClinicalTrialRecord < ApplicationJob
def perform(clinical_trial)
Scrapers::ClinicalTrial.populate_fields(clinical_trial)
end
end
27 changes: 27 additions & 0 deletions server/app/lib/scrapers/clinical_trial.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Scrapers
module ClinicalTrial
def self.populate_fields(clinical_trial)
resp = call_clinical_trials_api(clinical_trial.nct_id)
clinical_trial.description = resp.description
clinical_trial.name = resp.name
clinical_trial.save
end

def self.call_clinical_trials_api(nct_id)
http_resp = Util.make_get_request(url_for_nct_id(nct_id))
ClinicalTrialResponse.new(http_resp)
end

def self.url_for_nct_id(nct_id)
"https://clinicaltrials.gov/api/v2/studies/#{nct_id}?format=json&fields=#{fields}"
end

def self.fields
[
"protocolSection.identificationModule.nctId",
"protocolSection.identificationModule.briefTitle",
"protocolSection.descriptionModule.briefSummary"
].join(",")
end
end
end
24 changes: 24 additions & 0 deletions server/app/lib/scrapers/clinical_trial_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Scrapers
class ClinicalTrialResponse
attr_reader :json
def initialize(response_body)
@json = JSON.parse(response_body)
end

def nct_id
json.dig("protocolSection", "identificationModule", "nctId")
end

def name
json.dig("protocolSection", "identificationModule", "briefTitle")
end

def description
if desc = json.dig("protocolSection", "descriptionModule", "briefSummary")
desc.squish
else
nil
end
end
end
end
12 changes: 11 additions & 1 deletion server/app/models/clinical_trial.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
class ClinicalTrial < ActiveRecord::Base
has_and_belongs_to_many :sources

after_create :populate_additional_fields_if_needed

def self.url_for(nct_id:)
if nct_id.blank?
nil
else
"https://clinicaltrials.gov/ct2/show/#{nct_id}"
"https://clinicaltrials.gov/study/#{nct_id}"
end
end

def link
Rails.application.routes.url_helpers.url_for("/clinical-trials/#{self.id}")
end


private
def populate_additional_fields_if_needed
if self.name.blank? || self.description.blank?
PopulateClinicalTrialRecord.perform_later(self)
end
end
end

0 comments on commit 67708ba

Please sign in to comment.