Class: Lita::Handlers::OnewheelElectionCnn

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/onewheel_election_cnn.rb

Instance Method Summary collapse

Instance Method Details

#election(response) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lita/handlers/onewheel_election_cnn.rb', line 24

def election(response)
  Lita.logger.debug 'Getting election data'
  results = JSON.parse(RestClient.get('http://data.cnn.com/ELECTION/2016/full/P.full.json'))

  results['candidates'].each do |candidate|
    candidate_str = "#{candidate['fname']} #{candidate['lname']}: "
    candidate_str += "#{candidate['pctDecimal']}%"
    candidate_str += " WINNER! #{candidate['evotes']} electoral votes." if candidate['winner']
    Lita.logger.debug "Replying with #{candidate_str}"
    response.reply candidate_str
  end
end

#election_by_state(response) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/lita/handlers/onewheel_election_cnn.rb', line 37

def election_by_state(response)
  Lita.logger.debug 'get_source started'
  results = JSON.parse(RestClient.get('http://data.cnn.com/ELECTION/2016/full/P.full.json'))

  state = stateness(response.matches[0][0])

  results['races'].each do |race|
    if race['state'].downcase == state.downcase
      state_reply = "#{state}, #{race['evotes']} electoral votes, #{race['pctsrep']}% reporting"
      response.reply state_reply
      Lita.logger.debug "Replying with #{state_reply}"
      race['candidates'].each do |candidate|
        candidate_str = "#{candidate['fname']} #{candidate['lname']}: "
        candidate_str += "#{candidate['pctDecimal']}%"
        candidate_str += " WINNER! #{candidate['evotes']} electoral votes." if candidate['winner']
        Lita.logger.debug "Replying with #{candidate_str}"
        response.reply candidate_str
      end
    end
  end
end

#stateness(gimme) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/lita/handlers/onewheel_election_cnn.rb', line 59

def stateness(gimme)
  states = {"AK" => "Alaska",
                  "AL" => "Alabama",
                  "AR" => "Arkansas",
                  "AS" => "American Samoa",
                  "AZ" => "Arizona",
                  "CA" => "California",
                  "CO" => "Colorado",
                  "CT" => "Connecticut",
                  "DC" => "District of Columbia",
                  "DE" => "Delaware",
                  "FL" => "Florida",
                  "GA" => "Georgia",
                  "GU" => "Guam",
                  "HI" => "Hawaii",
                  "IA" => "Iowa",
                  "ID" => "Idaho",
                  "IL" => "Illinois",
                  "IN" => "Indiana",
                  "KS" => "Kansas",
                  "KY" => "Kentucky",
                  "LA" => "Louisiana",
                  "MA" => "Massachusetts",
                  "MD" => "Maryland",
                  "ME" => "Maine",
                  "MI" => "Michigan",
                  "MN" => "Minnesota",
                  "MO" => "Missouri",
                  "MS" => "Mississippi",
                  "MT" => "Montana",
                  "NC" => "North Carolina",
                  "ND" => "North Dakota",
                  "NE" => "Nebraska",
                  "NH" => "New Hampshire",
                  "NJ" => "New Jersey",
                  "NM" => "New Mexico",
                  "NV" => "Nevada",
                  "NY" => "New York",
                  "OH" => "Ohio",
                  "OK" => "Oklahoma",
                  "OR" => "Oregon",
                  "PA" => "Pennsylvania",
                  "PR" => "Puerto Rico",
                  "RI" => "Rhode Island",
                  "SC" => "South Carolina",
                  "SD" => "South Dakota",
                  "TN" => "Tennessee",
                  "TX" => "Texas",
                  "UT" => "Utah",
                  "VA" => "Virginia",
                  "VI" => "Virgin Islands",
                  "VT" => "Vermont",
                  "WA" => "Washington",
                  "WI" => "Wisconsin",
                  "WV" => "West Virginia",
                  "WY" => "Wyoming"}

  search_state = gimme.upcase
  if search_state.length == 2
    Lita.logger.debug "Returning #{states[search_state.upcase]} for #{search_state}"
    states[search_state.upcase]
  else
    Lita.logger.debug "Returning #{search_state}"
    search_state
  end
end