lita-onewheel-election-cnn 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e3eea90d323e83e054a11374d154552b92b68974
4
- data.tar.gz: a5157607f4d7c831e81abff8a8bb019ed55c0df8
3
+ metadata.gz: 728698bfdb8985eef9a8628f23cafbf9b4481902
4
+ data.tar.gz: 65244dac6cd59d35755598d1c68af9844ec13f36
5
5
  SHA512:
6
- metadata.gz: 9f34ac75eaa55e404f13afa191ad8a24e1f8461fc35f4af0518d37b5b436f44087822e06078e64d9daea9a4c55800f73c897fdd7785951b3a93401e8e7afc610
7
- data.tar.gz: e6c1b3ffdcef10a2fcef7b607a2669f6d5eff767a57a7c8b44e64fd2b2a59a97fc471cceb05b06a4c4c863cd02f58cdc0fa4992857329a6a9d3e3687bd661c5b
6
+ metadata.gz: bb831ed4c7cd9302e38ecf39459a32960517a16abb14c153c7ed399270a508fc9fa1974cda06a090bab9a68c2699b77ec606cfbf9892a24755333386f1ac4c9b
7
+ data.tar.gz: 3e28d34cecd2ae4ce84ab00e95e67a8c690ceda21244d775ec516b1b52911242081bc416d82079c336cdf6ed5c0a71aa255235b675511c6cf1f999e7b8b23810
@@ -3,11 +3,16 @@ require 'rest-client'
3
3
  module Lita
4
4
  module Handlers
5
5
  class OnewheelElectionCnn < Handler
6
- route /^e[rl]ection/i,
6
+ route /^e[rl]ection$/i,
7
7
  :election,
8
8
  command: true,
9
9
  help: {'election' => 'Display the current election results.'}
10
10
 
11
+ route /^e[rl]ection\s+(.*)$/i,
12
+ :election_by_state,
13
+ command: true,
14
+ help: {'election AB' => 'Display the current election results in Alabama.'}
15
+
11
16
  def election(response)
12
17
  Lita.logger.debug 'get_source started'
13
18
  results = JSON.parse(RestClient.get('http://data.cnn.com/ELECTION/2016/full/P.full.json'))
@@ -19,6 +24,90 @@ module Lita
19
24
  response.reply candidate_str
20
25
  end
21
26
  end
27
+
28
+ def election_by_state(response)
29
+ Lita.logger.debug 'get_source started'
30
+ results = JSON.parse(RestClient.get('http://data.cnn.com/ELECTION/2016/full/P.full.json'))
31
+
32
+ state = stateness(response.matches[0][0])
33
+
34
+ results['races'].each do |race|
35
+ if race['state'].downcase == state.downcase
36
+ race['candidates'].each do |candidate|
37
+ candidate_str = "#{state} - #{candidate['fname']} #{candidate['lname']}: "
38
+ candidate_str += "WINNER! " if candidate['winner']
39
+ candidate_str += "#{candidate['pctDecimal']}%, #{candidate['vpct']}% of precincts reporting."
40
+ response.reply candidate_str
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ def stateness(gimme)
47
+ states = {"AK" => "Alaska",
48
+ "AL" => "Alabama",
49
+ "AR" => "Arkansas",
50
+ "AS" => "American Samoa",
51
+ "AZ" => "Arizona",
52
+ "CA" => "California",
53
+ "CO" => "Colorado",
54
+ "CT" => "Connecticut",
55
+ "DC" => "District of Columbia",
56
+ "DE" => "Delaware",
57
+ "FL" => "Florida",
58
+ "GA" => "Georgia",
59
+ "GU" => "Guam",
60
+ "HI" => "Hawaii",
61
+ "IA" => "Iowa",
62
+ "ID" => "Idaho",
63
+ "IL" => "Illinois",
64
+ "IN" => "Indiana",
65
+ "KS" => "Kansas",
66
+ "KY" => "Kentucky",
67
+ "LA" => "Louisiana",
68
+ "MA" => "Massachusetts",
69
+ "MD" => "Maryland",
70
+ "ME" => "Maine",
71
+ "MI" => "Michigan",
72
+ "MN" => "Minnesota",
73
+ "MO" => "Missouri",
74
+ "MS" => "Mississippi",
75
+ "MT" => "Montana",
76
+ "NC" => "North Carolina",
77
+ "ND" => "North Dakota",
78
+ "NE" => "Nebraska",
79
+ "NH" => "New Hampshire",
80
+ "NJ" => "New Jersey",
81
+ "NM" => "New Mexico",
82
+ "NV" => "Nevada",
83
+ "NY" => "New York",
84
+ "OH" => "Ohio",
85
+ "OK" => "Oklahoma",
86
+ "OR" => "Oregon",
87
+ "PA" => "Pennsylvania",
88
+ "PR" => "Puerto Rico",
89
+ "RI" => "Rhode Island",
90
+ "SC" => "South Carolina",
91
+ "SD" => "South Dakota",
92
+ "TN" => "Tennessee",
93
+ "TX" => "Texas",
94
+ "UT" => "Utah",
95
+ "VA" => "Virginia",
96
+ "VI" => "Virgin Islands",
97
+ "VT" => "Vermont",
98
+ "WA" => "Washington",
99
+ "WI" => "Wisconsin",
100
+ "WV" => "West Virginia",
101
+ "WY" => "Wyoming"}
102
+
103
+ search_state = gimme.upcase
104
+ if search_state.length == 2
105
+ states[search_state.upcase]
106
+ else
107
+ search_state
108
+ end
109
+ end
110
+
22
111
  Lita.register_handler(self)
23
112
  end
24
113
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'lita-onewheel-election-cnn'
3
- spec.version = '0.0.0'
3
+ spec.version = '0.1.0'
4
4
  spec.authors = ['Andrew Kreps']
5
5
  spec.email = ['andrew.kreps@gmail.com']
6
6
  spec.description = %q{Lita interface to CNN's 2016 Presidential Election JSON.}
@@ -18,4 +18,28 @@ describe Lita::Handlers::OnewheelElectionCnn, lita_handler: true do
18
18
  expect(replies[0]).to eq("Hillary Clinton: WINNER! 0.0%, 0% of precincts reporting.")
19
19
  expect(replies[1]).to eq("Donald Trump: 0.0%, 0% of precincts reporting.")
20
20
  end
21
+
22
+ it 'shows by state' do
23
+ mock = File.open('spec/fixtures/election.json').read
24
+ allow(RestClient).to receive(:get) { mock }
25
+ send_command 'election va'
26
+ expect(replies[0]).to eq("Virginia - Hillary Clinton: 0.0%, 0% of precincts reporting.")
27
+ expect(replies[1]).to eq("Virginia - Donald Trump: 0.0%, 0% of precincts reporting.")
28
+ end
29
+
30
+ it 'shows by STATE' do
31
+ mock = File.open('spec/fixtures/election.json').read
32
+ allow(RestClient).to receive(:get) { mock }
33
+ send_command 'election VA'
34
+ expect(replies[0]).to eq("Virginia - Hillary Clinton: 0.0%, 0% of precincts reporting.")
35
+ expect(replies[1]).to eq("Virginia - Donald Trump: 0.0%, 0% of precincts reporting.")
36
+ end
37
+
38
+ it 'shows by full state name downcase' do
39
+ mock = File.open('spec/fixtures/election.json').read
40
+ allow(RestClient).to receive(:get) { mock }
41
+ send_command 'election new york'
42
+ expect(replies[0]).to eq("NEW YORK - Hillary Clinton: 0.0%, 0% of precincts reporting.")
43
+ expect(replies[1]).to eq("NEW YORK - Donald Trump: 0.0%, 0% of precincts reporting.")
44
+ end
21
45
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-onewheel-election-cnn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kreps