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
The Entity’s aliases (in the language specified by WikidataAPI
). Can be an empty array in the case of an unresolved Entity
.
The Entity’s description (in the language specified by WikidataAPI
). Can be nil in the case of an unresolved Entity
.
The Entity’s label (in the language specified by WikidataAPI
). Can be nil in the case of an unresolved Entity
.
Public Class Methods
# 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
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 all Claims that have a specific value_type
symbol
-
Parameters:
-
type
-> Symbol value of the Claim’svalue_type
attribute
-
-
Returns:
-
An
Array
of Claims
-
# 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
Does this Entity
have any claims?
Returns:
-
boolean
# File lib/gimme_wikidata/entity.rb, line 53 def has_claims? !(@claims.empty?) end
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
Prints a pretty version of the Entity
to console
# File lib/gimme_wikidata/printer.rb, line 7 def print(colour = :blue) heading = "#{label} (#{id})" puts '=' * heading.length puts "#{label} (#{id})".bold.colorize(background: colour) puts '=' * heading.length puts "Description\t".bold + @description puts "Aliases\t\t".bold + @aliases.join(', ') puts "Claims:".underline claims.each {|sc| sc.print } return nil end
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
-> TheProps
to fetch. Defaults toProps::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
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
-> TheProps
to fetch. Defaults toProps::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
Resolves an incomplete Entity
with additional details
-
Parameters:
-
Returns:
-
Raises:
# 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
Returns a simplified version of an Entity’s Claims
-
Returns:
-
Array
of hashes representing simplified Claims. SeeClaim.simplify()
-
# File lib/gimme_wikidata/entity.rb, line 131 def simple_claims claims.map {|c| c.simplify } end