Class: Hdo::StortingImporter::Vote

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

Defined Under Namespace

Classes: Counts

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, external_issue_id, personal, enacted, subject, method_name, result_type, time, for_count, against_count, absent_count, comment) ⇒ Vote

Returns a new instance of Vote.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/hdo/storting_importer/vote.rb', line 92

def initialize(external_id, external_issue_id, personal, enacted, subject, method_name, result_type, time, for_count, against_count, absent_count, comment)
  @external_id       = external_id
  @external_issue_id = external_issue_id
  @personal          = personal
  @enacted           = enacted
  @subject           = subject
  @method_name       = method_name
  @result_type       = result_type
  @time              = time
  @comment           = comment
  @counts            = Counts.new(Integer(for_count || 0), Integer(against_count || 0), Integer(absent_count || 0))

  @propositions    = []
  @representatives = []
end

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment.



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

def comment
  @comment
end

#countsObject (readonly)

Returns the value of attribute counts.



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

def counts
  @counts
end

#enactedObject (readonly) Also known as: enacted?

Returns the value of attribute enacted.



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

def enacted
  @enacted
end

#external_idObject (readonly)

Returns the value of attribute external_id.



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

def external_id
  @external_id
end

#external_issue_idObject (readonly)

Returns the value of attribute external_issue_id.



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

def external_issue_id
  @external_issue_id
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



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

def method_name
  @method_name
end

#personalObject (readonly) Also known as: personal?

Returns the value of attribute personal.



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

def personal
  @personal
end

#propositionsObject

Returns the value of attribute propositions.



12
13
14
# File 'lib/hdo/storting_importer/vote.rb', line 12

def propositions
  @propositions
end

#representativesObject

Returns the value of attribute representatives.



12
13
14
# File 'lib/hdo/storting_importer/vote.rb', line 12

def representatives
  @representatives
end

#result_typeObject (readonly)

Returns the value of attribute result_type.



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

def result_type
  @result_type
end

#subjectObject (readonly)

Returns the value of attribute subject.



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

def subject
  @subject
end

#timeObject (readonly)

Returns the value of attribute time.



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

def time
  @time
end

Class Method Details

.example(overrides = nil) ⇒ Object



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

def self.example(overrides = nil)
  vote = new('2175', '51448', true, false, 'Forslag 24 - 26 på vegne av Per Olaf Lundteigen', 'ikke_spesifisert', 'ikke_spesifisert', '2012-04-12T16:37:27.053', 2, 96, 71, 'hei')

  rep = Representative.example
  rep.vote_result = 'for'
  vote.representatives << rep

  prop = Proposition.example

  vote.propositions << prop

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

  vote
end

.from_hash(hash) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/hdo/storting_importer/vote.rb', line 67

def self.from_hash(hash)
  counts = hash['counts']

  args = [
    hash["external_id"],
    hash["external_issue_id"],
    hash["personal"],
    hash["enacted"],
    hash["subject"],
    hash["method"],
    hash["result_type"],
    hash["time"],
    counts && counts["for"],
    counts && counts["against"],
    counts && counts["absent"],
    hash["comment"],
  ]

  vote = new(*args)
  vote.representatives = Array(hash['representatives']).map { |e| Representative.from_hash(e || {}) }
  vote.propositions    = Array(hash['propositions']).map { |e| Proposition.from_hash(e || {}) }

  vote
end

.from_storting_doc(doc) ⇒ Object



41
42
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/vote.rb', line 41

def self.from_storting_doc(doc)
  issue_id = doc.css("sak_id").first.text

  doc.css("sak_votering").map do |vote_node|
    vote_id     = vote_node.css("votering_id").text
    personal    = vote_node.css("personlig_votering").text == "true"
    enacted     = vote_node.css("vedtatt").text == "true"
    subject     = vote_node.css("votering_tema").text
    method      = vote_node.css("votering_metode").text
    result_type = vote_node.css("votering_resultat_type").text
    time        = vote_node.css("votering_tid").text
    comment     = vote_node.css('kommentar').text

    forc     = Integer(vote_node.css("antall_for").text)
    againstc = Integer(vote_node.css("antall_mot").text)
    absentc  = Integer(vote_node.css("antall_ikke_tilstede").text)

    # settes til -1 ved personlig_votering=false
    forc = 0 if forc < 0
    againstc = 0 if againstc < 0
    absentc = 0 if absentc < 0

    new vote_id, issue_id, personal, enacted, subject, method, result_type, time, forc, againstc, absentc, comment
  end
end

.json_exampleObject



37
38
39
# File 'lib/hdo/storting_importer/vote.rb', line 37

def self.json_example
  Util.json_pretty example
end

Instance Method Details

#add_storting_propositions(node) ⇒ Object



116
117
118
# File 'lib/hdo/storting_importer/vote.rb', line 116

def add_storting_propositions(node)
  @propositions += Proposition.from_storting_doc(node, date)
end

#add_storting_results(node) ⇒ Object



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

def add_storting_results(node)
  @representatives += node.css("representant_voteringsresultat").map do |n|
    rep = Representative.from_storting_node(n.css("representant").first, date)
    rep.vote_result = case n.css("votering").text
                      when 'for'
                        'for'
                      when 'mot'
                       'against'
                      when 'ikke_tilstede'
                       'absent'
                      else
                        raise "unexpected vote: #{vote_result.inspect}"
                      end

    rep
  end
end

#dateObject



112
113
114
# File 'lib/hdo/storting_importer/vote.rb', line 112

def date
  @date ||= Date.parse(time)
end

#short_inspectObject



108
109
110
# File 'lib/hdo/storting_importer/vote.rb', line 108

def short_inspect
  short_inspect_string :include => [:external_id, :subject, :time, :counts]
end

#to_hashObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/hdo/storting_importer/vote.rb', line 138

def to_hash
  {
    'kind'              => self.class.kind,
    'external_id'       => @external_id,
    'external_issue_id' => @external_issue_id,
    'counts'            => @counts.to_hash,
    'personal'          => @personal,
    'enacted'           => @enacted,
    'subject'           => @subject,
    'method'            => @method_name,
    'result_type'       => @result_type,
    'time'              => @time,
    'comment'           => @comment,
    'representatives'   => @representatives.map(&:to_hash),
    'propositions'      => @propositions.map(&:to_hash)
  }
end