-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudrey2.rb
61 lines (52 loc) · 1.43 KB
/
audrey2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# audrey2.rb
require 'sinatra'
require 'sinatra/json'
require 'multi_json'
require 'redis'
configure :development, :test do
set :bind, '0.0.0.0'
end
get '/' do
if settings.development?
'Welcome to audrey2'
else
not_found
end
end
get '/v1/feeds' do
redis = Redis.new
feed_keys = redis.keys(params[:type] ? "#{params[:type]}:*" : '*')
feeds = feed_keys.map do |key|
if redis.type(key) != 'hash'
# ignore keys with non-hash values
else
feed = redis.hgetall(key)
# the public interface doesn't need "shelby_" as the prefix on the names of the keys
feed.keys.each do |k|
if k.start_with?('shelby_')
feed[k[7..-1]] = feed.delete(k)
end
end
feed["id"] = key
feed
end
end
feeds.compact!
json feeds
end
post '/v1/feeds' do
redis = Redis.new
if params[:id] && params[:id].length > 0 && params[:type] && params[:type].length > 0 && params[:auth_token] && params[:auth_token].length > 0 && params[:roll_id] && params[:roll_id].length > 0
id = "#{params[:type]}:#{params[:id]}"
if redis.exists id
# return an error if the key already exists
422
else
# otherwise, save the feed info and return
redis.mapped_hmset id, {'shelby_auth_token' => params[:auth_token], 'shelby_roll_id' => params[:roll_id]}
json({'id' => id, 'auth_token' => params[:auth_token], 'roll_id' => params[:roll_id]})
end
else
422
end
end