Class: Nobel::Laureate

Inherits:
Object
  • Object
show all
Defined in:
lib/nobel/laureate.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Laureate

Returns a new instance of Laureate.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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

Instance Attribute Details

#bornObject (readonly)

Returns the value of attribute born.



24
25
26
# File 'lib/nobel/laureate.rb', line 24

def born
  @born
end

#born_cityObject (readonly)

Returns the value of attribute born_city.



24
25
26
# File 'lib/nobel/laureate.rb', line 24

def born_city
  @born_city
end

#born_countryObject (readonly)

Returns the value of attribute born_country.



24
25
26
# File 'lib/nobel/laureate.rb', line 24

def born_country
  @born_country
end

#born_country_codeObject (readonly)

Returns the value of attribute born_country_code.



24
25
26
# File 'lib/nobel/laureate.rb', line 24

def born_country_code
  @born_country_code
end

#dataObject (readonly)

Returns the value of attribute data.



24
25
26
# File 'lib/nobel/laureate.rb', line 24

def data
  @data
end

#diedObject (readonly)

Returns the value of attribute died.



24
25
26
# File 'lib/nobel/laureate.rb', line 24

def died
  @died
end

#died_cityObject (readonly)

Returns the value of attribute died_city.



24
25
26
# File 'lib/nobel/laureate.rb', line 24

def died_city
  @died_city
end

#died_countryObject (readonly)

Returns the value of attribute died_country.



24
25
26
# File 'lib/nobel/laureate.rb', line 24

def died_country
  @died_country
end

#died_country_codeObject (readonly)

Returns the value of attribute died_country_code.



24
25
26
# File 'lib/nobel/laureate.rb', line 24

def died_country_code
  @died_country_code
end

#firstnameObject (readonly)

Returns the value of attribute firstname.



24
25
26
# File 'lib/nobel/laureate.rb', line 24

def firstname
  @firstname
end

#idObject (readonly)

Returns the value of attribute id.



24
25
26
# File 'lib/nobel/laureate.rb', line 24

def id
  @id
end

#motivationObject (readonly)

Returns the value of attribute motivation.



24
25
26
# File 'lib/nobel/laureate.rb', line 24

def motivation
  @motivation
end

#shareObject (readonly)

Returns the value of attribute share.



24
25
26
# File 'lib/nobel/laureate.rb', line 24

def share
  @share
end

#surnameObject (readonly)

Returns the value of attribute surname.



24
25
26
# File 'lib/nobel/laureate.rb', line 24

def surname
  @surname
end

Class Method Details

.all(params = {}) ⇒ Object Also known as: query



13
14
15
# File 'lib/nobel/laureate.rb', line 13

def all(params = {})
  get_data(params).map { |c| new(c) }
end

.find(id) ⇒ Object



8
9
10
11
# 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 = {}) ⇒ Object



19
20
21
# File 'lib/nobel/laureate.rb', line 19

def get_data(params = {})
  Nobel.api.laureate(params)['laureates'] || []
end

Instance Method Details

#categoriesObject



69
70
71
# File 'lib/nobel/laureate.rb', line 69

def categories
  won(:category)
end

#nameObject



59
60
61
# File 'lib/nobel/laureate.rb', line 59

def name
  "#{firstname} #{surname}".strip
end

#prizesObject



63
64
65
66
67
# File 'lib/nobel/laureate.rb', line 63

def prizes
  @prizes ||= (data['prizes'] || []).map do |p|
    Prize.new(p)
  end
end

#reload!Object



54
55
56
57
# File 'lib/nobel/laureate.rb', line 54

def reload!
  initialize self.class.get_data(id: id).first
  self
end

#yearsObject



73
74
75
# File 'lib/nobel/laureate.rb', line 73

def years
  won(:year)
end