class GimmeWikidata::Entity

Models an Entity on Wikidata.

An Entity is suclassed into Item and Property.

An Entity has to have a valid Wikidata id, but the other class data is optional

See: www.wikidata.org/wiki/Wikidata:Glossary#Entities.2C_items.2C_properties_and_queries

Attributes

aliases[RW]

The Entity’s aliases (in the language specified by WikidataAPI). Can be an empty array in the case of an unresolved Entity.

claims[RW]

Each Entity has a number of claims, stored as an array of Claim objects.

description[RW]

The Entity’s description (in the language specified by WikidataAPI). Can be nil in the case of an unresolved Entity.

id[R]

The Wikidata id of the Entity in the form ‘PN’ or ‘QN’ where ‘P’ means Property and ‘Q’ means Item. N can be any positive integer.

label[RW]

The Entity’s label (in the language specified by WikidataAPI). Can be nil in the case of an unresolved Entity.

Public Class Methods

new(id, label = nil, description = nil, aliases = nil, claims = []) click to toggle source
# File lib/gimme_wikidata/entity.rb, line 29
def initialize(id, label = nil, description = nil, aliases = nil, claims = [])
  throw ArgumentError.new "Invalid Wikidata Entity id: #{id}" unless GimmeWikidata.valid_id? id
  @id = id
  @label = label
  @description = description
  @aliases = aliases
  @claims = claims
  @claims = [] if @claims.nil?
end

Public Instance Methods

claims_with_property_id(id) click to toggle source

Get all the Claims which have Properties with the passed id

# File lib/gimme_wikidata/entity.rb, line 59
def claims_with_property_id(id)
  raise ArgumentError.new 'Invaild Wikidata Id' unless GimmeWikidata.valid_id? id
  claims = []
  @claims.each do |c|
    claims << c if c.property.id == id
  end
  claims
end
get_claims_by_value_type(type) click to toggle source

Get all Claims that have a specific value_type symbol

  • Parameters:

    • type -> Symbol value of the Claim’s value_type attribute

  • Returns:

# File lib/gimme_wikidata/entity.rb, line 118
def get_claims_by_value_type(type)
  return [] unless has_claims?
  claims = []
  @claims.each do |c|
    claims << c if c.value_type == type.to_sym
  end
  return claims
end
has_claims?() click to toggle source

Does this Entity have any claims?

Returns:

  • boolean

# File lib/gimme_wikidata/entity.rb, line 53
def has_claims?
  !(@claims.empty?)
end
has_fingerprint?() click to toggle source

Does this Entity have a fingerprint (an id, title and description)

Returns:

  • boolean

# File lib/gimme_wikidata/entity.rb, line 44
def has_fingerprint?
  !(@id.nil? || @label.nil? || @description.nil?)
end
print(colour = :blue) click to toggle source

Prints a pretty version of the Entity to console

resolve_claims(props = [Props::LABELS]) click to toggle source

Resolves the Claims that refer to Entities

Claims often refer to another entity on Wikidata. By default, we will only know the ids of these with one fetch Entity call. This therefore gets more data regarding these.

  • Parameters:

    • props -> The Props to fetch. Defaults to Props::LABELS

# File lib/gimme_wikidata/entity.rb, line 99
def resolve_claims(props = [Props::LABELS])
  return unless has_claims?
  item_claims = get_claims_by_value_type(:item)
  item_ids = item_claims.map {|c| c.value.id }
  entity_result = GimmeWikidata.fetch(item_ids, props: props)
  entity_result.entities.each do |item_details|
    item_index = item_ids.index(item_details.id)
    item_to_resolve = item_claims[item_index].value
    item_to_resolve.resolve_with(item_details)
  end
end
resolve_properties(props = [Props::LABELS]) click to toggle source

Get more data on an Entity’s Properties

Entities have Properties (through Claims), which are by default only returned as ids. This function gets more details for each of these.

  • Parameters:

    • props -> The Props to fetch. Defaults to Props::LABELS.

  • Returns:

    • nil

  • Raises:

    • StandardError -> if there was an error in fetching the data

# File lib/gimme_wikidata/entity.rb, line 79
def resolve_properties(props = [Props::LABELS])
  return nil unless has_claims?
  unique_property_ids = (claims.map {|c| c.property.id}).uniq
  # Get only the labels for the Entity's Properties from the Wikidata API
  response = GimmeWikidata::fetch(unique_property_ids, props: [Props::LABELS])
  raise StandardError.new "Could not resolve Entity (#{@id} - #{@label}) properties" unless response.was_successful?
  response.entities.each do |property_details|
    claims = claims_with_property_id(property_details.id)
    claims.map {|c| c.property.resolve_with(property_details)}
  end
  return nil
end
resolve_with(entity_details) click to toggle source

Resolves an incomplete Entity with additional details

  • Parameters:

  • Returns:

  • Raises:

    • ArgumentError -> if attempting to resolve an Item with a Property, or a Property with an Item

    • StandardError -> if attempting to resolve an Entity with details that have unequal ids

# File lib/gimme_wikidata/entity.rb, line 144
def resolve_with(entity_details)
  raise ArgumentError.new "Attempting to resolve an Item with a Property or vice versa" if entity_details.id[0] != @id[0]
  raise StandardError.new "Attempting to resolve Entity with id #{@id} with entity_details with id #{entity_details.id}" unless @id == entity_details.id
  #TODO: Wouldn't it be easier to simply overwrite the Entity? (self = entity_details?)
  @label = entity_details.label
  @description = entity_details.description
  @aliases = entity_details.aliases
  @claims = entity_details.claims
  @claims = [] if @claims.nil?
end
simple_claims() click to toggle source

Returns a simplified version of an Entity’s Claims

# File lib/gimme_wikidata/entity.rb, line 131
def simple_claims
  claims.map {|c| c.simplify }
end