class Nobel::Laureate

Attributes

born[R]
born_city[R]
born_country[R]
born_country_code[R]
data[R]
died[R]
died_city[R]
died_country[R]
died_country_code[R]
firstname[R]
id[R]
motivation[R]
share[R]
surname[R]

Public Class Methods

all(params = {}) click to toggle source
# File lib/nobel/laureate.rb, line 13
def all(params = {})
  get_data(params).map { |c| new(c) }
end
Also aliased as: query
find(id) click to toggle source
# File lib/nobel/laureate.rb, line 8
def find(id)
  data = get_data(id: id)
  new(data.first) if data.any?
end
get_data(params = {}) click to toggle source
# File lib/nobel/laureate.rb, line 19
def get_data(params = {})
  Nobel.api.laureate(params)['laureates'] || []
end
new(data) click to toggle source
# File lib/nobel/laureate.rb, line 28
def initialize(data)
  @data = data || {}

  year_regex = /^\d{4}-[0-1][1-9]-\d{2}$/

  @data.tap do |d|
    @id                = Integer(d['id'])

    @firstname         = d['firstname']
    @surname           = d['surname']

    @share             = Integer(d['share'])  if d['share']
    @motivation        = d['motivation']      if d['motivation']

    @born              = Date.parse d['born'] if d['born'] =~ year_regex
    @born_city         = d['bornCity']        if d['bornCity']
    @born_country      = d['bornCountry']     if d['bornCountry']
    @born_country_code = d['bornCountryCode'] if d['bornCountryCode']

    @died              = Date.parse d['died'] if d['died'] =~ year_regex
    @died_city         = d['diedCity']        if d['diedCity']
    @died_country      = d['diedCountry']     if d['diedCountry']
    @died_country_code = d['diedCountryCode'] if d['diedCountryCode']
  end
end
query(params = {})
Alias for: all

Public Instance Methods

categories() click to toggle source
# File lib/nobel/laureate.rb, line 69
def categories
  won(:category)
end
name() click to toggle source
# File lib/nobel/laureate.rb, line 59
def name
  "#{firstname} #{surname}".strip
end
prizes() click to toggle source
# File lib/nobel/laureate.rb, line 63
def prizes
  @prizes ||= (data['prizes'] || []).map do |p|
    Prize.new(p)
  end
end
reload!() click to toggle source
# File lib/nobel/laureate.rb, line 54
def reload!
  initialize self.class.get_data(id: id).first
  self
end
years() click to toggle source
# File lib/nobel/laureate.rb, line 73
def years
  won(:year)
end

Private Instance Methods

won(field) click to toggle source
# File lib/nobel/laureate.rb, line 79
def won(field)
  prizes.map(&field).uniq
end