Class: Dao::Api
- Inherits:
-
Object
show all
- Includes:
- Validations
- Defined in:
- lib/dao/api.rb,
lib/dao/api/dsl.rb,
lib/dao/api/modes.rb,
lib/dao/api/routes.rb,
lib/dao/api/endpoints.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
-
#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, included
Class Method Details
.add_mode(mode) ⇒ Object
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
|
# File 'lib/dao/api/modes.rb', line 14
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
def #{ mode }?(&block)
mode?(#{ mode.inspect }, &block)
end
__
modes.push(mode)
mode
else
false
end
end
|
.after_initializer(&block) ⇒ Object
33
34
35
|
# File 'lib/dao/api/initializers.rb', line 33
def after_initializer(&block)
after_initializers.push(block)
end
|
.after_initializers ⇒ Object
29
30
31
|
# File 'lib/dao/api/initializers.rb', line 29
def after_initializers
initializers[:after]
end
|
.before_initializer(&block) ⇒ Object
23
24
25
26
27
|
# File 'lib/dao/api/initializers.rb', line 23
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
19
20
21
|
# File 'lib/dao/api/initializers.rb', line 19
def before_initializers
initializers[:before]
end
|
.before_remove_const ⇒ Object
6
7
8
|
# File 'lib/dao/rails.rb', line 6
def Api.before_remove_const
unload!
end
|
.call(*args, &block) ⇒ Object
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
|
# File 'lib/dao/api/endpoints.rb', line 19
def call(*args, &block)
options = Dao.options_for!(args)
path = Path.new(args.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
63
64
65
|
# File 'lib/dao/api/endpoints.rb', line 63
def description(*args)
doc(:doc => lines_for(*args))
end
|
.doc(*args) ⇒ Object
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/dao/api/endpoints.rb', line 52
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
69
70
71
|
# File 'lib/dao/api/endpoints.rb', line 69
def docs
state[:docs]
end
|
.endpoint ⇒ Object
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
|
# File 'lib/dao/api/endpoints.rb', line 46
def call(*args, &block)
options = Dao.options_for!(args)
path = Path.new(args.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
|
.endpoints ⇒ Object
48
49
50
|
# File 'lib/dao/api/endpoints.rb', line 48
def endpoints
state[:endpoints]
end
|
.evaluate(&block) ⇒ Object
34
35
36
37
|
# File 'lib/dao/api/dsl.rb', line 34
def evaluate(&block)
@dsl ||= DSL.new(api=self)
@dsl.evaluate(&block)
end
|
.index ⇒ Object
90
91
92
93
94
95
96
97
|
# File 'lib/dao/api/endpoints.rb', line 90
def index
index = Map.new
index[:README] = readme
endpoints.each do |path, endpoint|
index[path] = endpoint.doc || {'description' => ''}
end
index
end
|
.initializers ⇒ Object
15
16
17
|
# File 'lib/dao/api/initializers.rb', line 15
def initializers
Initializers[self] ||= {:before => [], :after => []}
end
|
.lines_for(*args) ⇒ Object
82
83
84
|
# File 'lib/dao/api/endpoints.rb', line 82
def lines_for(*args)
Dao.unindent(args.flatten.compact.join("\n")).split(/\n/)
end
|
.modes(*modes) ⇒ Object
4
5
6
7
8
|
# File 'lib/dao/api/modes.rb', line 4
def modes(*modes)
@modes ||= []
modes.flatten.compact.map{|mode| Api.add_mode(mode)} unless modes.empty?
@modes
end
|
.modes=(*modes) ⇒ Object
10
11
12
|
# File 'lib/dao/api/modes.rb', line 10
def modes=(*modes)
modes(*modes)
end
|
.new(*args, &block) ⇒ Object
6
7
8
9
10
11
12
13
|
# File 'lib/dao/api/initializers.rb', line 6
def new(*args, &block)
allocate.instance_eval do
before_initializers(*args, &block)
initialize(*args, &block)
after_initializers(*args, &block)
self
end
end
|
.readme(*args) ⇒ Object
Also known as:
README
73
74
75
76
77
78
79
|
# File 'lib/dao/api/endpoints.rb', line 73
def readme(*args)
if args.empty?
state[:README]
else
state[:README] = lines_for(args)
end
end
|
.readme=(readme) ⇒ Object
86
87
88
|
# File 'lib/dao/api/endpoints.rb', line 86
def readme=(readme)
self.readme = readme.to_s
end
|
.routes ⇒ Object
4
5
6
|
# File 'lib/dao/api/routes.rb', line 4
def routes
@routes ||= Route::List.new
end
|
.state ⇒ Object
10
11
12
13
14
15
16
17
|
# File 'lib/dao/api/endpoints.rb', line 10
def state
@state ||= {
:endpoints => Map.new,
:blocks => {},
:README => [],
:docs => []
}
end
|
.superclasses ⇒ Object
37
38
39
|
# File 'lib/dao/api/initializers.rb', line 37
def superclasses
@superclasses ||= ancestors.select{|ancestor| ancestor <= Dao::Api}
end
|
.unload! ⇒ Object
6
7
8
|
# File 'lib/dao/api/endpoints.rb', line 6
def unload!
state.clear
end
|
Instance Method Details
#after_initialize(*args, &block) ⇒ Object
67
68
69
|
# File 'lib/dao/api/initializers.rb', line 67
def after_initialize(*args, &block)
:hook
end
|
#after_initializers(*args, &block) ⇒ Object
59
60
61
|
# File 'lib/dao/api/initializers.rb', line 59
def after_initializers(*args, &block)
run_initializers(:after, *args, &block)
end
|
#before_initialize(*args, &block) ⇒ Object
63
64
65
|
# File 'lib/dao/api/initializers.rb', line 63
def before_initialize(*args, &block)
:hook
end
|
#before_initializers(*args, &block) ⇒ Object
55
56
57
|
# File 'lib/dao/api/initializers.rb', line 55
def before_initializers(*args, &block)
run_initializers(:before, *args, &block)
end
|
#call(path = '/index', params = {}, options = {}) ⇒ Object
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/dao/api/endpoints.rb', line 106
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/dao/api/endpoints.rb', line 172
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
# File 'lib/dao/api/endpoints.rb', line 195
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
220
221
222
|
# File 'lib/dao/api/endpoints.rb', line 220
def catching?
catching
end
|
#catching_results(&block) ⇒ Object
216
217
218
|
# File 'lib/dao/api/endpoints.rb', line 216
def catching_results(&block)
catching(:result, &block)
end
|
#catching_results? ⇒ Boolean
224
225
226
|
# File 'lib/dao/api/endpoints.rb', line 224
def catching_results?
catching == :result
end
|
#context ⇒ Object
187
188
189
|
# File 'lib/dao/api/endpoints.rb', line 187
def context
callstack.last || raise('no context!')
end
|
#context? ⇒ Boolean
191
192
193
|
# File 'lib/dao/api/endpoints.rb', line 191
def context?
!!callstack.last
end
|
#data(*args) ⇒ Object
251
252
253
254
|
# File 'lib/dao/api/endpoints.rb', line 251
def data(*args)
context.data.replace(*args) unless args.empty?
context.data
end
|
#data!(*args) ⇒ Object
255
256
257
258
|
# File 'lib/dao/api/endpoints.rb', line 255
def data!(*args)
data(*args)
return!
end
|
#endpoints ⇒ Object
266
267
268
|
# File 'lib/dao/api/endpoints.rb', line 266
def endpoints
self.class.endpoints
end
|
#index ⇒ Object
262
263
264
|
# File 'lib/dao/api/endpoints.rb', line 262
def index
self.class.index
end
|
#mode(*args, &block) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/dao/api/modes.rb', line 53
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
49
50
51
|
# File 'lib/dao/api/modes.rb', line 49
def mode=(mode)
@mode = Mode.for(mode)
end
|
#mode?(mode, &block) ⇒ Boolean
74
75
76
77
78
79
80
81
|
# File 'lib/dao/api/modes.rb', line 74
def mode?(mode, &block)
condition = mode.case_of?(self.mode)
if block.nil?
condition
else
send(mode, &block) if condition
end
end
|
#parameter(*args, &block) ⇒ Object
Also known as:
param
immediate parameter parsing support
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
# File 'lib/dao/api/endpoints.rb', line 276
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!
314
315
316
317
318
319
320
321
|
# File 'lib/dao/api/endpoints.rb', line 314
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
270
271
272
|
# File 'lib/dao/api/endpoints.rb', line 270
def respond_to?(*args)
super(*args) || super(Path.absolute_path_for(*args))
end
|
#return!(*value) ⇒ Object
212
213
214
|
# File 'lib/dao/api/endpoints.rb', line 212
def return!(*value)
throw(:result, *value)
end
|
#route?(path) ⇒ Boolean
will an endpoint route to a endpoint?
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/dao/api/endpoints.rb', line 150
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
166
167
168
|
# File 'lib/dao/api/endpoints.rb', line 166
def route_for(*args)
self.class.routes.match(*args)
end
|
#run_initializers(which, *args, &block) ⇒ Object
46
47
48
49
50
51
52
53
|
# File 'lib/dao/api/initializers.rb', line 46
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
242
243
244
245
|
# File 'lib/dao/api/endpoints.rb', line 242
def status(*args)
context.status.update(*args) unless args.empty?
context.status
end
|
#status!(*args) ⇒ Object
246
247
248
249
|
# File 'lib/dao/api/endpoints.rb', line 246
def status!(*args)
status.update(*args)
return!
end
|
#superclasses ⇒ Object
42
43
44
|
# File 'lib/dao/api/initializers.rb', line 42
def superclasses
@superclasses ||= self.class.superclasses
end
|