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



197
198
199
200
201
202
# File 'lib/lita/handlers/tipbot.rb', line 197

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

#tipbot_apiObject



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/lita/handlers/tipbot.rb', line 204

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



234
235
236
237
238
239
240
241
242
243
244
# File 'lib/lita/handlers/tipbot.rb', line 234

def active_room_members(room_jid)
  data = room_data(room_jid)
  results = []
  data['participants'].each do |p|
    user = hipchat_api.users_show(p['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)


246
247
248
# File 'lib/lita/handlers/tipbot.rb', line 246

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

#hash_email(email) ⇒ Object



222
223
224
# File 'lib/lita/handlers/tipbot.rb', line 222

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
# 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|
    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



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

def room_data(room_jid)
  room_id = room_id_from_jid room_jid
  data = hipchat_api.rooms_show room_id
  data['room']
end

#room_id_from_jid(room_jid) ⇒ Object



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

def room_id_from_jid(room_jid)
  data = hipchat_api.rooms_list
  room = data['rooms'].select {|r| r['xmpp_jid'] == room_jid}.first
  room.nil? ? nil : 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



216
217
218
219
220
# File 'lib/lita/handlers/tipbot.rb', line 216

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