module GimmeWikidata

Constants

VERSION

Public Class Methods

fetch(ids, props: [Props::ALIASES, Props::LABELS, Props::DESCRIPTIONS, Props::CLAIMS]) click to toggle source

Fetch Entity (or Entities) from Wikidata by id (or ids)

Calls either fetch_entity or fetch_entities, so is just for convenience.

  • Parameters :

    • ids -> Either a single Wikidata id or an array of them. If the former, will call fetch_entity; if the latter, will call fetch_entities

    • props -> The properties to pull from Wikidata (see Props class). Defaults to aliases, labels, descriptions and claims.

  • Returns :

  • Raises :

    • ArgumentError if ids are not valid Wikidata ids

# File lib/gimme_wikidata.rb, line 52
def fetch(ids, props: [Props::ALIASES, Props::LABELS, Props::DESCRIPTIONS, Props::CLAIMS])
  case ids.class.to_s
  when 'Array'
    raise ArgumentError.new 'Invalid Wikidata ids' unless valid_ids? ids
    return fetch_entities(ids, props: props)
  when 'String'
    raise ArgumentError.new 'Invalid Wikidata id' unless valid_id? ids
    return fetch_entity(ids, props: props)
  else
    raise ArgumentError.new 'Invalid Wikidata ids'
  end
end
fetch_entities(ids, props: [Props::ALIASES, Props::LABELS, Props::DESCRIPTIONS, Props::CLAIMS]) click to toggle source

Fetch multiple Entities from Wikidata by id

Makes a call to Wikidata and get the results wrapped in a EntityResult object

  • Parameters :

    • ids -> An array of Wikidata ids, such as [‘Q1’, ‘Q2’, ‘P206’, ‘P16’]

    • props -> The properties to pull from Wikidata (see Props class)

  • Returns :

  • Raises :

    • ArgumentError if ids are not valid Wikidata ids

# File lib/gimme_wikidata.rb, line 93
def fetch_entities(ids, props: [Props::ALIASES, Props::LABELS, Props::DESCRIPTIONS, Props::CLAIMS])
  raise ArgumentError.new 'Invalid Wikidata ids' unless valid_ids? ids
  get_query = WikidataAPI.get_entities_query(ids, props: props)
  response = WikidataAPI.make_call(get_query)
  entity_result = Parser.parse_entity_response(response)
  entity_result.resolve_all_properties
  entity_result.resolve_all_claims if props.include? Props::CLAIMS
  entity_result
end
fetch_entity(id, props: [Props::ALIASES, Props::LABELS, Props::DESCRIPTIONS, Props::CLAIMS]) click to toggle source

Fetch a single Entity from Wikidata by id

  • Parameters :

    • id -> The Wikidata id of the Entity to get

    • props -> The properties to pull from Wikidata (see Props class)

  • Returns :

    • An Entity if successful, and an EntityResponse (with error message) if unsuccessful

  • Raises :

    • ArgumentError if ids are not valid Wikidata ids

# File lib/gimme_wikidata.rb, line 75
def fetch_entity(id, props: [Props::ALIASES, Props::LABELS, Props::DESCRIPTIONS, Props::CLAIMS])
  raise ArgumentError.new 'Invalid Wikidata id' unless valid_id? id
  entity_result = fetch_entities([id], props: props)
  return entity_result unless entity_result.was_successful?
  entity_result.entities.first # Simply return the first element, since there was only 1 Entity to fetch
end
valid_id?(id, type = [:item, :property]) click to toggle source

Valdiates a single id against Wikidata format

Wikidata has ids in the form ‘QN’ or ‘PN’ where Q means ‘Item’, P means ‘Property’, and N is any number.

  • Parameters :

    • id -> A (possible) Wikidata id to be validated, as a String

    • type -> array of symbols representing entity types to check. Defaults to +[:item, :property]+

  • Returns:

    • boolean

# File lib/gimme_wikidata.rb, line 126
def valid_id?(id, type = [:item, :property])

  return false unless id.is_a? String

  prefix =
  case type
  when [:item, :property] then 'QP'
  when [:item] then 'Q'
  when [:property] then 'P'
  else raise ArgumentError.new "type must be a subset of [:item, :property]"
  end

  !(/\A[#{prefix}][0-9]+\z/.match(id).nil?)
end
valid_ids?(ids, type = [:item, :property]) click to toggle source

Valdiates an array of ids against Wikidata format

Wikidata has ids in the form ‘QN’ or ‘PN’ where Q means ‘Item’, P means ‘Property’, and N is any number.

  • Parameters:

    • ids -> An array of (possible) Wikidata ids to be validated, in string form

    • type -> array of symbols representing entity types to check. Defaults to +[:item, :property]+

  • Returns:

    • boolean

# File lib/gimme_wikidata.rb, line 112
def valid_ids?(ids, type = [:item, :property])
  return false unless ids.is_a? Array
  ids.all? { |i| valid_id?(i, type) }
end