Class: Everypolitician::Popolo::Person

Inherits:
Object
  • Object
show all
Defined in:
lib/everypolitician/popolo.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Person

Returns a new instance of Person.



48
49
50
51
52
53
# File 'lib/everypolitician/popolo.rb', line 48

def initialize(document)
  @document = document
  document.each do |key, value|
    define_singleton_method(key) { value }
  end
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



46
47
48
# File 'lib/everypolitician/popolo.rb', line 46

def document
  @document
end

Instance Method Details

#[](key) ⇒ Object



55
56
57
# File 'lib/everypolitician/popolo.rb', line 55

def [](key)
  document[key]
end

#emailObject



67
68
69
# File 'lib/everypolitician/popolo.rb', line 67

def email
  self[:email]
end

#facebookObject



83
84
85
86
# File 'lib/everypolitician/popolo.rb', line 83

def facebook
  facebook_link = links.find { |d| d[:note] == 'facebook' }
  facebook_link[:url] if facebook_link
end

#genderObject



108
109
110
# File 'lib/everypolitician/popolo.rb', line 108

def gender
  self[:gender]
end

#imageObject



104
105
106
# File 'lib/everypolitician/popolo.rb', line 104

def image
  self[:image]
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/everypolitician/popolo.rb', line 59

def key?(key)
  document.key?(key)
end


63
64
65
# File 'lib/everypolitician/popolo.rb', line 63

def links
  document.fetch(:links, [])
end

#name_at(date) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/everypolitician/popolo.rb', line 88

def name_at(date)
  return name unless key?(:other_names)
  historic = other_names.find_all { |n| n.key?(:end_date) }
  return name if historic.empty?
  at_date = historic.find_all do |n|
    n[:end_date] >= date && (n[:start_date] || '0000-00-00') <= date
  end
  return name if at_date.empty?
  fail Error, "Too many names at #{date}: #{at_date}" if at_date.count > 1
  at_date.first[:name]
end

#sort_nameObject



100
101
102
# File 'lib/everypolitician/popolo.rb', line 100

def sort_name
  name
end

#twitterObject



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/everypolitician/popolo.rb', line 71

def twitter
  if key?(:contact_details)
    if twitter_contact = self[:contact_details].find { |d| d[:type] == 'twitter' }
      twitter_contact[:value].strip
    end
  elsif key?(:links)
    if twitter_link = self[:links].find { |d| d[:note][/twitter/i] }
      twitter_link[:url].strip
    end
  end
end