Class: Wikimelon::Resource
- Inherits:
-
Object
- Object
- Wikimelon::Resource
- Defined in:
- lib/wikimelon/resource.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Class Method Summary collapse
- .find(id, revision_id: nil) ⇒ Object
-
.find_many(ids) ⇒ Object
Fetch multiple resources by ID in batches of up to 50 (the wbgetentities hard limit).
-
.search(query, language: nil, limit: 10) ⇒ Object
Fuzzy-search by label or alias.
Instance Method Summary collapse
- #aliases(lang = nil) ⇒ Object
- #claims(property_id) ⇒ Object
- #description(lang = nil) ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(id, data) ⇒ Resource
constructor
A new instance of Resource.
- #label(lang = nil) ⇒ Object
Constructor Details
#initialize(id, data) ⇒ Resource
Returns a new instance of Resource.
27 28 29 30 31 |
# File 'lib/wikimelon/resource.rb', line 27 def initialize(id, data) @id = id @raw = data @entity = data.dig('entities', id) end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/wikimelon/resource.rb', line 5 def id @id end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
5 6 7 |
# File 'lib/wikimelon/resource.rb', line 5 def raw @raw end |
Class Method Details
.find(id, revision_id: nil) ⇒ Object
7 8 9 |
# File 'lib/wikimelon/resource.rb', line 7 def self.find(id, revision_id: nil) new(id, Wikimelon.entity(id, revision_id: revision_id)) end |
.find_many(ids) ⇒ Object
Fetch multiple resources by ID in batches of up to 50 (the wbgetentities hard limit). Returns wrapped resources in the same order as the input.
13 14 15 16 17 18 |
# File 'lib/wikimelon/resource.rb', line 13 def self.find_many(ids) ids.each_slice(50).flat_map do |batch| data = Wikimelon.entities(batch) batch.map { |id| new(id, data) } end end |
.search(query, language: nil, limit: 10) ⇒ Object
Fuzzy-search by label or alias. Returns Array<SearchResult>.
21 22 23 24 25 |
# File 'lib/wikimelon/resource.rb', line 21 def self.search(query, language: nil, limit: 10) type = self == Property ? 'property' : 'item' res = Wikimelon.search(query, type: type, language: language, limit: limit) (res['search'] || []).map { |hit| SearchResult.new(hit) } end |
Instance Method Details
#aliases(lang = nil) ⇒ Object
45 46 47 |
# File 'lib/wikimelon/resource.rb', line 45 def aliases(lang = nil) (@entity&.dig('aliases', lang || Wikimelon.default_language) || []).map { |a| a['value'] } end |
#claims(property_id) ⇒ Object
49 50 51 |
# File 'lib/wikimelon/resource.rb', line 49 def claims(property_id) (@entity&.dig('claims', property_id) || []).map { |c| Statement.new(c) } end |
#description(lang = nil) ⇒ Object
41 42 43 |
# File 'lib/wikimelon/resource.rb', line 41 def description(lang = nil) @entity&.dig('descriptions', lang || Wikimelon.default_language, 'value') end |
#exists? ⇒ Boolean
33 34 35 |
# File 'lib/wikimelon/resource.rb', line 33 def exists? !@entity.nil? && @entity['id'] == @id end |
#label(lang = nil) ⇒ Object
37 38 39 |
# File 'lib/wikimelon/resource.rb', line 37 def label(lang = nil) @entity&.dig('labels', lang || Wikimelon.default_language, 'value') end |