Class: Dao::Api
- Inherits:
-
Object
show all
- Includes:
- Validations
- Defined in:
- lib/dao/api.rb,
lib/dao/api/dsl.rb,
lib/dao/api/call.rb,
lib/dao/api/modes.rb,
lib/dao/api/routes.rb,
lib/dao/api/initializers.rb
Defined Under Namespace
Classes: DSL
Constant Summary
collapse
- Initializers =
{}
Constants included
from Validations
Validations::ClassMethods, Validations::InstanceMethods
Class Method Summary
collapse
Instance Method Summary
collapse
-
#after_initialize(*args, &block) ⇒ Object
-
#after_initializers(*args, &block) ⇒ Object
-
#before_initialize(*args, &block) ⇒ Object
-
#before_initializers(*args, &block) ⇒ Object
-
#call(path = '/index', params = {}, options = {}) ⇒ Object
-
#callstack(context = nil, &block) ⇒ Object
-
#catching(label = :result, &block) ⇒ Object
-
#catching? ⇒ Boolean
-
#catching_results(&block) ⇒ Object
-
#catching_results? ⇒ Boolean
-
#context ⇒ Object
-
#context? ⇒ Boolean
-
#data(*args) ⇒ Object
-
#data!(*args) ⇒ Object
-
#endpoints ⇒ Object
-
#index ⇒ Object
-
#mode(*args, &block) ⇒ Object
-
#mode=(mode) ⇒ Object
-
#mode?(mode, &block) ⇒ Boolean
-
#on(mode, *args, &block) ⇒ Object
-
#parameter(*args, &block) ⇒ Object
(also: #param)
immediate parameter parsing support.
-
#parameter!(*args, &block) ⇒ Object
(also: #param!)
-
#respond_to?(*args) ⇒ Boolean
-
#return!(*value) ⇒ Object
-
#route?(path) ⇒ Boolean
will an endpoint route to a endpoint?.
-
#route_for(*args) ⇒ Object
-
#run_initializers(which, *args, &block) ⇒ Object
-
#status(*args) ⇒ Object
-
#status!(*args) ⇒ Object
-
#superclasses ⇒ Object
add, for, included
Class Method Details
.add_mode(mode) ⇒ Object
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
|
# File 'lib/dao/api/modes.rb', line 15
def add_mode(mode)
mode = Mode.for(mode)
unless modes.include?(mode)
module_eval(<<-__, __FILE__, __LINE__ - 1)
def #{ mode }(*args, &block)
if args.empty?
if catching_results?
if self.mode.case_of?(Mode.for(#{ mode.inspect }))
mode(#{ mode.inspect }, &block)
return!
end
else
mode(#{ mode.inspect }, &block)
end
else
mode(#{ mode.inspect }) do
call(*args, &block)
end
end
end
alias_method(:#{ mode.upcase }, :#{ mode })
def #{ mode }?(&block)
mode?(#{ mode.inspect }, &block)
end
alias_method(:#{ mode.upcase }?, :#{ mode }?)
__
modes.push(mode)
mode
else
false
end
end
|
.after_initializer(&block) ⇒ Object
34
35
36
|
# File 'lib/dao/api/initializers.rb', line 34
def after_initializer(&block)
after_initializers.push(block)
end
|
.after_initializers ⇒ Object
30
31
32
|
# File 'lib/dao/api/initializers.rb', line 30
def after_initializers
initializers[:after]
end
|
.before_initializer(&block) ⇒ Object
24
25
26
27
28
|
# File 'lib/dao/api/initializers.rb', line 24
def before_initializer(&block)
method_name = "before_initializer_#{ before_initializers.size }"
define_method(method_name, &block)
before_initializers.push(method_name)
end
|
.before_initializers ⇒ Object
20
21
22
|
# File 'lib/dao/api/initializers.rb', line 20
def before_initializers
initializers[:before]
end
|
.call(*args, &block) ⇒ Object
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
|
# File 'lib/dao/api/call.rb', line 21
def call(*args, &block)
options = Dao.options_for!(args)
path = Path.new(args.shift || paths.shift || raise(ArgumentError, "no path!"))
api = self
route = routes.add(path) if Route.like?(path)
doc = args.shift || options[:doc]
self.doc(doc) if doc
endpoint =
if options.key?(:alias)
aliased_path = Path.new(options[:alias])
endpoints[aliased_path] || raise(ArgumentError, "no such path #{ aliased_path }!")
else
Endpoint.new({
'api' => api,
'path' => path,
'route' => route,
'block' => block,
'doc' => docs.pop
})
end
endpoints[path] = endpoint
end
|
.description(*args) ⇒ Object
Also known as:
desc
70
71
72
|
# File 'lib/dao/api/call.rb', line 70
def description(*args)
doc(:doc => lines_for(*args))
end
|
.doc(*args) ⇒ Object
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/dao/api/call.rb', line 59
def doc(*args)
docs.push(Map(:doc => nil)) if docs.empty?
doc = docs.last
options = Dao.options_for!(args)
options[:doc] = lines_for(*args) if options.empty?
doc.update(options)
doc
end
|
.docs ⇒ Object
76
77
78
|
# File 'lib/dao/api/call.rb', line 76
def docs
state[:docs]
end
|
.endpoint(path, &block) ⇒ Object
49
50
51
52
|
# File 'lib/dao/api/call.rb', line 49
def endpoint(path, &block)
paths << path.to_s
class_eval(&block)
end
|
.endpoints ⇒ Object
alias_method(‘endpoint’, ‘call’)
55
56
57
|
# File 'lib/dao/api/call.rb', line 55
def endpoints
state[:endpoints]
end
|
.evaluate(&block) ⇒ Object
Also known as:
configure
35
36
37
38
|
# File 'lib/dao/api/dsl.rb', line 35
def evaluate(&block)
@dsl ||= DSL.new(self)
@dsl.evaluate(&block)
end
|
.index ⇒ Object
101
102
103
104
105
106
107
108
|
# File 'lib/dao/api/call.rb', line 101
def index
index = Map.new
index[:README] = readme
endpoints.each do |path, endpoint|
index[path] = endpoint.doc || {'description' => ''}
end
index
end
|
.initializers ⇒ Object
16
17
18
|
# File 'lib/dao/api/initializers.rb', line 16
def initializers
Initializers[self] ||= {:before => [], :after => []}
end
|
.lines_for(*args) ⇒ Object
93
94
95
|
# File 'lib/dao/api/call.rb', line 93
def lines_for(*args)
Dao.unindent(args.flatten.compact.join("\n")).split(/\n/)
end
|
.modes(*modes) ⇒ Object
5
6
7
8
9
|
# File 'lib/dao/api/modes.rb', line 5
def modes(*modes)
@modes ||= []
modes.flatten.compact.map{|mode| Api.add_mode(mode)} unless modes.empty?
@modes
end
|
.modes=(*modes) ⇒ Object
11
12
13
|
# File 'lib/dao/api/modes.rb', line 11
def modes=(*modes)
modes(*modes)
end
|
.new(*args, &block) ⇒ Object
7
8
9
10
11
12
13
14
|
# File 'lib/dao/api/initializers.rb', line 7
def new(*args, &block)
allocate.instance_eval do
before_initializers(*args, &block)
initialize(*args, &block)
after_initializers(*args, &block)
self
end
end
|
.paths ⇒ Object
80
81
82
|
# File 'lib/dao/api/call.rb', line 80
def paths
state[:paths]
end
|
.readme(*args) ⇒ Object
Also known as:
README
84
85
86
87
88
89
90
|
# File 'lib/dao/api/call.rb', line 84
def readme(*args)
if args.empty?
state[:README]
else
state[:README] = lines_for(args)
end
end
|
.readme=(readme) ⇒ Object
97
98
99
|
# File 'lib/dao/api/call.rb', line 97
def readme=(readme)
self.readme = readme.to_s
end
|
.routes ⇒ Object
5
6
7
|
# File 'lib/dao/api/routes.rb', line 5
def routes
@routes ||= Route::List.new
end
|
.state ⇒ Object
11
12
13
14
15
16
17
18
19
|
# File 'lib/dao/api/call.rb', line 11
def state
@state ||= {
:endpoints => Map.new,
:blocks => {},
:README => [],
:docs => [],
:paths => [],
}
end
|
.superclasses ⇒ Object
38
39
40
|
# File 'lib/dao/api/initializers.rb', line 38
def superclasses
@superclasses ||= ancestors.select{|ancestor| ancestor <= Dao::Api}
end
|
.unload! ⇒ Object
7
8
9
|
# File 'lib/dao/api/call.rb', line 7
def unload!
state.clear
end
|
Instance Method Details
#after_initialize(*args, &block) ⇒ Object
68
69
70
|
# File 'lib/dao/api/initializers.rb', line 68
def after_initialize(*args, &block)
:hook
end
|
#after_initializers(*args, &block) ⇒ Object
60
61
62
|
# File 'lib/dao/api/initializers.rb', line 60
def after_initializers(*args, &block)
run_initializers(:after, *args, &block)
end
|
#before_initialize(*args, &block) ⇒ Object
64
65
66
|
# File 'lib/dao/api/initializers.rb', line 64
def before_initialize(*args, &block)
:hook
end
|
#before_initializers(*args, &block) ⇒ Object
56
57
58
|
# File 'lib/dao/api/initializers.rb', line 56
def before_initializers(*args, &block)
run_initializers(:before, *args, &block)
end
|
#call(path = '/index', params = {}, options = {}) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/dao/api/call.rb', line 117
def call(path = '/index', params = {}, options = {})
api = self
path = Path.new(path)
endpoint = endpoints[path] route = nil
unless endpoint
route = route_for(path)
endpoint = endpoints[route]
end
unless endpoint
return index if path == '/index'
raise(NameError, "NO SUCH INTERFACE: #{ path }")
end
if route
params.update(route.params_for(path))
path = route.path_for(params)
else
if Route.like?(path)
route = Route.new(path)
path = route.path_for(params)
else
route = path
end
end
context = Context.for(api, path, route, endpoint, params, options)
callstack(context) do
catching(:result) do
context.call()
end
end
context.result
end
|
#callstack(context = nil, &block) ⇒ Object
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/dao/api/call.rb', line 183
def callstack(context = nil, &block)
@callstack ||= []
if block and context
begin
@callstack.push(context)
return block.call()
ensure
@callstack.pop
end
else
@callstack
end
end
|
#catching(label = :result, &block) ⇒ Object
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
# File 'lib/dao/api/call.rb', line 206
def catching(label = :result, &block)
@catching ||= []
if block
begin
@catching.push(label)
catch(label, &block)
rescue Dao::Validations::Error
nil
ensure
@catching.pop
end
else
@catching.last
end
end
|
#catching? ⇒ Boolean
231
232
233
|
# File 'lib/dao/api/call.rb', line 231
def catching?
catching
end
|
#catching_results(&block) ⇒ Object
227
228
229
|
# File 'lib/dao/api/call.rb', line 227
def catching_results(&block)
catching(:result, &block)
end
|
#catching_results? ⇒ Boolean
235
236
237
|
# File 'lib/dao/api/call.rb', line 235
def catching_results?
catching == :result
end
|
#context ⇒ Object
198
199
200
|
# File 'lib/dao/api/call.rb', line 198
def context
callstack.last || raise('no context!')
end
|
#context? ⇒ Boolean
202
203
204
|
# File 'lib/dao/api/call.rb', line 202
def context?
!!callstack.last
end
|
#data(*args) ⇒ Object
263
264
265
266
|
# File 'lib/dao/api/call.rb', line 263
def data(*args)
context.data.replace(*args) unless args.empty?
context.data
end
|
#data!(*args) ⇒ Object
267
268
269
270
|
# File 'lib/dao/api/call.rb', line 267
def data!(*args)
data(*args)
return!
end
|
#endpoints ⇒ Object
278
279
280
|
# File 'lib/dao/api/call.rb', line 278
def endpoints
self.class.endpoints
end
|
#index ⇒ Object
274
275
276
|
# File 'lib/dao/api/call.rb', line 274
def index
self.class.index
end
|
#mode(*args, &block) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/dao/api/modes.rb', line 60
def mode(*args, &block)
@mode ||= Mode.default
if args.empty? and block.nil?
@mode
else
if block
mode = self.mode
self.mode = args.shift
begin
return(instance_eval(&block))
ensure
self.mode = mode
end
else
self.mode = args.shift
return(self)
end
end
end
|
#mode=(mode) ⇒ Object
56
57
58
|
# File 'lib/dao/api/modes.rb', line 56
def mode=(mode)
@mode = Mode.for(mode)
end
|
#mode?(mode, &block) ⇒ Boolean
81
82
83
84
85
86
87
88
|
# File 'lib/dao/api/modes.rb', line 81
def mode?(mode, &block)
condition = mode.case_of?(self.mode)
if block.nil?
condition
else
send(mode, &block) if condition
end
end
|
#on(mode, *args, &block) ⇒ Object
52
53
54
|
# File 'lib/dao/api/modes.rb', line 52
def on(mode, *args, &block)
send(mode.to_s.downcase, *args, &block)
end
|
#parameter(*args, &block) ⇒ Object
Also known as:
param
immediate parameter parsing support
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
# File 'lib/dao/api/call.rb', line 288
def parameter(*args, &block)
options = Map.options_for!(args)
keys = args + Array(options[:keys]) + Array(options[:or])
raise(ArgumentError, 'no keys') if keys.empty?
blank = Object.new.freeze
value = blank
keys.each do |key|
if params.has?(key)
value = params.get(key)
break unless value.to_s.strip.empty?
end
end
if value == blank
message =
case options[:error]
when nil, false
nil
when true
which = keys.map{|key| Array(key).join('.')}.join(' or ')
"#{ which } (parameter is blank)"
else
message = options[:error].to_s
end
errors.add(message) if message
status(options[:status]) if options[:status]
return! if options[:return!]
end
value == blank ? nil : value
end
|
#parameter!(*args, &block) ⇒ Object
Also known as:
param!
326
327
328
329
330
331
332
333
|
# File 'lib/dao/api/call.rb', line 326
def parameter!(*args, &block)
options = args.last.is_a?(Hash) ? Map.for(args.pop) : Map.new
args.push(options)
options[:error] = true unless options.has_key?(:error)
options[:return!] = true unless options.has_key?(:return!)
options[:status] = 412 unless options.has_key?(:status)
parameter(*args, &block)
end
|
#respond_to?(*args) ⇒ Boolean
282
283
284
|
# File 'lib/dao/api/call.rb', line 282
def respond_to?(*args)
super(*args) || super(Path.absolute_path_for(*args))
end
|
#return!(*value) ⇒ Object
223
224
225
|
# File 'lib/dao/api/call.rb', line 223
def return!(*value)
throw(:result, *value)
end
|
#route?(path) ⇒ Boolean
will an endpoint route to a endpoint?
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/dao/api/call.rb', line 161
def route?(path)
path = Path.new(path)
endpoint = endpoints[path]
route = nil
unless endpoint
route = route_for(path)
endpoint = endpoints[route]
end
endpoint
end
|
#route_for(*args) ⇒ Object
177
178
179
|
# File 'lib/dao/api/call.rb', line 177
def route_for(*args)
self.class.routes.match(*args)
end
|
#run_initializers(which, *args, &block) ⇒ Object
47
48
49
50
51
52
53
54
|
# File 'lib/dao/api/initializers.rb', line 47
def run_initializers(which, *args, &block)
superclasses.each do |superclass|
superclass.send("#{ which }_initializers").each do |method_name|
send(method_name, *args, &block)
end
end
send("#{ which }_initialize", *args, &block)
end
|
#status(*args) ⇒ Object
254
255
256
257
|
# File 'lib/dao/api/call.rb', line 254
def status(*args)
context.status.update(*args) unless args.empty?
context.status
end
|
#status!(*args) ⇒ Object
258
259
260
261
|
# File 'lib/dao/api/call.rb', line 258
def status!(*args)
status.update(*args)
return!
end
|
#superclasses ⇒ Object
43
44
45
|
# File 'lib/dao/api/initializers.rb', line 43
def superclasses
@superclasses ||= self.class.superclasses
end
|