Class: Wikidata::Entity

Inherits:
HashedObject show all
Defined in:
lib/wikidata/entity.rb

Direct Known Subclasses

Item, Property

Instance Attribute Summary

Attributes inherited from HashedObject

#data_hash

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from HashedObject

#initialize, #method_missing

Constructor Details

This class inherits a constructor from Wikidata::HashedObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Wikidata::HashedObject

Class Method Details

.find_all(query) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/wikidata/entity.rb', line 6

def self.find_all query

  found_objects = []

  query = {
    action: 'wbgetentities',
    sites: 'enwiki',
    format: 'json'
  }.merge(Wikidata.default_languages_hash).merge(query)

  ids = query[:ids] || []
  titles = query[:titles] || []

  # Split IDs and titles
  ids = ids.split("|") if ids && ids.class == String
  titles = titles.split("|") if titles && titles.class == String

  # Reject already cached values
  fetchable_ids = ids.reject do |id|
    if val = IdentityMap.cached_value(id)
      found_objects << val
      true
    else
      false
    end
  end
  fetchable_titles = titles.reject do |title|
    if val = IdentityMap.cached_value(title)
      found_objects << val
      true
    else
      false
    end
  end

  # Fetch by IDs
  if fetchable_ids.length > 0
    fetchable_ids.in_groups_of(50, false) do |group|
      found_objects.concat query_and_build_objects(query.merge(ids: group.join("|")))
    end
  end

  # Fetch by titles
  if fetchable_titles.length > 0
    fetchable_titles.in_groups_of(50, false) do |group|
      found_objects.concat query_and_build_objects(query.merge(titles: group.join("|")))
    end
  end

  found_objects
end

.find_all_by_id(id, query = {}) ⇒ Object



68
69
70
# File 'lib/wikidata/entity.rb', line 68

def self.find_all_by_id id, query = {}
  find_all({ids: id}.merge(query))
end

.find_all_by_title(title, query = {}) ⇒ Object



76
77
78
# File 'lib/wikidata/entity.rb', line 76

def self.find_all_by_title title, query = {}
  find_all({titles: title}.merge(query))
end

.find_by_id(*args) ⇒ Object



72
73
74
# File 'lib/wikidata/entity.rb', line 72

def self.find_by_id *args
  find_all_by_id(*args).first
end

.find_by_title(*args) ⇒ Object



80
81
82
# File 'lib/wikidata/entity.rb', line 80

def self.find_by_title *args
  find_all_by_title(*args).first
end

.query_and_build_objects(query) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/wikidata/entity.rb', line 58

def self.query_and_build_objects(query)
  response = HTTParty.get('http://www.wikidata.org/w/api.php', {query: query})
  puts "Getting: #{query}".yellow if Wikidata.verbose?
  response['entities'].map do |entity_id, entity_hash|
    item = new(entity_hash)
    IdentityMap.cache!(entity_id, item)
    item
  end
end

Instance Method Details

#delocalize(hash, locale = I18n.default_locale) ⇒ Object



88
89
90
91
92
# File 'lib/wikidata/entity.rb', line 88

def delocalize(hash, locale = I18n.default_locale)
  return nil unless hash
  h = hash[locale.to_s]
  h ? h.value : nil
end

#description(*args) ⇒ Object



98
99
100
# File 'lib/wikidata/entity.rb', line 98

def description(*args)
  delocalize self.data_hash.descriptions, *args
end

#inspectObject



84
85
86
# File 'lib/wikidata/entity.rb', line 84

def inspect
  "<#{self.class.to_s} id=#{id}>"
end

#label(*args) ⇒ Object



94
95
96
# File 'lib/wikidata/entity.rb', line 94

def label(*args)
  delocalize self.data_hash.labels, *args
end