pollster 0.2.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +1 -1
  3. data/README.md +68 -111
  4. data/example.rb +128 -0
  5. data/lib/pollster.rb +46 -4
  6. data/lib/pollster/api.rb +655 -0
  7. data/lib/pollster/api_client.rb +400 -0
  8. data/lib/pollster/api_error.rb +26 -0
  9. data/lib/pollster/configuration.rb +184 -0
  10. data/lib/pollster/models/chart.rb +239 -0
  11. data/lib/pollster/models/chart_estimate.rb +226 -0
  12. data/lib/pollster/models/chart_estimate_lowess_parameters.rb +197 -0
  13. data/lib/pollster/models/chart_pollster_estimate_summary.rb +217 -0
  14. data/lib/pollster/models/chart_pollster_trendlines.rb +41 -0
  15. data/lib/pollster/models/inline_response_200.rb +208 -0
  16. data/lib/pollster/models/inline_response_200_3.rb +206 -0
  17. data/lib/pollster/models/inline_response_200_4.rb +208 -0
  18. data/lib/pollster/models/poll.rb +279 -0
  19. data/lib/pollster/models/poll_question.rb +198 -0
  20. data/lib/pollster/models/poll_question_responses.rb +197 -0
  21. data/lib/pollster/models/poll_question_sample_subpopulations.rb +209 -0
  22. data/lib/pollster/models/pollster_chart_poll_questions.rb +92 -0
  23. data/lib/pollster/models/question.rb +253 -0
  24. data/lib/pollster/models/question_poll_responses_clean.rb +93 -0
  25. data/lib/pollster/models/question_poll_responses_raw.rb +86 -0
  26. data/lib/pollster/models/question_responses.rb +207 -0
  27. data/lib/pollster/models/tag.rb +207 -0
  28. data/lib/pollster/version.rb +1 -1
  29. data/pollster.gemspec +17 -16
  30. metadata +85 -65
  31. data/.gitignore +0 -2
  32. data/Gemfile +0 -3
  33. data/Gemfile.lock +0 -19
  34. data/Rakefile +0 -8
  35. data/lib/pollster/base.rb +0 -45
  36. data/lib/pollster/chart.rb +0 -69
  37. data/lib/pollster/poll.rb +0 -47
  38. data/lib/pollster/question.rb +0 -19
  39. data/test/test_pollster.rb +0 -26
@@ -0,0 +1,253 @@
1
+ require 'date'
2
+
3
+ module Pollster
4
+
5
+ class Question
6
+ # Unique Question identifier. For example: `obama-job-approval`. May contain spaces, symbols and capital letters.
7
+ attr_accessor :slug
8
+
9
+ # Gist of the question, phrased such that a journalist on the beat will understand what it represents. For example: `2016 West Virginia Governor: Cole vs. Justice`
10
+ attr_accessor :name
11
+
12
+ # All Tag slugs for Tags on this Question
13
+ attr_accessor :tags
14
+
15
+ # All Chart slugs for Charts built from this Question
16
+ attr_accessor :charts
17
+
18
+ # Date of the election pertaining to this Question, or `null` if the question does not pertain to an upcoming election
19
+ attr_accessor :election_date
20
+
21
+ # Number of Poll objects that ask this Question. Use the `polls` API endpoint with the `question` parameter for Poll details
22
+ attr_accessor :n_polls
23
+
24
+ # Date Pollster editors first created this Question
25
+ attr_accessor :created_at
26
+
27
+ # Responses Pollster editors decided upon. Individual polls have their own responses which often differ from these responses.
28
+ attr_accessor :responses
29
+
30
+
31
+ # Attribute mapping from ruby-style variable name to JSON key.
32
+ def self.attribute_map
33
+ {
34
+ :'slug' => :'slug',
35
+ :'name' => :'name',
36
+ :'tags' => :'tags',
37
+ :'charts' => :'charts',
38
+ :'election_date' => :'election_date',
39
+ :'n_polls' => :'n_polls',
40
+ :'created_at' => :'created_at',
41
+ :'responses' => :'responses'
42
+ }
43
+ end
44
+
45
+ # Attribute type mapping.
46
+ def self.swagger_types
47
+ {
48
+ :'slug' => :'String',
49
+ :'name' => :'String',
50
+ :'tags' => :'Array<String>',
51
+ :'charts' => :'Array<String>',
52
+ :'election_date' => :'Date',
53
+ :'n_polls' => :'Integer',
54
+ :'created_at' => :'DateTime',
55
+ :'responses' => :'Array<QuestionResponses>'
56
+ }
57
+ end
58
+
59
+ # Initializes the object
60
+ # @param [Hash] attributes Model attributes in the form of hash
61
+ def initialize(attributes = {})
62
+ return unless attributes.is_a?(Hash)
63
+
64
+ # convert string to symbol for hash key
65
+ attributes = attributes.each_with_object({}){|(k,v), h| h[k.to_sym] = v}
66
+
67
+ if attributes.has_key?(:'slug')
68
+ self.slug = attributes[:'slug']
69
+ end
70
+
71
+ if attributes.has_key?(:'name')
72
+ self.name = attributes[:'name']
73
+ end
74
+
75
+ if attributes.has_key?(:'tags')
76
+ if (value = attributes[:'tags']).is_a?(Array)
77
+ self.tags = value
78
+ end
79
+ end
80
+
81
+ if attributes.has_key?(:'charts')
82
+ if (value = attributes[:'charts']).is_a?(Array)
83
+ self.charts = value
84
+ end
85
+ end
86
+
87
+ if attributes.has_key?(:'election_date')
88
+ self.election_date = attributes[:'election_date']
89
+ end
90
+
91
+ if attributes.has_key?(:'n_polls')
92
+ self.n_polls = attributes[:'n_polls']
93
+ end
94
+
95
+ if attributes.has_key?(:'created_at')
96
+ self.created_at = attributes[:'created_at']
97
+ end
98
+
99
+ if attributes.has_key?(:'responses')
100
+ if (value = attributes[:'responses']).is_a?(Array)
101
+ self.responses = value
102
+ end
103
+ end
104
+
105
+ end
106
+
107
+ # Show invalid properties with the reasons. Usually used together with valid?
108
+ # @return Array for valid properies with the reasons
109
+ def list_invalid_properties
110
+ invalid_properties = Array.new
111
+ return invalid_properties
112
+ end
113
+
114
+ # Check to see if the all the properties in the model are valid
115
+ # @return true if the model is valid
116
+ def valid?
117
+ return true
118
+ end
119
+
120
+ # Checks equality by comparing each attribute.
121
+ # @param [Object] Object to be compared
122
+ def ==(o)
123
+ return true if self.equal?(o)
124
+ self.class == o.class &&
125
+ slug == o.slug &&
126
+ name == o.name &&
127
+ tags == o.tags &&
128
+ charts == o.charts &&
129
+ election_date == o.election_date &&
130
+ n_polls == o.n_polls &&
131
+ created_at == o.created_at &&
132
+ responses == o.responses
133
+ end
134
+
135
+ # @see the `==` method
136
+ # @param [Object] Object to be compared
137
+ def eql?(o)
138
+ self == o
139
+ end
140
+
141
+ # Calculates hash code according to all attributes.
142
+ # @return [Fixnum] Hash code
143
+ def hash
144
+ [slug, name, tags, charts, election_date, n_polls, created_at, responses].hash
145
+ end
146
+
147
+ # Builds the object from hash
148
+ # @param [Hash] attributes Model attributes in the form of hash
149
+ # @return [Object] Returns the model itself
150
+ def build_from_hash(attributes)
151
+ return nil unless attributes.is_a?(Hash)
152
+ self.class.swagger_types.each_pair do |key, type|
153
+ if type =~ /\AArray<(.*)>/i
154
+ # check to ensure the input is an array given that the the attribute
155
+ # is documented as an array but the input is not
156
+ if attributes[self.class.attribute_map[key]].is_a?(Array)
157
+ self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } )
158
+ end
159
+ elsif !attributes[self.class.attribute_map[key]].nil?
160
+ self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
161
+ end # or else data not found in attributes(hash), not an issue as the data can be optional
162
+ end
163
+
164
+ self
165
+ end
166
+
167
+ # Deserializes the data based on type
168
+ # @param string type Data type
169
+ # @param string value Value to be deserialized
170
+ # @return [Object] Deserialized data
171
+ def _deserialize(type, value)
172
+ case type.to_sym
173
+ when :DateTime
174
+ DateTime.parse(value)
175
+ when :Date
176
+ Date.parse(value)
177
+ when :String
178
+ value.to_s
179
+ when :Integer
180
+ value.to_i
181
+ when :Float
182
+ value.to_f
183
+ when :BOOLEAN
184
+ if value.to_s =~ /\A(true|t|yes|y|1)\z/i
185
+ true
186
+ else
187
+ false
188
+ end
189
+ when :Object
190
+ # generic object (usually a Hash), return directly
191
+ value
192
+ when /\AArray<(?<inner_type>.+)>\z/
193
+ inner_type = Regexp.last_match[:inner_type]
194
+ value.map { |v| _deserialize(inner_type, v) }
195
+ when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
196
+ k_type = Regexp.last_match[:k_type]
197
+ v_type = Regexp.last_match[:v_type]
198
+ {}.tap do |hash|
199
+ value.each do |k, v|
200
+ hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
201
+ end
202
+ end
203
+ else # model
204
+ temp_model = Pollster.const_get(type).new
205
+ temp_model.build_from_hash(value)
206
+ end
207
+ end
208
+
209
+ # Returns the string representation of the object
210
+ # @return [String] String presentation of the object
211
+ def to_s
212
+ to_hash.to_s
213
+ end
214
+
215
+ # to_body is an alias to to_hash (backward compatibility)
216
+ # @return [Hash] Returns the object in the form of hash
217
+ def to_body
218
+ to_hash
219
+ end
220
+
221
+ # Returns the object in the form of hash
222
+ # @return [Hash] Returns the object in the form of hash
223
+ def to_hash
224
+ hash = {}
225
+ self.class.attribute_map.each_pair do |attr, param|
226
+ value = self.send(attr)
227
+ next if value.nil?
228
+ hash[param] = _to_hash(value)
229
+ end
230
+ hash
231
+ end
232
+
233
+ # Outputs non-array value in the form of hash
234
+ # For object, use to_hash. Otherwise, just return the value
235
+ # @param [Object] value Any valid value
236
+ # @return [Hash] Returns the value in the form of hash
237
+ def _to_hash(value)
238
+ if value.is_a?(Array)
239
+ value.compact.map{ |v| _to_hash(v) }
240
+ elsif value.is_a?(Hash)
241
+ {}.tap do |hash|
242
+ value.each { |k, v| hash[k] = _to_hash(v) }
243
+ end
244
+ elsif value.respond_to? :to_hash
245
+ value.to_hash
246
+ else
247
+ value
248
+ end
249
+ end
250
+
251
+ end
252
+
253
+ end
@@ -0,0 +1,93 @@
1
+ require 'date'
2
+ require 'ruby-immutable-struct'
3
+
4
+ module Pollster
5
+ QuestionPollResponseClean = RubyImmutableStruct.new(
6
+ :responses,
7
+ :poll_slug,
8
+ :survey_house,
9
+ :start_date,
10
+ :end_date,
11
+ :question_text,
12
+ :sample_subpopulation,
13
+ :observations,
14
+ :margin_of_error,
15
+ :mode,
16
+ :partisanship,
17
+ :partisan_affiliation
18
+ ) do
19
+ def self.from_tsv_line_with_labels(tsv_line, labels)
20
+ if m = QuestionPollResponseClean::TsvLineRegex.match(tsv_line)
21
+ # split(..., -1) avoids omitting trailing blanks
22
+ values = m[:values].split(/\t/, -1).map{ |v| v.empty? ? nil : v.to_f }
23
+
24
+ if values.length != labels.length
25
+ raise ArgumentError.new("TSV line `#{tsv_line}` has #{values.length} values for #{labels.length} labels")
26
+ end
27
+
28
+ responses = {}
29
+ for i in 0...(labels.length)
30
+ responses[labels[i]] = values[i]
31
+ end
32
+
33
+ QuestionPollResponseClean.new(
34
+ responses,
35
+ m[:poll_slug],
36
+ m[:survey_house],
37
+ Date.parse(m[:start_date]),
38
+ Date.parse(m[:end_date]),
39
+ m[:question_text],
40
+ m[:sample_subpopulation],
41
+ m[:observations].empty? ? nil : m[:observations].to_i,
42
+ m[:margin_of_error].empty? ? nil : m[:margin_of_error].to_f,
43
+ m[:mode],
44
+ m[:partisanship],
45
+ m[:partisan_affiliation]
46
+ )
47
+ else
48
+ raise ArgumentError.new("TSV line `#{tsv_line}` is not a valid QuestionPollResponseClean line")
49
+ end
50
+ end
51
+ end
52
+ QuestionPollResponseClean::TsvLineRegex = %r{
53
+ \A
54
+ (?<values>(?:\d+(?:\.\d+)?)?(?:\t(?:\d+(?:\.\d+)?)?)*)\t
55
+ (?<poll_slug>[^\t]+)\t
56
+ (?<survey_house>[^\t]+)\t
57
+ (?<start_date>\d\d\d\d-\d\d-\d\d)\t
58
+ (?<end_date>\d\d\d\d-\d\d-\d\d)\t
59
+ (?<question_text>[^\t]*)\t
60
+ (?<sample_subpopulation>[^\t]+)\t
61
+ (?<observations>\d*)\t
62
+ (?<margin_of_error>(?:\d+(?:\.\d+)?)?)\t
63
+ (?<mode>[^\t]+)\t
64
+ (?<partisanship>[^\t]+)\t
65
+ (?<partisan_affiliation>[^\t]+)\b
66
+ # No \Z ... Pollster's API doesn't guarantee it won't add columns
67
+ }x
68
+
69
+ class QuestionPollResponsesClean
70
+ HeaderLineRegex = /\A(?<labels>[^\t]+(?:\t[^\t]+)*)\tpoll_slug\tsurvey_house\tstart_date\tend_date\tquestion_text\tsample_subpopulation\tobservations\tmargin_of_error\tmode\tpartisanship\tpartisan_affiliation\b/
71
+
72
+ include Enumerable
73
+
74
+ def initialize(array)
75
+ @array = array
76
+ end
77
+
78
+ def each(&block)
79
+ @array.each(&block)
80
+ end
81
+
82
+ def self.from_tsv(tsv)
83
+ lines = tsv.split(/(?:\r?\n)+/).reject(&:empty?)
84
+ if m = HeaderLineRegex.match(lines[0])
85
+ labels = m[:labels].split(/\t/)
86
+ rows = lines[1..-1].map { |tsv_line| QuestionPollResponseClean.from_tsv_line_with_labels(tsv_line, labels) }
87
+ QuestionPollResponsesClean.new(rows)
88
+ else
89
+ raise ArgumentError.new("First line of TSV is `#{lines[0]}`, which does not match Pollster::QuestionPollResponsesClean::HeaderLineRegex")
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,86 @@
1
+ require 'date'
2
+ require 'ruby-immutable-struct'
3
+
4
+ module Pollster
5
+ QuestionPollResponseRaw = RubyImmutableStruct.new(
6
+ :response_text,
7
+ :pollster_label,
8
+ :value,
9
+ :poll_slug,
10
+ :survey_house,
11
+ :start_date,
12
+ :end_date,
13
+ :question_text,
14
+ :sample_subpopulation,
15
+ :observations,
16
+ :margin_of_error,
17
+ :mode,
18
+ :partisanship,
19
+ :partisan_affiliation
20
+ ) do
21
+ def self.from_tsv_line(tsv_line)
22
+ if m = QuestionPollResponseRaw::TsvLineRegex.match(tsv_line)
23
+ QuestionPollResponseRaw.new(
24
+ m[:response_text],
25
+ m[:pollster_label],
26
+ m[:value].to_f,
27
+ m[:poll_slug],
28
+ m[:survey_house],
29
+ Date.parse(m[:start_date]),
30
+ Date.parse(m[:end_date]),
31
+ m[:question_text],
32
+ m[:sample_subpopulation],
33
+ m[:observations].empty? ? nil : m[:observations].to_i,
34
+ m[:margin_of_error].empty? ? nil : m[:margin_of_error].to_f,
35
+ m[:mode],
36
+ m[:partisanship],
37
+ m[:partisan_affiliation]
38
+ )
39
+ else
40
+ raise ArgumentError.new("TSV line `#{tsv_line}` is not a valid QuestionPollResponseRaw line")
41
+ end
42
+ end
43
+ end
44
+ QuestionPollResponseRaw::TsvLineRegex = %r{
45
+ \A
46
+ (?<response_text>[^\t]+)\t
47
+ (?<pollster_label>[^\t]*)\t
48
+ (?<value>\d+(?:\.\d+)?)\t
49
+ (?<poll_slug>[^\t]+)\t
50
+ (?<survey_house>[^\t]+)\t
51
+ (?<start_date>\d\d\d\d-\d\d-\d\d)\t
52
+ (?<end_date>\d\d\d\d-\d\d-\d\d)\t
53
+ (?<question_text>[^\t]*)\t
54
+ (?<sample_subpopulation>[^\t]+)\t
55
+ (?<observations>\d*)\t
56
+ (?<margin_of_error>(?:\d+(?:\.\d+)?)?)\t
57
+ (?<mode>[^\t]+)\t
58
+ (?<partisanship>[^\t]+)\t
59
+ (?<partisan_affiliation>[^\t]+)\b
60
+ # No \Z ... Pollster's API doesn't guarantee it won't add columns
61
+ }x
62
+
63
+ class QuestionPollResponsesRaw
64
+ HeaderLineRegex = /\Aresponse_text\tpollster_label\tvalue\tpoll_slug\tsurvey_house\tstart_date\tend_date\tquestion_text\tsample_subpopulation\tobservations\tmargin_of_error\tmode\tpartisanship\tpartisan_affiliation\b/
65
+
66
+ include Enumerable
67
+
68
+ def initialize(array)
69
+ @array = array
70
+ end
71
+
72
+ def each(&block)
73
+ @array.each(&block)
74
+ end
75
+
76
+ def self.from_tsv(tsv)
77
+ lines = tsv.split(/(?:\r?\n)+/).reject(&:empty?)
78
+ if m = HeaderLineRegex.match(lines[0])
79
+ rows = lines[1..-1].map { |tsv_line| QuestionPollResponseRaw.from_tsv_line(tsv_line) }
80
+ QuestionPollResponsesRaw.new(rows)
81
+ else
82
+ raise ArgumentError.new("First line of TSV is `#{lines[0]}`, which does not match Pollster::QuestionPollResponsesRaw::HeaderLineRegex")
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,207 @@
1
+ require 'date'
2
+
3
+ module Pollster
4
+
5
+ class QuestionResponses
6
+ # More verbose description of the option, also unique to this Question. For people, this is usually a full name (e.g., `Hillary Clinton`); other response names are chosen on a case-by-case basis (e.g., `Other` or `Remain in the E.U.`)
7
+ attr_accessor :name
8
+
9
+ # Identifier for this response, succinct and unique to this Question. For example, `Clinton`. The canonical use case: the label is what one would write on a chart.
10
+ attr_accessor :label
11
+
12
+ # If this response is a candidate running for office, the name of the candidate's registered political party (e.g., `Republican`); otherwise `null`
13
+ attr_accessor :party
14
+
15
+ # If this response is a candidate running for re-election to the same office, `true`; otherwise `false`.
16
+ attr_accessor :incumbent
17
+
18
+
19
+ # Attribute mapping from ruby-style variable name to JSON key.
20
+ def self.attribute_map
21
+ {
22
+ :'name' => :'name',
23
+ :'label' => :'label',
24
+ :'party' => :'party',
25
+ :'incumbent' => :'incumbent'
26
+ }
27
+ end
28
+
29
+ # Attribute type mapping.
30
+ def self.swagger_types
31
+ {
32
+ :'name' => :'String',
33
+ :'label' => :'String',
34
+ :'party' => :'String',
35
+ :'incumbent' => :'BOOLEAN'
36
+ }
37
+ end
38
+
39
+ # Initializes the object
40
+ # @param [Hash] attributes Model attributes in the form of hash
41
+ def initialize(attributes = {})
42
+ return unless attributes.is_a?(Hash)
43
+
44
+ # convert string to symbol for hash key
45
+ attributes = attributes.each_with_object({}){|(k,v), h| h[k.to_sym] = v}
46
+
47
+ if attributes.has_key?(:'name')
48
+ self.name = attributes[:'name']
49
+ end
50
+
51
+ if attributes.has_key?(:'label')
52
+ self.label = attributes[:'label']
53
+ end
54
+
55
+ if attributes.has_key?(:'party')
56
+ self.party = attributes[:'party']
57
+ end
58
+
59
+ if attributes.has_key?(:'incumbent')
60
+ self.incumbent = attributes[:'incumbent']
61
+ end
62
+
63
+ end
64
+
65
+ # Show invalid properties with the reasons. Usually used together with valid?
66
+ # @return Array for valid properies with the reasons
67
+ def list_invalid_properties
68
+ invalid_properties = Array.new
69
+ return invalid_properties
70
+ end
71
+
72
+ # Check to see if the all the properties in the model are valid
73
+ # @return true if the model is valid
74
+ def valid?
75
+ return true
76
+ end
77
+
78
+ # Checks equality by comparing each attribute.
79
+ # @param [Object] Object to be compared
80
+ def ==(o)
81
+ return true if self.equal?(o)
82
+ self.class == o.class &&
83
+ name == o.name &&
84
+ label == o.label &&
85
+ party == o.party &&
86
+ incumbent == o.incumbent
87
+ end
88
+
89
+ # @see the `==` method
90
+ # @param [Object] Object to be compared
91
+ def eql?(o)
92
+ self == o
93
+ end
94
+
95
+ # Calculates hash code according to all attributes.
96
+ # @return [Fixnum] Hash code
97
+ def hash
98
+ [name, label, party, incumbent].hash
99
+ end
100
+
101
+ # Builds the object from hash
102
+ # @param [Hash] attributes Model attributes in the form of hash
103
+ # @return [Object] Returns the model itself
104
+ def build_from_hash(attributes)
105
+ return nil unless attributes.is_a?(Hash)
106
+ self.class.swagger_types.each_pair do |key, type|
107
+ if type =~ /\AArray<(.*)>/i
108
+ # check to ensure the input is an array given that the the attribute
109
+ # is documented as an array but the input is not
110
+ if attributes[self.class.attribute_map[key]].is_a?(Array)
111
+ self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } )
112
+ end
113
+ elsif !attributes[self.class.attribute_map[key]].nil?
114
+ self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
115
+ end # or else data not found in attributes(hash), not an issue as the data can be optional
116
+ end
117
+
118
+ self
119
+ end
120
+
121
+ # Deserializes the data based on type
122
+ # @param string type Data type
123
+ # @param string value Value to be deserialized
124
+ # @return [Object] Deserialized data
125
+ def _deserialize(type, value)
126
+ case type.to_sym
127
+ when :DateTime
128
+ DateTime.parse(value)
129
+ when :Date
130
+ Date.parse(value)
131
+ when :String
132
+ value.to_s
133
+ when :Integer
134
+ value.to_i
135
+ when :Float
136
+ value.to_f
137
+ when :BOOLEAN
138
+ if value.to_s =~ /\A(true|t|yes|y|1)\z/i
139
+ true
140
+ else
141
+ false
142
+ end
143
+ when :Object
144
+ # generic object (usually a Hash), return directly
145
+ value
146
+ when /\AArray<(?<inner_type>.+)>\z/
147
+ inner_type = Regexp.last_match[:inner_type]
148
+ value.map { |v| _deserialize(inner_type, v) }
149
+ when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
150
+ k_type = Regexp.last_match[:k_type]
151
+ v_type = Regexp.last_match[:v_type]
152
+ {}.tap do |hash|
153
+ value.each do |k, v|
154
+ hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
155
+ end
156
+ end
157
+ else # model
158
+ temp_model = Pollster.const_get(type).new
159
+ temp_model.build_from_hash(value)
160
+ end
161
+ end
162
+
163
+ # Returns the string representation of the object
164
+ # @return [String] String presentation of the object
165
+ def to_s
166
+ to_hash.to_s
167
+ end
168
+
169
+ # to_body is an alias to to_hash (backward compatibility)
170
+ # @return [Hash] Returns the object in the form of hash
171
+ def to_body
172
+ to_hash
173
+ end
174
+
175
+ # Returns the object in the form of hash
176
+ # @return [Hash] Returns the object in the form of hash
177
+ def to_hash
178
+ hash = {}
179
+ self.class.attribute_map.each_pair do |attr, param|
180
+ value = self.send(attr)
181
+ next if value.nil?
182
+ hash[param] = _to_hash(value)
183
+ end
184
+ hash
185
+ end
186
+
187
+ # Outputs non-array value in the form of hash
188
+ # For object, use to_hash. Otherwise, just return the value
189
+ # @param [Object] value Any valid value
190
+ # @return [Hash] Returns the value in the form of hash
191
+ def _to_hash(value)
192
+ if value.is_a?(Array)
193
+ value.compact.map{ |v| _to_hash(v) }
194
+ elsif value.is_a?(Hash)
195
+ {}.tap do |hash|
196
+ value.each { |k, v| hash[k] = _to_hash(v) }
197
+ end
198
+ elsif value.respond_to? :to_hash
199
+ value.to_hash
200
+ else
201
+ value
202
+ end
203
+ end
204
+
205
+ end
206
+
207
+ end