Class: Hdo::StortingImporter::Representative

Inherits:
Object
  • Object
show all
Includes:
HasJsonSchema, Inspectable, IvarEquality
Defined in:
lib/hdo/storting_importer/representative.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Inspectable

#short_inspect_string

Methods included from IvarEquality

#==, #__ivars__, #hash

Methods included from HasJsonSchema

#as_json, included, schemas, #to_json, #valid?, #validate!

Constructor Details

#initialize(external_id, first_name, last_name, gender, date_of_birth, date_of_death, district, party, committees, period) ⇒ Representative

Returns a new instance of Representative.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/hdo/storting_importer/representative.rb', line 84

def initialize(external_id, first_name, last_name, gender, date_of_birth, date_of_death, district, party, committees, period)
  @external_id   = external_id
  @first_name    = first_name
  @last_name     = last_name
  @gender        = gender
  @date_of_birth = date_of_birth
  @date_of_death = date_of_death
  @district      = district
  @party         = party
  @committees    = committees
  @period        = period

  @vote_result   = nil
end

Instance Attribute Details

#committeesObject (readonly)

Returns the value of attribute committees.



10
11
12
# File 'lib/hdo/storting_importer/representative.rb', line 10

def committees
  @committees
end

#date_of_birthObject (readonly)

Returns the value of attribute date_of_birth.



10
11
12
# File 'lib/hdo/storting_importer/representative.rb', line 10

def date_of_birth
  @date_of_birth
end

#date_of_deathObject (readonly)

Returns the value of attribute date_of_death.



10
11
12
# File 'lib/hdo/storting_importer/representative.rb', line 10

def date_of_death
  @date_of_death
end

#districtObject (readonly)

Returns the value of attribute district.



10
11
12
# File 'lib/hdo/storting_importer/representative.rb', line 10

def district
  @district
end

#external_idObject (readonly)

Returns the value of attribute external_id.



10
11
12
# File 'lib/hdo/storting_importer/representative.rb', line 10

def external_id
  @external_id
end

#first_nameObject (readonly)

Returns the value of attribute first_name.



10
11
12
# File 'lib/hdo/storting_importer/representative.rb', line 10

def first_name
  @first_name
end

#genderObject (readonly)

Returns the value of attribute gender.



10
11
12
# File 'lib/hdo/storting_importer/representative.rb', line 10

def gender
  @gender
end

#last_nameObject (readonly)

Returns the value of attribute last_name.



10
11
12
# File 'lib/hdo/storting_importer/representative.rb', line 10

def last_name
  @last_name
end

#partyObject (readonly)

Returns the value of attribute party.



10
11
12
# File 'lib/hdo/storting_importer/representative.rb', line 10

def party
  @party
end

#periodObject (readonly)

Returns the value of attribute period.



10
11
12
# File 'lib/hdo/storting_importer/representative.rb', line 10

def period
  @period
end

#vote_resultObject

Returns the value of attribute vote_result.



13
14
15
# File 'lib/hdo/storting_importer/representative.rb', line 13

def vote_result
  @vote_result
end

Class Method Details

.exampleObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hdo/storting_importer/representative.rb', line 17

def self.example
  new(
    'ADA',
    'André Oktay',
    'Dahl',
    'M',
    '1975-07-07T00:00:00',
    '0001-01-01T00:00:00',
    'Akershus',
    'Høyre',
    ['Justiskomiteen'],
    '2011-2012'
  )
end

.from_hash(hash) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hdo/storting_importer/representative.rb', line 67

def self.from_hash(hash)
  v = new hash['externalId'],
          hash['firstName'],
          hash['lastName'],
          hash['gender'],
          hash['dateOfBirth'],
          hash['dateOfDeath'],
          hash['district'],
          hash['party'],
          hash['committees'],
          hash['period']

  v.vote_result = hash['voteResult']

  v
end

.from_storting_doc(doc) ⇒ Object



36
37
38
39
40
41
# File 'lib/hdo/storting_importer/representative.rb', line 36

def self.from_storting_doc(doc)
  nodes = doc.css("dagensrepresentant")
  nodes += doc.css("representant")

  nodes.map { |e| from_storting_node(e) }
end

.from_storting_node(node) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hdo/storting_importer/representative.rb', line 43

def self.from_storting_node(node)
  district_node = node.css("fylke navn").first
  district      = district_node ? district_node.text : ''

  party_node = node.css("parti navn").first
  party      = party_node ? party_node.text : ''

  committees = node.css("komite").map { |c| c.css("navn").text.strip }
  period     = '2011-2012' # FIXME

  new(
    node.css("id").first.text,
    node.css("fornavn").first.text,
    node.css("etternavn").first.text,
    node.css("kjoenn").first.text == "mann" ? 'M' : 'F',
    node.css("foedselsdato").first.text,
    node.css("doedsdato").first.text,
    district,
    party,
    committees,
    period
  )
end

.json_exampleObject



32
33
34
# File 'lib/hdo/storting_importer/representative.rb', line 32

def self.json_example
  Util.json_pretty example
end

Instance Method Details

#short_inspectObject



99
100
101
# File 'lib/hdo/storting_importer/representative.rb', line 99

def short_inspect
  short_inspect_string :include => [:external_id, :first_name, :last_name, :party, :vote_result]
end

#to_hashObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/hdo/storting_importer/representative.rb', line 107

def to_hash
  h = {
    'kind'        => self.class.kind,
    'externalId'  => @external_id,
    'firstName'   => @first_name,
    'lastName'    => @last_name,
    'gender'      => @gender,
    'dateOfBirth' => @date_of_birth,
    'dateOfDeath' => @date_of_death,
    'district'    => @district,
    'party'       => @party,
    'committees'  => @committees,
    'period'      => @period
  }

  h['voteResult'] = @vote_result if @vote_result

  h
end