pollster 0.2.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
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,197 @@
1
+ require 'date'
2
+
3
+ module Pollster
4
+ # Only present if `algorithm` is `lowess`. Shows [parameters](https://stat.ethz.ch/R-manual/R-devel/library/stats/html/lowess.html) to lowess.
5
+ class ChartEstimateLowessParameters
6
+ # `f` lowess parameter
7
+ attr_accessor :f
8
+
9
+ # `delta` lowess parameter
10
+ attr_accessor :delta
11
+
12
+ # `iter` lowess parameter
13
+ attr_accessor :iter
14
+
15
+
16
+ # Attribute mapping from ruby-style variable name to JSON key.
17
+ def self.attribute_map
18
+ {
19
+ :'f' => :'f',
20
+ :'delta' => :'delta',
21
+ :'iter' => :'iter'
22
+ }
23
+ end
24
+
25
+ # Attribute type mapping.
26
+ def self.swagger_types
27
+ {
28
+ :'f' => :'Float',
29
+ :'delta' => :'Float',
30
+ :'iter' => :'Integer'
31
+ }
32
+ end
33
+
34
+ # Initializes the object
35
+ # @param [Hash] attributes Model attributes in the form of hash
36
+ def initialize(attributes = {})
37
+ return unless attributes.is_a?(Hash)
38
+
39
+ # convert string to symbol for hash key
40
+ attributes = attributes.each_with_object({}){|(k,v), h| h[k.to_sym] = v}
41
+
42
+ if attributes.has_key?(:'f')
43
+ self.f = attributes[:'f']
44
+ end
45
+
46
+ if attributes.has_key?(:'delta')
47
+ self.delta = attributes[:'delta']
48
+ end
49
+
50
+ if attributes.has_key?(:'iter')
51
+ self.iter = attributes[:'iter']
52
+ end
53
+
54
+ end
55
+
56
+ # Show invalid properties with the reasons. Usually used together with valid?
57
+ # @return Array for valid properies with the reasons
58
+ def list_invalid_properties
59
+ invalid_properties = Array.new
60
+ return invalid_properties
61
+ end
62
+
63
+ # Check to see if the all the properties in the model are valid
64
+ # @return true if the model is valid
65
+ def valid?
66
+ return true
67
+ end
68
+
69
+ # Checks equality by comparing each attribute.
70
+ # @param [Object] Object to be compared
71
+ def ==(o)
72
+ return true if self.equal?(o)
73
+ self.class == o.class &&
74
+ f == o.f &&
75
+ delta == o.delta &&
76
+ iter == o.iter
77
+ end
78
+
79
+ # @see the `==` method
80
+ # @param [Object] Object to be compared
81
+ def eql?(o)
82
+ self == o
83
+ end
84
+
85
+ # Calculates hash code according to all attributes.
86
+ # @return [Fixnum] Hash code
87
+ def hash
88
+ [f, delta, iter].hash
89
+ end
90
+
91
+ # Builds the object from hash
92
+ # @param [Hash] attributes Model attributes in the form of hash
93
+ # @return [Object] Returns the model itself
94
+ def build_from_hash(attributes)
95
+ return nil unless attributes.is_a?(Hash)
96
+ self.class.swagger_types.each_pair do |key, type|
97
+ if type =~ /\AArray<(.*)>/i
98
+ # check to ensure the input is an array given that the the attribute
99
+ # is documented as an array but the input is not
100
+ if attributes[self.class.attribute_map[key]].is_a?(Array)
101
+ self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } )
102
+ end
103
+ elsif !attributes[self.class.attribute_map[key]].nil?
104
+ self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
105
+ end # or else data not found in attributes(hash), not an issue as the data can be optional
106
+ end
107
+
108
+ self
109
+ end
110
+
111
+ # Deserializes the data based on type
112
+ # @param string type Data type
113
+ # @param string value Value to be deserialized
114
+ # @return [Object] Deserialized data
115
+ def _deserialize(type, value)
116
+ case type.to_sym
117
+ when :DateTime
118
+ DateTime.parse(value)
119
+ when :Date
120
+ Date.parse(value)
121
+ when :String
122
+ value.to_s
123
+ when :Integer
124
+ value.to_i
125
+ when :Float
126
+ value.to_f
127
+ when :BOOLEAN
128
+ if value.to_s =~ /\A(true|t|yes|y|1)\z/i
129
+ true
130
+ else
131
+ false
132
+ end
133
+ when :Object
134
+ # generic object (usually a Hash), return directly
135
+ value
136
+ when /\AArray<(?<inner_type>.+)>\z/
137
+ inner_type = Regexp.last_match[:inner_type]
138
+ value.map { |v| _deserialize(inner_type, v) }
139
+ when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
140
+ k_type = Regexp.last_match[:k_type]
141
+ v_type = Regexp.last_match[:v_type]
142
+ {}.tap do |hash|
143
+ value.each do |k, v|
144
+ hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
145
+ end
146
+ end
147
+ else # model
148
+ temp_model = Pollster.const_get(type).new
149
+ temp_model.build_from_hash(value)
150
+ end
151
+ end
152
+
153
+ # Returns the string representation of the object
154
+ # @return [String] String presentation of the object
155
+ def to_s
156
+ to_hash.to_s
157
+ end
158
+
159
+ # to_body is an alias to to_hash (backward compatibility)
160
+ # @return [Hash] Returns the object in the form of hash
161
+ def to_body
162
+ to_hash
163
+ end
164
+
165
+ # Returns the object in the form of hash
166
+ # @return [Hash] Returns the object in the form of hash
167
+ def to_hash
168
+ hash = {}
169
+ self.class.attribute_map.each_pair do |attr, param|
170
+ value = self.send(attr)
171
+ next if value.nil?
172
+ hash[param] = _to_hash(value)
173
+ end
174
+ hash
175
+ end
176
+
177
+ # Outputs non-array value in the form of hash
178
+ # For object, use to_hash. Otherwise, just return the value
179
+ # @param [Object] value Any valid value
180
+ # @return [Hash] Returns the value in the form of hash
181
+ def _to_hash(value)
182
+ if value.is_a?(Array)
183
+ value.compact.map{ |v| _to_hash(v) }
184
+ elsif value.is_a?(Hash)
185
+ {}.tap do |hash|
186
+ value.each { |k, v| hash[k] = _to_hash(v) }
187
+ end
188
+ elsif value.respond_to? :to_hash
189
+ value.to_hash
190
+ else
191
+ value
192
+ end
193
+ end
194
+
195
+ end
196
+
197
+ end
@@ -0,0 +1,217 @@
1
+ require 'date'
2
+
3
+ module Pollster
4
+ # Pollster-generated summary of the chart. Pollster uses this to generate a sentence on the chart page such as, \"The polls suggest X leads Y, with a likelihood of Z%.\" If Pollster does not generate a chart summary, this value will be `null`.
5
+ class ChartPollsterEstimateSummary
6
+ # Response label
7
+ attr_accessor :behind
8
+
9
+ # `chart.pollster_estimates[0].datetime`
10
+ attr_accessor :datetime
11
+
12
+ # Response label
13
+ attr_accessor :ahead
14
+
15
+ # Probability (between 0.5 and 1) that the response label `ahead` is ahead of `behind`
16
+ attr_accessor :probability
17
+
18
+ # `chart.pollster_estimates[0].created_at`
19
+ attr_accessor :created_at
20
+
21
+
22
+ # Attribute mapping from ruby-style variable name to JSON key.
23
+ def self.attribute_map
24
+ {
25
+ :'behind' => :'behind',
26
+ :'datetime' => :'datetime',
27
+ :'ahead' => :'ahead',
28
+ :'probability' => :'probability',
29
+ :'created_at' => :'created_at'
30
+ }
31
+ end
32
+
33
+ # Attribute type mapping.
34
+ def self.swagger_types
35
+ {
36
+ :'behind' => :'String',
37
+ :'datetime' => :'DateTime',
38
+ :'ahead' => :'String',
39
+ :'probability' => :'Float',
40
+ :'created_at' => :'DateTime'
41
+ }
42
+ end
43
+
44
+ # Initializes the object
45
+ # @param [Hash] attributes Model attributes in the form of hash
46
+ def initialize(attributes = {})
47
+ return unless attributes.is_a?(Hash)
48
+
49
+ # convert string to symbol for hash key
50
+ attributes = attributes.each_with_object({}){|(k,v), h| h[k.to_sym] = v}
51
+
52
+ if attributes.has_key?(:'behind')
53
+ self.behind = attributes[:'behind']
54
+ end
55
+
56
+ if attributes.has_key?(:'datetime')
57
+ self.datetime = attributes[:'datetime']
58
+ end
59
+
60
+ if attributes.has_key?(:'ahead')
61
+ self.ahead = attributes[:'ahead']
62
+ end
63
+
64
+ if attributes.has_key?(:'probability')
65
+ self.probability = attributes[:'probability']
66
+ end
67
+
68
+ if attributes.has_key?(:'created_at')
69
+ self.created_at = attributes[:'created_at']
70
+ end
71
+
72
+ end
73
+
74
+ # Show invalid properties with the reasons. Usually used together with valid?
75
+ # @return Array for valid properies with the reasons
76
+ def list_invalid_properties
77
+ invalid_properties = Array.new
78
+ return invalid_properties
79
+ end
80
+
81
+ # Check to see if the all the properties in the model are valid
82
+ # @return true if the model is valid
83
+ def valid?
84
+ return true
85
+ end
86
+
87
+ # Checks equality by comparing each attribute.
88
+ # @param [Object] Object to be compared
89
+ def ==(o)
90
+ return true if self.equal?(o)
91
+ self.class == o.class &&
92
+ behind == o.behind &&
93
+ datetime == o.datetime &&
94
+ ahead == o.ahead &&
95
+ probability == o.probability &&
96
+ created_at == o.created_at
97
+ end
98
+
99
+ # @see the `==` method
100
+ # @param [Object] Object to be compared
101
+ def eql?(o)
102
+ self == o
103
+ end
104
+
105
+ # Calculates hash code according to all attributes.
106
+ # @return [Fixnum] Hash code
107
+ def hash
108
+ [behind, datetime, ahead, probability, created_at].hash
109
+ end
110
+
111
+ # Builds the object from hash
112
+ # @param [Hash] attributes Model attributes in the form of hash
113
+ # @return [Object] Returns the model itself
114
+ def build_from_hash(attributes)
115
+ return nil unless attributes.is_a?(Hash)
116
+ self.class.swagger_types.each_pair do |key, type|
117
+ if type =~ /\AArray<(.*)>/i
118
+ # check to ensure the input is an array given that the the attribute
119
+ # is documented as an array but the input is not
120
+ if attributes[self.class.attribute_map[key]].is_a?(Array)
121
+ self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } )
122
+ end
123
+ elsif !attributes[self.class.attribute_map[key]].nil?
124
+ self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
125
+ end # or else data not found in attributes(hash), not an issue as the data can be optional
126
+ end
127
+
128
+ self
129
+ end
130
+
131
+ # Deserializes the data based on type
132
+ # @param string type Data type
133
+ # @param string value Value to be deserialized
134
+ # @return [Object] Deserialized data
135
+ def _deserialize(type, value)
136
+ case type.to_sym
137
+ when :DateTime
138
+ DateTime.parse(value)
139
+ when :Date
140
+ Date.parse(value)
141
+ when :String
142
+ value.to_s
143
+ when :Integer
144
+ value.to_i
145
+ when :Float
146
+ value.to_f
147
+ when :BOOLEAN
148
+ if value.to_s =~ /\A(true|t|yes|y|1)\z/i
149
+ true
150
+ else
151
+ false
152
+ end
153
+ when :Object
154
+ # generic object (usually a Hash), return directly
155
+ value
156
+ when /\AArray<(?<inner_type>.+)>\z/
157
+ inner_type = Regexp.last_match[:inner_type]
158
+ value.map { |v| _deserialize(inner_type, v) }
159
+ when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
160
+ k_type = Regexp.last_match[:k_type]
161
+ v_type = Regexp.last_match[:v_type]
162
+ {}.tap do |hash|
163
+ value.each do |k, v|
164
+ hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
165
+ end
166
+ end
167
+ else # model
168
+ temp_model = Pollster.const_get(type).new
169
+ temp_model.build_from_hash(value)
170
+ end
171
+ end
172
+
173
+ # Returns the string representation of the object
174
+ # @return [String] String presentation of the object
175
+ def to_s
176
+ to_hash.to_s
177
+ end
178
+
179
+ # to_body is an alias to to_hash (backward compatibility)
180
+ # @return [Hash] Returns the object in the form of hash
181
+ def to_body
182
+ to_hash
183
+ end
184
+
185
+ # Returns the object in the form of hash
186
+ # @return [Hash] Returns the object in the form of hash
187
+ def to_hash
188
+ hash = {}
189
+ self.class.attribute_map.each_pair do |attr, param|
190
+ value = self.send(attr)
191
+ next if value.nil?
192
+ hash[param] = _to_hash(value)
193
+ end
194
+ hash
195
+ end
196
+
197
+ # Outputs non-array value in the form of hash
198
+ # For object, use to_hash. Otherwise, just return the value
199
+ # @param [Object] value Any valid value
200
+ # @return [Hash] Returns the value in the form of hash
201
+ def _to_hash(value)
202
+ if value.is_a?(Array)
203
+ value.compact.map{ |v| _to_hash(v) }
204
+ elsif value.is_a?(Hash)
205
+ {}.tap do |hash|
206
+ value.each { |k, v| hash[k] = _to_hash(v) }
207
+ end
208
+ elsif value.respond_to? :to_hash
209
+ value.to_hash
210
+ else
211
+ value
212
+ end
213
+ end
214
+
215
+ end
216
+
217
+ end
@@ -0,0 +1,41 @@
1
+ require 'date'
2
+ require 'ruby-immutable-struct'
3
+
4
+ module Pollster
5
+ ChartPollsterTrendlinePoint = RubyImmutableStruct.new(:label, :date, :value, :low, :high) do
6
+ def self.from_tsv_line(tsv_line)
7
+ if m = ChartPollsterTrendlinePoint::TsvLineRegex.match(tsv_line)
8
+ ChartPollsterTrendlinePoint.new(
9
+ m[:label],
10
+ Date.parse(m[:date]),
11
+ m[:value].to_f,
12
+ m[:low].empty? ? nil : m[:low].to_f,
13
+ m[:high].empty? ? nil : m[:high].to_f
14
+ )
15
+ else
16
+ raise ArgumentError.new("TSV line `#{tsv_line}` is not a valid ChartPollsterTrendlinePoint line")
17
+ end
18
+ end
19
+ end
20
+ ChartPollsterTrendlinePoint::TsvLineRegex = /\A(?<label>[^\t]+)\t(?<date>\d\d\d\d-\d\d-\d\d)\t(?<value>\d+(?:\.\d+)?)\t(?<low>(?:\d+(?:\.\d+)?)?)\t(?<high>(?:\d+(?:\.\d+)?)?)/
21
+
22
+ ChartPollsterTrendlines = RubyImmutableStruct.new(:points) do
23
+ ExpectedHeaderLine = "label\tdate\tvalue\tlow\thigh"
24
+
25
+ def by_label
26
+ ret = points.group_by(&:label)
27
+ ret.values.each { |arr| arr.sort_by!(&:date) }
28
+ ret
29
+ end
30
+
31
+ def self.from_tsv(tsv)
32
+ lines = tsv.split(/(?:\r?\n)+/).reject(&:empty?)
33
+ if /\A#{ExpectedHeaderLine}/ !~ lines[0]
34
+ raise ArgumentError.new("First line of TSV is `#{lines[0]}`; expected `#{ExpectedHeaderLine}`")
35
+ else
36
+ points = lines[1..-1].map { |tsv_line| ChartPollsterTrendlinePoint.from_tsv_line(tsv_line) }
37
+ ChartPollsterTrendlines.new(points)
38
+ end
39
+ end
40
+ end
41
+ end