Class: Wikidatum::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/wikidatum/item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idString (readonly)

Returns the ID of the Wikibase item, in the format “Q123”.

Returns:

  • (String)

    the ID of the Wikibase item, in the format “Q123”.



5
6
7
# File 'lib/wikidatum/item.rb', line 5

def id
  @id
end

Instance Method Details

#aliases(langs: []) ⇒ Array<Wikidatum::Term>

Get aliases for the item.

Examples:

Get aliases for all languages.

item.aliases

Get the aliases for one or more specific languages.

item.aliases(langs: ['en', 'es'])

Also accepts symbols.

item.aliases(langs: [:en, :es])

Parameters:

  • langs (Array<Symbol, String>) (defaults to: [])

    If unspecified, will return all aliases for all languages.

Returns:



98
99
100
101
102
103
# File 'lib/wikidatum/item.rb', line 98

def aliases(langs: [])
  return @aliases if langs.empty?

  langs.map!(&:to_s)
  @aliases.filter { |al| langs.include?(al.lang) }
end

#description(lang:) ⇒ Wikidatum::Term?

Get the description for an item in a given language.

Examples:

Get the description in a given language.

item.description(lang: 'fr')

Also accepts symbols.

item.description(lang: :en)

You can also use the LanguageCodes constants to write code that’s easier to read.

item.description(lang: Wikidatum::LanguageCodes::English)

Parameters:

  • lang (String, Symbol)

Returns:



67
68
69
# File 'lib/wikidatum/item.rb', line 67

def description(lang:)
  @descriptions.find { |desc| desc.lang == lang.to_s }
end

#descriptions(langs: []) ⇒ Array<Wikidatum::Term>

Get descriptions for an item.

Examples:

Get all descriptions on the item.

item.descriptions

Get the descriptions for a few specific languages.

item.descriptions(langs: ['en', 'es', 'fr'])

Parameters:

  • langs (Array<String, Symbol>) (defaults to: [])

Returns:



80
81
82
83
84
85
# File 'lib/wikidatum/item.rb', line 80

def descriptions(langs: [])
  return @descriptions if langs.empty?

  langs.map!(&:to_s)
  @descriptions.filter { |desc| langs.include?(desc.lang) }
end

#label(lang:) ⇒ Wikidatum::Term?

Get the label for an item in a given language.

Examples:

Get the label of the item for a given language.

item.label(lang: 'es')

Also accepts symbols.

item.label(lang: :en)

You can also use the LanguageCodes constants to write code that’s easier to read.

item.label(lang: Wikidatum::LanguageCodes::English)

Parameters:

  • lang (String, Symbol)

Returns:



36
37
38
# File 'lib/wikidatum/item.rb', line 36

def label(lang:)
  @labels.find { |label| label.lang == lang.to_s }
end

#labels(langs: []) ⇒ Array<Wikidatum::Term>

Get labels for an item.

Examples:

Get all labels on the item.

item.labels

Get the labels for a few specific languages.

item.labels(langs: ['en', 'es', 'fr'])

Parameters:

  • langs (Array<String, Symbol>) (defaults to: [])

Returns:



49
50
51
52
53
54
# File 'lib/wikidatum/item.rb', line 49

def labels(langs: [])
  return @labels if langs.empty?

  langs.map!(&:to_s)
  @labels.filter { |label| langs.include?(label.lang) }
end

Get a specific sitelink based on its shortcode.

Examples:

item.sitelink(site: 'enwiki')

Using a symbol key

item.sitelink(site: :enwiki)

Parameters:

  • site (String, Symbol)

    The shortcode for the sitelink you want to access, e.g. ‘enwiki’ or ‘commons’. Can be a string or a symbol.

Returns:



129
130
131
# File 'lib/wikidatum/item.rb', line 129

def sitelink(site:)
  @sitelinks.find { |sitelink| sitelink.site == site.to_s }
end

Get the sitelinks on the item.

Examples:

Getting all sitelinks for the item.

item.sitelinks

Getting only a few specific sitelinks.

item.sitelinks(sites: ['enwiki', 'eswiki', 'commons'])

Also accepts symbols.

item.sitelinks(sites: [:enwiki, :eswiki])

Parameters:

  • sites (Array<String, Symbol>) (defaults to: [])

    An array of sitelink shortcodes to return (e.g. ['enwiki', 'eswiki']), if not provided then all sitelinks will be returned.

Returns:



144
145
146
147
148
149
# File 'lib/wikidatum/item.rb', line 144

def sitelinks(sites: [])
  return @sitelinks if sites.empty?

  sites.map!(&:to_s)
  @sitelinks.filter { |sitelink| sites.include?(sitelink.site) }
end

#statements(properties: []) ⇒ Array<Wikidatum::Statement>

Get statements on the item.

Examples:

Get all statements.

item.statements

Get statements for one or more specific properties.

item.statements(properties: ['P123', 'P124'])

Parameters:

  • properties (Array<String>) (defaults to: [])

    One or more Wikidata properties, in the format of ['P123']. If unspecified, will return all statements for the item.

Returns:



114
115
116
117
118
# File 'lib/wikidatum/item.rb', line 114

def statements(properties: [])
  return @statements if properties.empty?

  @statements.filter { |statement| properties.include?(statement.property_id) }
end

#to_hHash

Convert the item, including all of its labels, descriptions, aliases, statements, and sitelinks, to a Ruby hash.

This can be useful for debugging purposes.

Examples:

View the contents of an item according to the Wikidatum gem by outputting prettified JSON.

require 'json'

puts JSON.pretty_generate(item.to_h)

Returns:

  • (Hash)


162
163
164
165
166
167
168
169
170
171
# File 'lib/wikidatum/item.rb', line 162

def to_h
  {
    id: @id,
    labels: @labels.map(&:to_h),
    descriptions: @descriptions.map(&:to_h),
    aliases: @aliases.map(&:to_h),
    statements: @statements.map(&:to_h),
    sitelinks: @sitelinks.map(&:to_h)
  }
end