class Wikisnakker::Item

Constants

PROPERTY_REGEX

Attributes

raw[R]

Public Class Methods

find(ids) click to toggle source
# File lib/wikisnakker/item.rb, line 3
def self.find(ids)
  lookup = Lookup.find(ids)
  data = lookup.values.reject { |d| d[:missing] == '' }
  inflated = data.map { |rd| new(rd) }
  ids.is_a?(Array) ? inflated : inflated.first
end
new(raw) click to toggle source
# File lib/wikisnakker/item.rb, line 10
def initialize(raw)
  @raw = raw
end

Public Instance Methods

[](key) click to toggle source
# File lib/wikisnakker/item.rb, line 26
def [](key)
  __send__(key)
end
aliases(lang) click to toggle source
# File lib/wikisnakker/item.rb, line 82
def aliases(lang)
  return [] unless all_aliases.key?(lang.to_sym)
  all_aliases[lang.to_sym].map { |a| a[:value] }
end
all_aliases() click to toggle source
# File lib/wikisnakker/item.rb, line 53
def all_aliases
  raw[:aliases]
end
description(lang) click to toggle source
# File lib/wikisnakker/item.rb, line 77
def description(lang)
  return nil unless descriptions.key?(lang.to_sym)
  descriptions[lang.to_sym][:value]
end
descriptions() click to toggle source
# File lib/wikisnakker/item.rb, line 49
def descriptions
  raw[:descriptions] || {}
end
first_property(pid) click to toggle source
# File lib/wikisnakker/item.rb, line 37
def first_property(pid)
  property(pid).first
end
id() click to toggle source
# File lib/wikisnakker/item.rb, line 41
def id
  raw[:id]
end
label(lang) click to toggle source
# File lib/wikisnakker/item.rb, line 72
def label(lang)
  return nil unless labels.key?(lang.to_sym)
  labels[lang.to_sym][:value]
end
labels() click to toggle source
# File lib/wikisnakker/item.rb, line 45
def labels
  raw[:labels]
end
method_missing(method_name, *arguments, &block) click to toggle source
Calls superclass method
# File lib/wikisnakker/item.rb, line 16
def method_missing(method_name, *arguments, &block)
  pid, plural = method_name.to_s.scan(PROPERTY_REGEX).flatten
  return super unless pid
  plural.empty? ? first_property(pid) : property(pid)
end
properties() click to toggle source
# File lib/wikisnakker/item.rb, line 57
def properties
  raw[:claims].keys
end
property(pid) click to toggle source
# File lib/wikisnakker/item.rb, line 30
def property(pid)
  # A claim's rank can be either preferred, normal or deprecated. We sort them by
  # rank in reverse order because lexicographic ordering happens to work for the
  # known ranks.
  raw[:claims][pid.to_sym].to_a.map { |c| Claim.new(c) }.group_by(&:rank).sort.reverse.map(&:last).flatten
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/wikisnakker/item.rb, line 22
def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.match(PROPERTY_REGEX) || super
end
to_s() click to toggle source

TODO: have an option that defaults to a different language

# File lib/wikisnakker/item.rb, line 68
def to_s
  labels.key?(:en) ? labels[:en][:value] : @id
end