-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1138 from griffithlab/port-clinical-trial-scraper
Auto update clinical trial names and descriptions
- Loading branch information
Showing
4 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |