Sha256: 272f8ee0c9e8f16718d37946daa51067522b915168120cccdaaefaa3d6715269

Contents?: true

Size: 764 Bytes

Versions: 3

Compression:

Stored size: 764 Bytes

Contents

class Wikidata
  def self.search(term, known_things)
    begin
      JSON.parse(
        HTTParty.get(
          "https://www.wikidata.org/w/api.php?action=wbsearchentities&search=" +
          "#{URI.encode(term, /\W/)}&format=json&language=en&type=item&continue=0",
          verify: false
        ).body
      )['search'].map do |item|
        description = if item['description'].to_s.present? && 
                         item['description'] != 'Wikimedia disambiguation page'
          " (#{item['description']})"
        else
          ''
        end
        
        "#{item['label']}#{description}"
      end.uniq.select{|i| known_things.select{|t| t[:name] == i }.none? }.map{|item| { name: item } }
    rescue SocketError => e
      []
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
voluntary-0.7.1 app/models/wikidata.rb
voluntary-0.7.0 app/models/wikidata.rb
voluntary-0.6.0 app/models/wikidata.rb