Class: Wikisnakker::Lookup

Inherits:
Object
  • Object
show all
Defined in:
lib/wikisnakker/lookup.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*ids) ⇒ Lookup

Returns a new instance of Lookup.



14
15
16
17
18
19
20
21
# File 'lib/wikisnakker/lookup.rb', line 14

def initialize(*ids)
  ids = ids.flatten.compact.uniq
  @used_props = Set.new
  entities = ids.each_slice(50).map do |id_slice|
    get(id_slice)[:entities]
  end
  @entities = entities.reduce(&:merge) || {}
end

Class Method Details

.find(ids) ⇒ Object



7
8
9
10
11
12
# File 'lib/wikisnakker/lookup.rb', line 7

def self.find(ids)
  lookup = new(ids)
  property_lookup = new(lookup.properties)
  lookup.populate_with(property_lookup)
  lookup
end

Instance Method Details

#[](key) ⇒ Object



31
32
33
# File 'lib/wikisnakker/lookup.rb', line 31

def [](key)
  @entities[key]
end

#populate_with(properties) ⇒ Object



35
36
37
38
39
# File 'lib/wikisnakker/lookup.rb', line 35

def populate_with(properties)
  each_wikibase_entitiyid(@entities) do |entityid|
    entityid[:value] = properties["Q#{entityid[:value][:"numeric-id"]}".to_sym]
  end
end

#propertiesObject



23
24
25
# File 'lib/wikisnakker/lookup.rb', line 23

def properties
  @used_props.to_a.map { |e| "Q#{e}".to_sym }
end

#valuesObject



27
28
29
# File 'lib/wikisnakker/lookup.rb', line 27

def values
  @values ||= @entities.values
end