class Wikidata::Entity

Attributes

hash[RW]

Public Class Methods

entity_id(attribute) click to toggle source

TODO Handle other types www.wikidata.org/wiki/Wikidata:Glossary#Entities.2C_items.2C_properties_and_queries

# File lib/wikidata/entity.rb, line 74
def entity_id attribute
  return unless attribute.mainsnak.datavalue
  attribute.mainsnak.datavalue.value.tap do |h|
    case h['entity-type']
      when 'item'
        return "Q#{h['numeric-id']}"
      else
        return nil
    end
  end
end
new(hash) click to toggle source
# File lib/wikidata/entity.rb, line 8
def initialize hash
  @hash = Hashie::Mash.new hash
  @_properties = {}
end

Public Instance Methods

id() click to toggle source
# File lib/wikidata/entity.rb, line 13
def id
  return hash['id'] if hash['id']
  return hash['title'] if hash['title']
end
inspect() click to toggle source
# File lib/wikidata/entity.rb, line 66
def inspect
  "<#{self.class} id: #{id}, title: \"#{title}\">"
end
properties(code) click to toggle source
# File lib/wikidata/entity.rb, line 48
def properties code
  @_properties[code] ||= Array(raw_property(code)).map {|a| Wikidata::Property.build a }
end
property(code) click to toggle source
# File lib/wikidata/entity.rb, line 58
def property code
  properties(code).first
end
property_id(code) click to toggle source
# File lib/wikidata/entity.rb, line 62
def property_id code
  property_ids(code).first
end
property_ids(code) click to toggle source
# File lib/wikidata/entity.rb, line 52
def property_ids code
  Array(raw_property(code)).map do |attribute|
    self.class.entity_id attribute
  end.compact
end
property_keys() click to toggle source
# File lib/wikidata/entity.rb, line 40
def property_keys
  hash.claims.keys
end
property_name(code) click to toggle source
# File lib/wikidata/entity.rb, line 44
def property_name code
  Wikidata::Item.find(code).title
end
title() click to toggle source
# File lib/wikidata/entity.rb, line 18
def title
  return labels['en'].value if labels && labels['en']
  return sitelinks['en'].value if sitelinks && sitelinks['en']
  hash['title'] if hash['title']
end
url() click to toggle source
# File lib/wikidata/entity.rb, line 24
def url
  Wikidata.settings.item.url.gsub(':id', id)
end

Private Instance Methods

raw_property(code) click to toggle source
# File lib/wikidata/entity.rb, line 89
def raw_property code
  return unless hash.claims
  hash.claims[code]
end