Sha256: bf9e8e68bd98caf92802acaa161a51d01a9758be4e3b84a0ddd517836afeec24

Contents?: true

Size: 1.77 KB

Versions: 10

Compression:

Stored size: 1.77 KB

Contents

class Dataset::Adaptors::Iqvoc::SearchAdaptor < Dataset::Adaptors::Iqvoc::HTTPAdaptor
  def search(raw_params = {})
    languages = raw_params.fetch(:languages, I18n.locale)
    languages = Array.wrap(languages).flatten.join(',')

    params = {
      q: raw_params[:q],
      t: raw_params[:t],
      l: languages,
      c: raw_params[:c],
      qt: raw_params[:qt],
      page: 1 # hard code the first page as we need to follow pagination links
    }

    fetch_results('/search.html', params)
    @results
  end

  def fetch_results(path, params = {})
    begin
      response = @conn.get(path, 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,
      Faraday::Error::TimeoutError => e
        Rails.logger.warn("HTTP error while querying remote source #{path}: #{e.message}")
        return nil
    end
  end

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

    @doc.css('.search-result').map do |element|
      link = element.at_css('.search-result-link')
      label, path = link.text, link['data-resource-path']
      result = SearchResultRemote.new(url, path, label)

      if (meta = element.css('.search-result-meta > .search-result-value')) && meta.any?
        meta.each do |element|
          result.add_meta_information(element['data-key'], element.text)
        end
      end

      if body = element.at_css('.search-result-body')
        result.body = body.text
      end

      result.rdf_namespace = element['data-rdf-namespace']
      result.rdf_predicate = element['data-rdf-predicate']
      result.language = element['data-rdf-language']

      result
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
iqvoc-4.8.2 app/models/dataset/adaptors/iqvoc/search_adaptor.rb
iqvoc-4.8.1 app/models/dataset/adaptors/iqvoc/search_adaptor.rb
iqvoc-4.8.0 app/models/dataset/adaptors/iqvoc/search_adaptor.rb
iqvoc-4.7.0 app/models/dataset/adaptors/iqvoc/search_adaptor.rb
iqvoc-4.6.1 app/models/dataset/adaptors/iqvoc/search_adaptor.rb
iqvoc-4.5.2 app/models/dataset/adaptors/iqvoc/search_adaptor.rb
iqvoc-4.6.0 app/models/dataset/adaptors/iqvoc/search_adaptor.rb
iqvoc-4.5.1 app/models/dataset/adaptors/iqvoc/search_adaptor.rb
iqvoc-4.5.0 app/models/dataset/adaptors/iqvoc/search_adaptor.rb
iqvoc-4.4.0 app/models/dataset/adaptors/iqvoc/search_adaptor.rb