nextbike.rb - nextbike - Lita Slack chatbot plugin for nextbike interactions.
(HTM) git clone git://jay.scot/nextbike
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
nextbike.rb (2428B)
---
1 require 'nokogiri'
2 require 'open-uri'
3
4 module Lita
5 module Handlers
6
7 class NextBike < Handler
8 REDIS_CITY_KEY = "nc_"
9
10 template_root File.expand_path("../../templates", __FILE__)
11 route('nextbike', :nextbike, help: { "nextbike" => "Shows how many bikes are at a given location" })
12 route('nextbike status', :status)
13 route('nextbike update', :update)
14 route('nextbike show cities', :listcities)
15 route(/^nextbike show location\s(.+)/, :listlocation)
16
17 def status(response)
18 response.reply "Status: All good in the hood."
19 end
20
21 def update(response)
22 doc = Nokogiri::XML(open("https://nextbike.net/maps/nextbike-official.xml"))
23 count = 0
24 doc.search('//country//city').each do |data|
25 uid = data['uid']
26 name = data['name']
27 redis.hset(REDIS_CITY_KEY,uid,name)
28 count+=1
29 end
30 response.reply "*Update*: #{count} cities added/refreshed."
31 end
32
33 def listcities(response)
34 city_list = redis.hvals(REDIS_CITY_KEY)
35 if city_list.empty?
36 response.reply("*Info*: No cities are stored, run update?")
37 else
38 response.reply(render_template("nextbike_cities.slack", data: city_list))
39 end
40 end
41
42 def getUID
43 end
44
45 def getName
46 end
47
48 def listlocation(response)
49 city_uid = nil
50 city_name = response.matches.first
51 city_list = redis.hgetall(REDIS_CITY_KEY)
52 city_list.each do |key, value|
53 if "#{value.downcase}" == "#{city_name.first}"
54 city_uid = key
55 end
56 end
57
58 if city_uid.nil?
59 response.reply("*Info*: Could not find the city #{city_name.first}")
60 return true
61 end
62
63 doc = Nokogiri::XML(open("https://nextbike.net/maps/nextbike-official.xml?city=#{city_uid}"))
64 location_list = []
65 doc.search('//country//city//place').each do |data|
66 location_list.push(data['name'])
67 end
68
69 response.reply(render_template("nextbike_location.slack", data: location_list))
70 end
71
72 def nextbike(response)
73 doc = Nokogiri::XML(open("https://nextbike.net/maps/nextbike-official.xml?city=237"))
74 doc.search('//country//city//place').each do |data|
75 if data['uid'] == "264308"
76 response.reply("*#{data['name']}* has *#{data['bikes']}* bikes left.")
77 end
78 end
79 end
80
81 end
82
83 Lita.register_handler(NextBike)
84
85 end
86 end