class Wikisnakker::Lookup
Public Class Methods
find(ids)
click to toggle source
# 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
new(*ids)
click to toggle source
# 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
Public Instance Methods
[](key)
click to toggle source
# File lib/wikisnakker/lookup.rb, line 31 def [](key) @entities[key] end
populate_with(properties)
click to toggle source
# 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
properties()
click to toggle source
# File lib/wikisnakker/lookup.rb, line 23 def properties @used_props.to_a.map { |e| "Q#{e}".to_sym } end
values()
click to toggle source
# File lib/wikisnakker/lookup.rb, line 27 def values @values ||= @entities.values end
Private Instance Methods
each_wikibase_entitiyid(obj) { |result| ... }
click to toggle source
# File lib/wikisnakker/lookup.rb, line 66 def each_wikibase_entitiyid(obj) recurse_proc(obj) do |result| next unless result.is_a?(Hash) && result[:type] == 'wikibase-entityid'.freeze yield(result) end end
get(*ids)
click to toggle source
# File lib/wikisnakker/lookup.rb, line 43 def get(*ids) query = { action: 'wbgetentities', ids: ids.flatten.join('|'), format: 'json' } url = 'https://www.wikidata.org/w/api.php?' + URI.encode_www_form(query) json = Yajl::Parser.parse(open(url), symbolize_keys: true) save_wikibase_entityids(json) json end
recurse_proc(result, &proc)
click to toggle source
Recursively calls passed Proc if the parsed data structure is an Array or Hash Taken from the json gem. @see git.io/v4Tf7
# File lib/wikisnakker/lookup.rb, line 76 def recurse_proc(result, &proc) case result when Array result.each { |x| recurse_proc x, &proc } proc.call result when Hash result.each do |x, y| recurse_proc x, &proc recurse_proc y, &proc end proc.call result else proc.call result end end
save_wikibase_entityids(json)
click to toggle source
If a property is set to another Wikidata article, resolve that (e.g. set 'gender' to 'male' rather than 'Q6581097') We don't know yet what that will resolve to, and we don't want to look them up one by one, so keep track of any entity ids we encounter and then resolve them later in '#populate_with'.
# File lib/wikisnakker/lookup.rb, line 60 def save_wikibase_entityids(json) each_wikibase_entitiyid(json) do |entityid| @used_props << entityid[:value][:'numeric-id'] end end