Class: WikiData::Fetcher

Inherits:
WikiData show all
Defined in:
lib/wikidata/fetcher.rb

Constant Summary collapse

LOOKUP_FILE =
'https://raw.githubusercontent.com/everypolitician/wikidata-fetcher/master/lookup.json'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from WikiData

ids_from_pages

Constructor Details

#initialize(h) ⇒ Fetcher

Returns a new instance of Fetcher.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wikidata/fetcher.rb', line 20

def initialize(h)
  if h[:id]
    @item = Wikisnakker::Item.find(h[:id]) or raise "No such item #{h[:id]}"
    @id = @item.id or raise "No ID for #{h[:id]} = #{@item}"
    warn "Different ID (#{@id}) for #{h[:id]}" if @id != h[:id]
  elsif h[:item]
    # Already have a Wikisnakker::Item, eg from a bulk lookup
    @item = h[:item]
    @id = @item.id or raise "No ID for #{h[:id]} = #{@item}"
  else
    raise 'No id'
  end
end

Class Method Details

.find(ids) ⇒ Object



12
13
14
# File 'lib/wikidata/fetcher.rb', line 12

def self.find(ids)
  Hash[Wikisnakker::Item.find(ids).map { |wditem| [wditem.id, new(item: wditem)] }]
end

.wikidata_propertiesObject



16
17
18
# File 'lib/wikidata/fetcher.rb', line 16

def self.wikidata_properties
  @wikidata_properties ||= JSON.parse(open(LOOKUP_FILE).read, symbolize_names: true)
end

Instance Method Details

#data(*lang) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/wikidata/fetcher.rb', line 34

def data(*lang)
  return unless item

  data = {
    id:   id,
    name: first_label_used(lang | ['en']),
  }.merge(labels).merge(wikipedia_links)

  # Short-circuit if there are no claims
  return data if item.properties.empty?

  # Short-circuit if this is not a human
  unless human?
    warn "#{id} is_instance_of #{type.join(' & ')}. Skipping"
    return nil
  end

  unknown_properties.each do |p|
    puts "⁇ Unknown property: https://www.wikidata.org/wiki/Property:#{p} for #{id}"
  end

  wanted_properties.each do |p|
    val = property_value(p)
    next warn "Unknown value for #{p} for #{id}" unless val
    data[want[p].to_sym] = val
  end

  data
end