Class: Wikisnakker::Item

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

Constant Summary collapse

PROPERTY_REGEX =
/^(P\d+)(s?)$/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Item

Returns a new instance of Item.



10
11
12
# File 'lib/wikisnakker/item.rb', line 10

def initialize(raw)
  @raw = raw
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object



16
17
18
19
20
# 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

Class Method Details

.find(ids) ⇒ Object



3
4
5
6
7
8
# 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

Instance Method Details

#[](key) ⇒ Object



26
27
28
# File 'lib/wikisnakker/item.rb', line 26

def [](key)
  __send__(key)
end

#aliases(lang) ⇒ Object



82
83
84
85
# 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_aliasesObject



53
54
55
# File 'lib/wikisnakker/item.rb', line 53

def all_aliases
  raw[:aliases]
end

#description(lang) ⇒ Object



77
78
79
80
# File 'lib/wikisnakker/item.rb', line 77

def description(lang)
  return nil unless descriptions.key?(lang.to_sym)
  descriptions[lang.to_sym][:value]
end

#descriptionsObject



49
50
51
# File 'lib/wikisnakker/item.rb', line 49

def descriptions
  raw[:descriptions] || {}
end

#first_property(pid) ⇒ Object



37
38
39
# File 'lib/wikisnakker/item.rb', line 37

def first_property(pid)
  property(pid).first
end

#idObject



41
42
43
# File 'lib/wikisnakker/item.rb', line 41

def id
  raw[:id]
end

#label(lang) ⇒ Object



72
73
74
75
# File 'lib/wikisnakker/item.rb', line 72

def label(lang)
  return nil unless labels.key?(lang.to_sym)
  labels[lang.to_sym][:value]
end

#labelsObject



45
46
47
# File 'lib/wikisnakker/item.rb', line 45

def labels
  raw[:labels]
end

#propertiesObject



57
58
59
# File 'lib/wikisnakker/item.rb', line 57

def properties
  raw[:claims].keys
end

#property(pid) ⇒ Object



30
31
32
33
34
35
# 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) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# 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


61
62
63
64
65
# File 'lib/wikisnakker/item.rb', line 61

def sitelinks
  Hash[raw[:sitelinks].map do |key, value|
    [key.to_sym, Sitelink.new(value)]
  end]
end

#to_sObject

TODO: have an option that defaults to a different language



68
69
70
# File 'lib/wikisnakker/item.rb', line 68

def to_s
  labels.key?(:en) ? labels[:en][:value] : @id
end