Class: Wikidata

Inherits:
Object
  • Object
show all
Defined in:
app/models/wikidata.rb

Class Method Summary collapse

Class Method Details

.search(term, known_things) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/models/wikidata.rb', line 2

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