Class: Wikidata::Entity

Inherits:
Hashie::Mash
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/wikidata/entity.rb

Direct Known Subclasses

Item

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, opts = {}) ⇒ Entity

Returns a new instance of Entity.



7
8
9
10
# File 'lib/wikidata/entity.rb', line 7

def initialize(hash, opts = {})
  super(hash, opts)
  @_properties = {}
end

Class Method Details

.entity_id(attribute) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/wikidata/entity.rb', line 72

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

Instance Method Details

#idObject



12
13
14
# File 'lib/wikidata/entity.rb', line 12

def id
  self['id'] || self['title']
end

#inspectObject



65
66
67
# File 'lib/wikidata/entity.rb', line 65

def inspect
  "<#{self.class} id: #{id}, title: \"#{title}\">"
end

#properties(code) ⇒ Object



47
48
49
# File 'lib/wikidata/entity.rb', line 47

def properties(code)
  @_properties[code] ||= Array(raw_property(code)).map { |a| Wikidata::Property.build a }
end

#property(code) ⇒ Object



57
58
59
# File 'lib/wikidata/entity.rb', line 57

def property(code)
  properties(code).first
end

#property_id(code) ⇒ Object



61
62
63
# File 'lib/wikidata/entity.rb', line 61

def property_id(code)
  property_ids(code).first
end

#property_ids(code) ⇒ Object



51
52
53
54
55
# File 'lib/wikidata/entity.rb', line 51

def property_ids(code)
  Array(raw_property(code)).map do |attribute|
    self.class.entity_id attribute
  end.compact
end

#property_keysObject



39
40
41
# File 'lib/wikidata/entity.rb', line 39

def property_keys
  claims.keys
end

#property_name(code) ⇒ Object



43
44
45
# File 'lib/wikidata/entity.rb', line 43

def property_name(code)
  Wikidata::Item.find(code).title
end

#titleObject



16
17
18
19
20
21
# File 'lib/wikidata/entity.rb', line 16

def title
  return labels['en'].value if labels && labels['en']
  return sitelinks['en'].value if sitelinks && sitelinks['en']

  title
end

#urlObject



23
24
25
# File 'lib/wikidata/entity.rb', line 23

def url
  Wikidata.settings.item.url.gsub(':id', id)
end