pollster 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -37,9 +37,9 @@ List charts about Wisconsin
37
37
  Calculate the margin between Obama and Romney from a recent general election poll
38
38
 
39
39
  poll = Poll.where(:chart => '2012-general-election-romney-vs-obama').first
40
- question = poll.questions.detect { |question| question[:chart] == '2012-general-election-romney-vs-obama' }
41
- obama = question[:responses].detect { |response| response[:choice] == "Obama" }
42
- romney = question[:responses].detect { |response| response[:choice] == "Romney" }
40
+ responses = poll.questions.detect { |question| question.chart == '2012-general-election-romney-vs-obama' }.responses
41
+ obama = responses.detect { |response| response.choice == "Obama" }
42
+ romney = responses.detect { |response| response.choice == "Romney" }
43
43
  obama[:value] - romney[:value]
44
44
 
45
45
  See the methodology used in recent polls about the Affordable Care Act
@@ -100,32 +100,33 @@ List the polls that were used to create the estimate for a specific chart:
100
100
  >> chart = Pollster::Chart.find('2012-iowa-gop-primary')
101
101
  >> chart.polls
102
102
  => [#<Pollster::Poll:...
103
- @end_date=#<Date: 2012-01-01 (4911855/2,0,2299161)>,
104
- @id=12385,
105
- @method="Automated Phone",
106
- @pollster="InsiderAdvantage",
107
- @questions=
108
- [{:name=>"2012 Iowa GOP Primary",
109
- :chart=>"2012-iowa-gop-primary",
110
- :topic=>"2012-gop-primary",
111
- :state=>"IA",
112
- :responses=>
113
- [{:choice=>"Undecided",
114
- :value=>2,
115
- :subpopulation=>"Likely Voters",
116
- :number_of_observations=>729,
117
- :margin_of_error=>nil},
118
- {:choice=>"Bachmann",
119
- :value=>6,
120
- :subpopulation=>"Likely Voters",
121
- :number_of_observations=>729,
122
- :margin_of_error=>nil},
123
- {:choice=>"Romney",
124
- :value=>23,
125
- :subpopulation=>"Likely Voters",
126
- :number_of_observations=>729,
127
- :margin_of_error=>nil},
128
- ...]
103
+ @start_date=#<Date: 2012-01-01 (4911855/2,0,2299161)>>,
104
+ @end_date=#<Date: 2012-01-01 (4911855/2,0,2299161)>,
105
+ @id=12385,
106
+ @method="Automated Phone",
107
+ @pollster="InsiderAdvantage",
108
+ @questions=
109
+ [{:name=>"2012 Iowa GOP Primary",
110
+ :chart=>"2012-iowa-gop-primary",
111
+ :topic=>"2012-gop-primary",
112
+ :state=>"IA",
113
+ :subpopulations=>
114
+ [{:name=>"Likely Voters",
115
+ :observations=>729,
116
+ :margin_of_error=>nil,
117
+ :responses=>
118
+ [{:choice=>"Romney", :value=>23},
119
+ {:choice=>"Bachmann", :value=>6},
120
+ {:choice=>"Gingrich", :value=>16},
121
+ {:choice=>"Huntsman", :value=>2},
122
+ {:choice=>"Paul", :value=>22},
123
+ {:choice=>"Perry", :value=>10},
124
+ {:choice=>"Santorum", :value=>18},
125
+ {:choice=>"Other", :value=>1},
126
+ {:choice=>"Undecided", :value=>2}]}]}],
127
+ @source= "http://www.realclearpolitics.com/docs/2012/InsiderAdvantage_Iowa_0102.pdf",
128
+ ...]
129
+
129
130
 
130
131
  You may also list all polls available through Pollster:
131
132
 
@@ -139,6 +140,7 @@ This response is paginated, with 10 polls per page. To access subsequent pages,
139
140
 
140
141
  - Aaron Bycoffe, bycoffe@huffingtonpost.com
141
142
  - Jay Boice, jay.boice@huffingtonpost.com
143
+ - Andrei Scheinkman, andrei@huffingtonpost.com
142
144
 
143
145
  ## Copyright
144
146
 
data/lib/pollster.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require 'pollster/base'
2
2
  require 'pollster/chart'
3
3
  require 'pollster/poll'
4
+ require 'pollster/question'
data/lib/pollster/poll.rb CHANGED
@@ -11,7 +11,7 @@ module Pollster
11
11
  end
12
12
 
13
13
  # Get a list of all polls.
14
- # Polls are listed in pages of 25.
14
+ # Polls are listed in pages of 10.
15
15
  def self.all(params={})
16
16
  page = params[:page] || 1
17
17
  invoke('polls', {:page => page}).map { |poll| self.create(poll) }
@@ -31,7 +31,9 @@ module Pollster
31
31
  def self.create(data)
32
32
  data = hash_keys_to_sym(data)
33
33
  data[:questions] = data[:questions].map { |question| hash_keys_to_sym(question) }
34
- data[:questions].each { |question| question[:responses] = question[:responses].map { |response| hash_keys_to_sym(response) } }
34
+ data[:questions].each { |question| question[:subpopulations] = question[:subpopulations].map { |subpopulation| hash_keys_to_sym(subpopulation) } }
35
+ data[:questions].each { |question| question[:subpopulations].each { |subpopulation| subpopulation[:responses] = subpopulation[:responses].map { |response| hash_keys_to_sym(response) } } }
36
+ data[:questions] = data[:questions].map { |question| Pollster::Question.new(question) }
35
37
  data[:start_date] = Date.parse(data[:start_date])
36
38
  data[:end_date] = Date.parse(data[:end_date])
37
39
  self.new(data)
@@ -0,0 +1,19 @@
1
+ module Pollster
2
+
3
+ class Question < Base
4
+
5
+ attr_reader :name, :chart, :topic, :state, :subpopulations
6
+
7
+ def initialize(params={})
8
+ params.each_pair do |k,v|
9
+ instance_variable_set("@#{k}", v)
10
+ end
11
+ end
12
+
13
+ def responses
14
+ subpopulations.first[:responses]
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -1,3 +1,3 @@
1
1
  module Pollster
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
data/pollster.gemspec CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
6
6
  s.name = 'pollster'
7
7
  s.version = Pollster::VERSION
8
8
  s.platform = Gem::Platform::RUBY
9
- s.authors = ["Jay Boice", "Aaron Bycoffe"]
9
+ s.authors = ["Jay Boice", "Aaron Bycoffe", "Andrei Scheinkman"]
10
10
  s.email = ["jay.boice@huffingtonpost.com", "bycoffe@huffingtonpost.com"]
11
11
  s.homepage = "http://github.com/huffingtonpost/ruby-pollster"
12
12
  s.summary = "Ruby library for accessing the Pollster API"
metadata CHANGED
@@ -1,16 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pollster
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jay Boice
9
9
  - Aaron Bycoffe
10
+ - Andrei Scheinkman
10
11
  autorequire:
11
12
  bindir: bin
12
13
  cert_chain: []
13
- date: 2012-07-05 00:00:00.000000000 Z
14
+ date: 2012-07-09 00:00:00.000000000 Z
14
15
  dependencies:
15
16
  - !ruby/object:Gem::Dependency
16
17
  name: json
@@ -62,6 +63,7 @@ files:
62
63
  - lib/pollster/base.rb
63
64
  - lib/pollster/chart.rb
64
65
  - lib/pollster/poll.rb
66
+ - lib/pollster/question.rb
65
67
  - lib/pollster/version.rb
66
68
  - pollster.gemspec
67
69
  - test/test_pollster.rb
@@ -90,4 +92,3 @@ signing_key:
90
92
  specification_version: 3
91
93
  summary: Ruby library for accessing the Pollster API
92
94
  test_files: []
93
- has_rdoc: