Sha256: 60a34d06e13b59a248392e7e2a7f97d99720dd559937157746198dcec55d29cd
Contents?: true
Size: 1.73 KB
Versions: 5
Compression:
Stored size: 1.73 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 => 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
5 entries across 5 versions & 1 rubygems