-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.rb
More file actions
117 lines (106 loc) · 2.42 KB
/
api.rb
File metadata and controls
117 lines (106 loc) · 2.42 KB
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
require 'json'
module API
def def_level(&blk)
res = {"type" => "root"}
if respond_to?(:[]=)
self["subsections"] ||= []
self["subsections"] << res
end
res.instance_eval(&blk)
res["subsections"].each do |subsec|
id = subsec["id"]
if !id && subsec["name"]
id = subsec["name"].downcase.gsub(/[^A-Za-z0-9]/, '_')
end
node = subsec["node"]
if !node && subsec["name"]
node = subsec["name"].gsub(/[^A-Za-z0-9 ]/, '').split(' ').map(&:capitalize).join
end
puts "[#{id}]"
puts "type=#{subsec.fetch("type").to_json}"
unless id.start_with?("_")
puts "name=#{subsec.fetch("name").to_json}"
puts "node=#{node.to_json}"
end
subsec.delete("id")
subsec.delete("type")
subsec.delete("name")
subsections = subsec.delete("subsections")
subsec.each do |k, v|
puts "#{k}=#{v.to_json}"
end
if subsections
puts "subsections=#{JSON.pretty_generate(subsections)}"
end
puts
end
end
%i(
sequence
camera_zoom
simple
choice
password
sticky_note
green_sticky_note
multi_visit
calendar
level_2_calendar
level_2_zodiac
level_3_shredded_sticky
level_3_math_table
search_engine
next_level
play_animation
branch
close
hints
).each do |sym|
class_eval <<~EOF
def #{sym}(name = nil, &blk)
res = {"type" => "#{sym}"}
if respond_to?(:[]=)
self["subsections"] ||= []
self["subsections"] << res
end
res["name"] = name if name
res.instance_eval(&blk) if blk
res
end
EOF
end
alias choose choice
def message(msg)
simple do |s|
s.message = fix_msg(msg)
end
end
def messages(msgs)
simple do |s|
s.messages = msgs.map { |m| fix_msg(m) }
end
end
def fix_msg(msg)
msg.split("\n").map(&:lstrip).join("\n")
end
def on_choice(choice_msg, &blk)
self["choices"] ||= []
choice = {"message" => choice_msg}
self["choices"] << choice
instance_eval do
sequence.instance_exec(choice, &blk)
end
raise "Can't set_flag on choice; set_flag on choice response instead" if choice["set_flag"]
end
end
class Hash
include API
def method_missing(sym, *args)
if sym.end_with?("=")
sym_s = sym.to_s[0..-2]
self[sym_s] = args[0]
else
super
end
end
end