mirror of
https://github.com/chenasraf/snpr.git
synced 2026-05-18 01:39:01 +00:00
* Passwords now need to be 8 characters long (new default in authlogic). * To login to the Sidekiq web interface, log in to opensnp.org, if you have the `admin` flag set, you will find it at the usual place. * I finally found out why Rubocop didn't complain about the frozen string literal pragma missing and added all of them. * Rubocop complained about keyword arguments, which is only relevant for Rails 5, so I disabled the cop. * I removed the `inherit_from: .rubocop_todo.yml` from `.rubocop.yml`, so Hound does complain about **all** the issues. I also added a few tasks for running Rubocop.
86 lines
2.4 KiB
Ruby
86 lines
2.4 KiB
Ruby
# frozen_string_literal: true
|
|
# See http://api.plos.org/solr/faq/#solr_api_recommended_usage for API limits
|
|
require 'plos'
|
|
|
|
class PlosSearch
|
|
include Sidekiq::Worker
|
|
sidekiq_options queue: :plos, unique: true
|
|
attr_reader :snp, :client
|
|
|
|
def perform(snp_id)
|
|
@snp = Snp.where(id: snp_id).first
|
|
return false if @snp.nil? || snp_illegal? || recently_updated?
|
|
@client = PLOS::Client.new(self.class.api_key)
|
|
articles = perform_search
|
|
# In many weird cases on production, articles is nil
|
|
# (can't reproduce on development)
|
|
# For now the workaround is to use Array.wrap to make empty list
|
|
# i.e, nil => []
|
|
# - Philipp
|
|
Array.wrap(articles).each do |article|
|
|
import_article(article) if not article.nil?
|
|
end
|
|
snp.plos_updated!
|
|
logger.info('sleeping for 6 seconds in honor of the API limits')
|
|
sleep(6)
|
|
end
|
|
|
|
def import_article(article)
|
|
plos_paper_attributes = {
|
|
first_author: article.try(:authors).try(:first).try(:to_s),
|
|
doi: article.id,
|
|
pub_date: article.published_at,
|
|
title: article.title,
|
|
}
|
|
plos_paper = PlosPaper.find_or_initialize_by(doi: plos_paper_attributes[:doi])
|
|
plos_paper.update_attributes!(plos_paper_attributes)
|
|
plos_paper.snps << snp unless plos_paper.snps.include? snp
|
|
Sidekiq::Client.enqueue(PlosDetails, plos_paper.id)
|
|
end
|
|
|
|
def perform_search
|
|
# honoring API limits
|
|
result = nil
|
|
begin
|
|
Timeout.timeout(5) do
|
|
result = client.search(snp.name, 0, 999)
|
|
end
|
|
rescue Timeout::Error
|
|
logger.error("API call timed out")
|
|
false
|
|
end
|
|
logger.info('Successfully called the API')
|
|
result
|
|
# the following rescue never seems to fire? can't find the error in the logs - Philipp
|
|
rescue => e
|
|
logger.error("API call unsuccessful: Error was: #{e.class}: #{e.message}")
|
|
raise e
|
|
end
|
|
|
|
def snp_illegal?
|
|
# we don't need mitochondrial or VG-SNPs as these just result in noise
|
|
# from the PLOS API
|
|
forbidden_names = ["mt-", "vg"]
|
|
if forbidden_names.any? { |part| snp.name[part] }
|
|
logger.info("Snp #{snp.name} is a mitochondrial or vg snp")
|
|
true
|
|
else
|
|
false
|
|
end
|
|
end
|
|
|
|
def recently_updated?
|
|
# we don't need to update snps that have been updated in the last month
|
|
if snp.plos_updated > 31.days.ago
|
|
logger.info("time threshold for #{snp.name} not met")
|
|
true
|
|
else
|
|
false
|
|
end
|
|
end
|
|
|
|
def self.api_key
|
|
ENV.fetch('PLOS_API_KEY')
|
|
end
|
|
end
|