-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathweb.rb
135 lines (100 loc) · 2.88 KB
/
web.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
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
125
126
127
128
129
130
131
132
require 'sinatra'
configure do
require 'dalli'
if ENV["MEMCACHEDCLOUD_SERVERS"]
servers = ENV["MEMCACHEDCLOUD_SERVERS"].split(',')
username = ENV["MEMCACHEDCLOUD_USERNAME"]
password = ENV["MEMCACHEDCLOUD_PASSWORD"]
else
servers = ['localhost:11211']
username = nil
password = nil
end
$cache = Dalli::Client.new(servers, :username => username, :password => password, :expires_in => 300)
end
# Inbound routes
get '/' do
erb :index
end
get '/endpoint' do
rc = process_input("GET", request)
if (rc == true)
erb :data_recd_200, :layout => nil
else
426
end
end
post '/endpoint' do
rc = process_input("POST", request)
if (rc == true)
erb :data_recd_200, :layout => nil
else
426
end
end
# read from cache
get '/card/:id' do |card_id|
data = $cache.get(card_id)
string = ''
unless data.kind_of?(Array) && data.length > 0
string = "None found. Note that card data is not retained for longer than 5 minutes and is lost upon service restarts."
end
@records = data
erb :card, :locals => { :card_id => card_id, :string => string }, :layout => :layout
end
# delete card
get '/delete/:id' do |card_id|
data = $cache.get(card_id)
if data.nil?
string = 'Card data does not exist to delete.'
else
$cache.delete(card_id)
erb :card, :locals => { :card_id => card_id, :string => string }, :layout => :layout
end
end
get '/test403' do
403
end
get '/test426' do
426
end
# Error responses
error 403 do
'Forbidden, SSL is required'
end
error 426 do
'Bad Request, required lead generation parameters missing'
#erb :error426
end
error Dalli::RingError do
status 503
erb :cache_503, :layout => :layout
end
# write to cache
def write_to_cache(card_id, name, email, screen_name, token, method)
data = $cache.get(card_id)
unless data.kind_of?(Array)
data = []
end
# this is a demo app, we have no need to keep the email we get, so let's just record if we think we got one
email_found = (email.length > 5) ? "Yes" : "No"
card = {"name" => name, "email" => email_found, "screen_name" => screen_name, "token" => token, "method" => method}
data.push(card)
$cache.set(card_id, data)
end
def process_input (method, request)
# we always expect these fields
# name, email, screen_name, token, card
# TODO we could get custom fields, store those too
name = request["name"] ? request["name"] : nil
email = request["email"] ? request["email"] : nil
screen_name = request["screen_name"] ? request["screen_name"] : nil
token = request["token"] ? request["token"] : nil
card = request["card"] ? request["card"] : nil
if (name && email && screen_name && token && card)
write_to_cache(card, name, email, screen_name, token, method)
return true
else
return false
end
end