Sha256: 96d7b47c4c00dfd75c7fd445ff56606e70fab5e25e50c9fb7b0832e2737e8b98

Contents?: true

Size: 677 Bytes

Versions: 3

Compression:

Stored size: 677 Bytes

Contents

class Wikidata
  def self.search(term, known_things)
    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 } }
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
voluntary-0.5.2 app/models/wikidata.rb
voluntary-0.5.1 app/models/wikidata.rb
voluntary-0.5.0 app/models/wikidata.rb