Class: Lita::Handlers::Tipbot

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hipchat_apiObject



200
201
202
203
204
205
# File 'lib/lita/handlers/tipbot.rb', line 200

def hipchat_api
  if @hipchat_api.nil?
    @hipchat_api = HipChat::API.new(config.hipchat_api_token)
  end
  @hipchat_api
end

#tipbot_apiObject



207
208
209
210
211
212
213
214
215
216
217
# File 'lib/lita/handlers/tipbot.rb', line 207

def tipbot_api
  if @tipbot_api.nil?
    params = {
      url: config.tipbot_url,
      auth_token: config.tipbot_auth_token,
      log: log
    }
    @tipbot_api = TipbotApi.new(params)
  end
  @tipbot_api
end

Instance Method Details

#active_room_members(room_jid) ⇒ Object

return a list of hipchat api v1 user hashes exclude any user with email in config.emails_to_exclude exclude any non-active user TODO: dont make an api call for every participant instead: make one call for all users, then check to see if each is an active participant



237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/lita/handlers/tipbot.rb', line 237

def active_room_members(room_jid)
  log.debug "looking up room jid: #{room_jid}"
  data = room_data(room_jid)
  log.debug "room_data: #{data.inspect}"
  results = []
  data['participants'].each do |p|
    user = hipchat_api.users_show(p['user_id'])
    next if user['user']['status'] != 'available'
    next if exclude_user? user['user']
    results << user['user']
  end
  results
end

#address(response) ⇒ Object



133
134
135
136
# File 'lib/lita/handlers/tipbot.rb', line 133

def address(response)
  hash = user_hash(response.user.mention_name)
  response.reply tipbot_api.address(hash)
end

#balance(response) ⇒ Object



138
139
140
141
# File 'lib/lita/handlers/tipbot.rb', line 138

def balance(response)
  hash = user_hash(response.user.mention_name)
  response.reply tipbot_api.balance(hash).to_s
end

#exclude_user?(user_hash) ⇒ Boolean

Returns:

  • (Boolean)


251
252
253
# File 'lib/lita/handlers/tipbot.rb', line 251

def exclude_user?(user_hash)
  config.emails_to_exclude.include?(user_hash['email'])
end

#hash_email(email) ⇒ Object



225
226
227
# File 'lib/lita/handlers/tipbot.rb', line 225

def hash_email(email)
  Digest::MD5.hexdigest email
end

#history(response) ⇒ Object



143
144
145
146
# File 'lib/lita/handlers/tipbot.rb', line 143

def history(response)
  hash = user_hash(response.user.mention_name)
  response.reply tipbot_api.history(hash)
end

#make_it_rain(response) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/lita/handlers/tipbot.rb', line 164

def make_it_rain(response)
  images = [
    "http://disinfo.s3.amazonaws.com/wp-content/uploads/2013/12/make-it-rain-1jk6.jpg",
    "http://voice.instructure.com/Portals/166399/images/scrooge-mcduck-make-it-rain.jpeg",
    "http://cdn01.dailycaller.com/wp-content/uploads/2012/10/Big-Bird-Makin-It-Rain-e1349457102996.jpeg",
    "http://i.imgur.com/jSaI0pv.jpg",
    "http://i.imgur.com/0Rz84wK.gif"
  ]

  response.reply([
    "#{response.user.name} is makin' it rain!",
    images.sample
  ])

  src_hash = user_hash(response.user.mention_name)
  room_jid = response.message.source.room
  users    = active_room_members room_jid

  users.shuffle.each do |user|
    # skip tipper
    next if user['mention_name'] == response.user.mention_name

    log.info "tipping #{user['email']}"

    dest_hash = hash_email user['email']
    log.debug "SRC  HASH: #{src_hash}"
    log.debug "DEST HASH: #{dest_hash}"
    log.debug "NAME:      #{user['name']}"

    response.reply "A coin for #{user['name']}!"
    tipbot_api.tip src_hash, dest_hash, 1
  end
end

#register(response) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/lita/handlers/tipbot.rb', line 122

def register(response)
  hash = user_hash(response.user.mention_name)

  log.info "Registering #{hash}"
  body = tipbot_api.register hash

  log.debug "register response: #{body}"
  # TODO: check for errors
  response.reply "You have been registered."
end

#room_data(room_jid) ⇒ Object



255
256
257
258
259
260
# File 'lib/lita/handlers/tipbot.rb', line 255

def room_data(room_jid)
  room_id = room_id_from_jid room_jid
  data = hipchat_api.rooms_show room_id
  log.debug "room #{room_id} data: #{data.inspect}"
  data['room']
end

#room_id_from_jid(room_jid) ⇒ Object



262
263
264
265
266
267
# File 'lib/lita/handlers/tipbot.rb', line 262

def room_id_from_jid(room_jid)
  data = hipchat_api.rooms_list
  log.debug "all room data: #{data.inspect}"
  room = data['rooms'].select {|r| r['xmpp_jid'] == room_jid}.first
  room.nil? ? nil : room['room_id']
end

#tip(response) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/lita/handlers/tipbot.rb', line 148

def tip(response)
  recipient, amount = response.match_data[1..2]
  from_hash = user_hash(response.user.mention_name)
  to_hash   = user_hash(recipient.slice(1..-1))
  tipbot_api.tip(from_hash, to_hash, amount)
  response.reply "Tip sent! Such kind shibe."
end

#user_hash(mention_name) ⇒ Object



219
220
221
222
223
# File 'lib/lita/handlers/tipbot.rb', line 219

def user_hash(mention_name)
  user_data = hipchat_api.users_list
  user = user_data['users'].select {|u| u['mention_name'] == mention_name}.first
  hash_email user['email']
end

#withdraw(response) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/lita/handlers/tipbot.rb', line 156

def withdraw(response)
  src_hash     = user_hash(response.user.mention_name)
  dest_address = response.match_data[1]

  resp = tipbot_api.withdraw(src_hash, dest_address)
  response.reply resp
end