Sha256: cbc7b5bfe19389c51eff844471e180e4a53596be4118cc0a6d10a782e9a1f64b

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

require 'linkeddata'
require 'timeout'

class Dataset::IqvocDataset
  DEFAULT_TIMEOUT = 2.freeze

  attr_reader :name, :url

  def initialize(url)
    @url = URI.parse(url)
    dataset_url = URI.join(@url.to_s + '/', 'dataset.rdf')

    begin
      @repository = Timeout::timeout(DEFAULT_TIMEOUT) do
        RDF::Repository.load(dataset_url)
      end
    rescue Errno::ECONNREFUSED, Timeout::Error => e
      Rails.logger.error("Iqvoc source couldn't be resolved: #{@url}, message: #{e.message}")
    ensure
      @name = fetch_name
    end
  end

  def to_s
    "#{name} (#{url})"
  end

  def search(params)
    Dataset::Adaptors::Iqvoc::SearchAdaptor.new(url).search(params)
  end

  def alphabetical_search(prefix, locale)
    Dataset::Adaptors::Iqvoc::AlphabeticalSearchAdaptor.new(url).search(prefix, locale)
  end

  def find_label(concept_url)
    Dataset::Adaptors::Iqvoc::LabelAdaptor.new(url).find(concept_url)
  end

  private
  def fetch_name
    return @url.to_s if @repository.nil?

    void = RDF::Vocabulary.new('http://rdfs.org/ns/void#')
    query = RDF::Query.new({ dataset: { RDF.type => void.Dataset, RDF::DC.title => :title } })
    results = Timeout::timeout(DEFAULT_TIMEOUT) do
      query.execute(@repository)
    end

    return @url.to_s if results.nil? || results.empty?
    results.map { |solution| solution.title.to_s }.first
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
iqvoc-4.12.1 app/models/dataset/iqvoc_dataset.rb
iqvoc-4.12.0 app/models/dataset/iqvoc_dataset.rb