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, email, gender, date_of_birth, date_of_death, district, parties, committees) ⇒ Representative

Returns a new instance of Representative.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/hdo/storting_importer/representative.rb', line 120

def initialize(external_id, first_name, last_name, email, gender, date_of_birth, date_of_death, district, parties, committees)
  @external_id   = external_id
  @first_name    = first_name
  @last_name     = last_name
  @email         = email
  @gender        = gender
  @date_of_birth = date_of_birth
  @date_of_death = date_of_death
  @district      = district
  @parties       = parties
  @committees    = committees

  @vote_result              = nil
  @permanent_substitute_for = nil
  @substitute               = 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

#emailObject (readonly)

Returns the value of attribute email.



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

def email
  @email
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

#partiesObject (readonly)

Returns the value of attribute parties.



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

def parties
  @parties
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

#permanent_substitute_forObject

Returns the value of attribute permanent_substitute_for.



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

def permanent_substitute_for
  @permanent_substitute_for
end

#substituteObject

Returns the value of attribute substitute.



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

def substitute
  @substitute
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

.example(overrides = nil) ⇒ Object



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

def self.example(overrides = nil)
  obj = new(
    'ADA',
    'André Oktay',
    'Dahl',
    '[email protected]',
    'M',
    '1975-07-07T00:00:00',
    '0001-01-01T00:00:00',
    'Akershus',
    [PartyMembership.from_hash("external_id" => 'H', 'start_date' => '2011-10-01', 'end_date' => nil)],
    [CommitteeMembership.from_hash('external_id' => 'JUSTIS', 'start_date' => '2011-10-01', 'end_date' => nil)]
  )

  if overrides
    obj = from_hash(obj.to_hash.merge(overrides))
  end

  obj
end

.from_hash(hash) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/hdo/storting_importer/representative.rb', line 101

def self.from_hash(hash)
  v = new hash['external_id'],
          hash['first_name'],
          hash['last_name'],
          hash['email'],
          hash['gender'],
          hash['date_of_birth'],
          hash['date_of_death'],
          hash['district'],
          Array(hash['parties']).map { |e| PartyMembership.from_hash(e) },
          Array(hash['committees']).map { |e| CommitteeMembership.from_hash(e) }

  v.vote_result = hash['vote_result']
  v.permanent_substitute_for = hash['permanent_substitute_for']
  v.substitute = hash['substitute']

  v
end

.from_storting_doc(doc) ⇒ Object



42
43
44
45
46
47
# File 'lib/hdo/storting_importer/representative.rb', line 42

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, date = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/hdo/storting_importer/representative.rb', line 49

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

  start_date = date || Date.today
  end_date   = nil

  party_node = node.css("parti id").first
  if party_node
    parties = [ PartyMembership.new(party_node.text, start_date, end_date) ]
  else
    parties = []
  end

  email_node = node.css('epost').first
  if email_node
    email = email_node.text
  else
    email = nil
  end

  committee_ids = node.css("komite").map { |c| c.css("id").text.strip }
  committees = committee_ids.map do |id|
    CommitteeMembership.new(id, start_date, end_date)
  end

  rep = new(
    node.css("id").first.text,
    node.css("fornavn").first.text,
    node.css("etternavn").first.text,
    email.nil? || email.empty? ? nil : email,
    node.css("kjoenn").first.text == "mann" ? 'M' : 'F',
    node.css("foedselsdato").first.text,
    node.css("doedsdato").first.text,
    district,
    parties,
    committees
  )

  sub_for = node.css('fast_vara_for').first
  if sub_for && sub_for['nil'] != 'true'
    rep.permanent_substitute_for = sub_for.css("id").first.text
  end

  sub = node.css('vara').first
  if sub && sub['nil'] != 'true'
    rep.substitute = sub.css('id').first.text
  end

  rep
end

.json_exampleObject



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

def self.json_example
  Util.json_pretty example
end

Instance Method Details

#short_inspectObject



137
138
139
# File 'lib/hdo/storting_importer/representative.rb', line 137

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

#to_hashObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/hdo/storting_importer/representative.rb', line 145

def to_hash
  h = {
    'kind'          => self.class.kind,
    'external_id'   => @external_id,
    'first_name'    => @first_name,
    'last_name'     => @last_name,
    'email'         => @email,
    'gender'        => @gender,
    'date_of_birth' => @date_of_birth,
    'date_of_death' => @date_of_death,
    'district'      => @district,
    'parties'       => @parties.map { |e| e.to_hash },
    'committees'    => @committees.map { |e| e.to_hash }
  }

  h['vote_result'] = @vote_result if @vote_result
  h['permanent_substitute_for'] = @permanent_substitute_for if @permanent_substitute_for
  h['substitute'] = @substitute if @substitute

  h
end