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
data/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- require 'rake/testtask'
2
-
3
- Rake::TestTask.new do |t|
4
- t.libs << 'test'
5
- end
6
-
7
- desc "Run tests"
8
- task :default => :test
@@ -1,45 +0,0 @@
1
- require 'json'
2
- require 'uri'
3
- require 'net/http'
4
- require 'date'
5
- require 'time'
6
- require 'zlib'
7
-
8
- module Pollster
9
-
10
- class Base
11
- API_SERVER = 'elections.huffingtonpost.com'
12
- API_BASE = '/pollster/api'
13
-
14
- class << self
15
-
16
- private
17
-
18
- def encode_params(params)
19
- params.map { |k, v| "#{k}=#{v}" }.join('&')
20
- end
21
-
22
- def build_request_url(path, params={})
23
- URI("http://#{API_SERVER}#{API_BASE}/#{path}#{params.size > 0 ? "?#{encode_params(params)}" : ''}")
24
- end
25
-
26
- def invoke(path, params={})
27
- uri = build_request_url(path, params)
28
- request = Net::HTTP::Get.new(uri.request_uri)
29
- request['Accept-Encoding'] = 'gzip,deflate'
30
- response = Net::HTTP.start(uri.host, uri.port) { |http| http.request(request) }
31
- body = response.header['Content-Encoding'].eql?('gzip') ?
32
- Zlib::GzipReader.new(StringIO.new(response.body)).read() :
33
- response.body
34
- raise Exception, JSON.parse(body)["errors"].join(', ') if response.code.eql?('400')
35
- JSON.parse(body)
36
- end
37
-
38
- def hash_keys_to_sym(hash)
39
- Hash[*hash.map { |k, v| [k.to_sym, v] }.flatten(1)]
40
- end
41
-
42
- end
43
- end
44
-
45
- end
@@ -1,69 +0,0 @@
1
- module Pollster
2
-
3
- class Chart < Base
4
-
5
- attr_reader :title, :slug, :poll_count, :last_updated, :url, :estimates, :estimates_by_date, :state, :topic
6
-
7
- def initialize(params={})
8
- params.each_pair do |k,v|
9
- instance_variable_set("@#{k}", v)
10
- end
11
- end
12
-
13
- # Get a list of polls for this chart.
14
- def polls(params={})
15
- Poll.where(params.merge({:chart => self.slug}))
16
- end
17
-
18
- # Get a list of all charts.
19
- def self.all
20
- invoke('charts').map { |chart| self.create(chart) }
21
- end
22
-
23
- # Get a chart based on its slug.
24
- def self.find(slug)
25
- self.create invoke("charts/#{slug}")
26
- end
27
-
28
- # Get a list of charts based on the given parameters.
29
- # See API documentation for acceptable parameters.
30
- def self.where(params={})
31
- if params.empty?
32
- raise "A search parameter is required"
33
- end
34
- invoke('charts', params).map { |chart| self.create(chart) }
35
- end
36
-
37
- def to_s
38
- "#{self.class}: #{self.title}"
39
- end
40
-
41
- def inspect
42
- "<#{self.class}: #{self.title}>"
43
- end
44
-
45
- def estimates_by_date
46
- @estimates_by_date ||= Pollster::Chart.find(slug).estimates_by_date
47
- end
48
-
49
- private
50
-
51
- def self.create(data)
52
- data = Hash[*data.map { |k, v| [k.to_sym, v] }.flatten(1)]
53
- data[:last_updated] = Time.parse(data[:last_updated])
54
- data[:estimates].map! { |estimate| {:choice => estimate['choice'], :value => estimate['value'], :chance => estimate['chance']} }
55
- if data[:estimates_by_date]
56
- data[:estimates_by_date] = data[:estimates_by_date].map do |x|
57
- estimate = hash_keys_to_sym(x)
58
- estimate[:date] = Date.parse(estimate[:date])
59
- estimate[:estimates] = estimate[:estimates].map { |e| hash_keys_to_sym(e) }
60
- estimate
61
- end
62
- end
63
- self.new(data)
64
- end
65
-
66
-
67
- end
68
-
69
- end
@@ -1,47 +0,0 @@
1
- module Pollster
2
-
3
- class Poll < Base
4
-
5
- attr_reader :start_date, :end_date, :method, :pollster, :url, :source, :questions, :survey_houses, :sponsors
6
-
7
- def initialize(params={})
8
- params.each_pair do |k,v|
9
- instance_variable_set("@#{k}", v)
10
- end
11
- end
12
-
13
- # Get a list of all polls.
14
- # Polls are listed in pages of 10.
15
- def self.all(params={})
16
- page = params[:page] || 1
17
- invoke('polls', {:page => page}).map { |poll| self.create(poll) }
18
- end
19
-
20
- # Get a list of polls based on the given parameters.
21
- # See API documentation for acceptable parameters.
22
- def self.where(params={})
23
- if params.empty?
24
- raise "A search parameter is required"
25
- end
26
- invoke('polls', params).map { |poll| self.create(poll) }
27
- end
28
-
29
- private
30
-
31
- def self.create(data)
32
- data = hash_keys_to_sym(data)
33
- data[:questions] = data[:questions].map { |question| hash_keys_to_sym(question) }
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) }
37
- data[:start_date] = Date.parse(data[:start_date])
38
- data[:end_date] = Date.parse(data[:end_date])
39
- data[:survey_houses] = data[:survey_houses].map { |survey_house| hash_keys_to_sym(survey_house) }
40
- data[:sponsors] = data[:sponsors].map { |sponsor| hash_keys_to_sym(sponsor) }
41
- self.new(data)
42
- end
43
-
44
-
45
- end
46
-
47
- end
@@ -1,19 +0,0 @@
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,26 +0,0 @@
1
- require 'test/unit'
2
- require 'pollster'
3
- include Pollster
4
-
5
- class PollsterTest < Test::Unit::TestCase
6
-
7
- def test_ge_chart
8
- assert_equal "US", Chart.find('2012-general-election-romney-vs-obama').state
9
- end
10
-
11
- def test_poll_page_size
12
- assert_equal 10, Poll.all.size
13
- end
14
-
15
- def test_invalid_chart_slug
16
- assert_raise(Exception) { Chart.find('invalid-slug') }
17
- end
18
-
19
- def test_estimates_by_date
20
- assert_block do
21
- chart = Chart.all.first
22
- chart.estimates_by_date.is_a?(Array)
23
- end
24
- end
25
-
26
- end