Sha256: c7af60d9311237906c19d2f2c87f77d3e8bf50eaddb770546e60e5c3c9065f5f

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

class Dataset::Adaptors::Iqvoc::AlphabeticalSearchAdaptor < Dataset::Adaptors::Iqvoc::SearchAdaptor
  def search(prefix, locale)
    fetch_results("#{locale}/alphabetical_concepts/#{prefix}.html")
    @results
  end

  def fetch_results(url, params = {})
    begin
      response = @conn.get(url, params)
      @results ||= []
      @results += extract_results(response.body)
      while more = @doc.at_css('a[rel=next]')
        fetch_results(more[:href], {})
      end
    rescue Faraday::Error::ConnectionFailed,
      Faraday::Error::ResourceNotFound => e
        Rails.logger.warn("HTTP error while querying remote source #{url}: #{e.message}")
        return nil
    end
  end

  def extract_results(html)
    @doc = Nokogiri::HTML(html)

    @doc.css('.concept-item').map do |element|
      link = element.at_css('.concept-item-link')
      label, path = link.text, link['data-resource-path'] # href

      options = {
        definition: element.at_css('.concept-item-definition').try(:content),
        definition_language: element.at_css('.concept-item-definition').try(:[], :lang),
        additional_info: element.at_css('.additional_info').try(:content)
      }

      result = AlphabeticalSearchResultRemote.new(url, path, label, options)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
iqvoc-4.13.2 app/models/dataset/adaptors/iqvoc/alphabetical_search_adaptor.rb
iqvoc-4.13.0 app/models/dataset/adaptors/iqvoc/alphabetical_search_adaptor.rb
iqvoc-4.12.1 app/models/dataset/adaptors/iqvoc/alphabetical_search_adaptor.rb
iqvoc-4.12.0 app/models/dataset/adaptors/iqvoc/alphabetical_search_adaptor.rb
iqvoc-4.11.1 app/models/dataset/adaptors/iqvoc/alphabetical_search_adaptor.rb
iqvoc-4.11.0 app/models/dataset/adaptors/iqvoc/alphabetical_search_adaptor.rb