-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypeform_callback.rb
More file actions
42 lines (35 loc) · 830 Bytes
/
typeform_callback.rb
File metadata and controls
42 lines (35 loc) · 830 Bytes
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
class UnsupportedFieldType < StandardError; end
class TypeformCallback
attr_reader :body
def initialize(body)
@body = body
end
def answers
json["answers"].inject({}) do |result, answer|
case answer["type"]
when "text"
value = answer["value"]
when "choice"
value = answer["value"]["label"]
when "number"
value = answer["value"]["amount"]
else
raise UnsupportedFieldType, answer["type"]
end
key = deserialize_answer(answer["tags"][0])
if result[key]
result[key] = Array(result[key])
result[key] << value
else
result[key] = value
end
result
end
end
private def json
@json ||= JSON.parse(body)
end
private def deserialize_answer(string)
string.gsub(":", ".")
end
end