Class: Dao::Api

Inherits:
Object
  • 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

Methods included from Validations

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_initializersObject



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_initializersObject



20
21
22
# File 'lib/dao/api/initializers.rb', line 20

def before_initializers
  initializers[:before]
end

.call(*args, &block) ⇒ Object



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/call.rb', line 20

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



64
65
66
# File 'lib/dao/api/call.rb', line 64

def description(*args)
  doc(:doc => lines_for(*args))
end

.doc(*args) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/dao/api/call.rb', line 53

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

.docsObject



70
71
72
# File 'lib/dao/api/call.rb', line 70

def docs
  state[:docs]
end

.endpointObject



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
# File 'lib/dao/api/call.rb', line 47

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

.endpointsObject



49
50
51
# File 'lib/dao/api/call.rb', line 49

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(api=self)
  @dsl.evaluate(&block)
end

.indexObject



91
92
93
94
95
96
97
98
# File 'lib/dao/api/call.rb', line 91

def index
  index = Map.new
  index[:README] = readme
  endpoints.each do |path, endpoint|
    index[path] = endpoint.doc || {'description' => ''}
  end
  index
end

.initializersObject



16
17
18
# File 'lib/dao/api/initializers.rb', line 16

def initializers
  Initializers[self] ||= {:before => [], :after => []}
end

.lines_for(*args) ⇒ Object



83
84
85
# File 'lib/dao/api/call.rb', line 83

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

.readme(*args) ⇒ Object Also known as: README



74
75
76
77
78
79
80
# File 'lib/dao/api/call.rb', line 74

def readme(*args)
  if args.empty?
    state[:README]
  else
    state[:README] = lines_for(args)
  end
end

.readme=(readme) ⇒ Object



87
88
89
# File 'lib/dao/api/call.rb', line 87

def readme=(readme)
  self.readme = readme.to_s
end

.routesObject



5
6
7
# File 'lib/dao/api/routes.rb', line 5

def routes
  @routes ||= Route::List.new
end

.stateObject



11
12
13
14
15
16
17
18
# File 'lib/dao/api/call.rb', line 11

def state
  @state ||= {
    :endpoints => Map.new,
    :blocks => {},
    :README => [],
    :docs => []
  }
end

.superclassesObject



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

call support



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
147
# File 'lib/dao/api/call.rb', line 107

def call(path = '/index', params = {}, options = {})
  api = self
  path = Path.new(path)
  endpoint = endpoints[path]  ### endpoints.by_path(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

context stack support



173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/dao/api/call.rb', line 173

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



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/dao/api/call.rb', line 196

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

Returns:

  • (Boolean)


221
222
223
# File 'lib/dao/api/call.rb', line 221

def catching?
  catching
end

#catching_results(&block) ⇒ Object



217
218
219
# File 'lib/dao/api/call.rb', line 217

def catching_results(&block)
  catching(:result, &block)
end

#catching_results?Boolean

Returns:

  • (Boolean)


225
226
227
# File 'lib/dao/api/call.rb', line 225

def catching_results?
  catching == :result
end

#contextObject



188
189
190
# File 'lib/dao/api/call.rb', line 188

def context
  callstack.last || raise('no context!')
end

#context?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/dao/api/call.rb', line 192

def context?
  !!callstack.last
end

#data(*args) ⇒ Object



252
253
254
255
# File 'lib/dao/api/call.rb', line 252

def data(*args)
  context.data.replace(*args) unless args.empty?
  context.data
end

#data!(*args) ⇒ Object



256
257
258
259
# File 'lib/dao/api/call.rb', line 256

def data!(*args)
  data(*args)
  return!
end

#endpointsObject



267
268
269
# File 'lib/dao/api/call.rb', line 267

def endpoints
  self.class.endpoints
end

#indexObject

misc



263
264
265
# File 'lib/dao/api/call.rb', line 263

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

Returns:

  • (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

Raises:

  • (ArgumentError)


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
312
# File 'lib/dao/api/call.rb', line 277

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!



315
316
317
318
319
320
321
322
# File 'lib/dao/api/call.rb', line 315

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

Returns:

  • (Boolean)


271
272
273
# File 'lib/dao/api/call.rb', line 271

def respond_to?(*args)
  super(*args) || super(Path.absolute_path_for(*args))
end

#return!(*value) ⇒ Object



213
214
215
# File 'lib/dao/api/call.rb', line 213

def return!(*value)
  throw(:result, *value)
end

#route?(path) ⇒ Boolean

will an endpoint route to a endpoint?

Returns:

  • (Boolean)


151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/dao/api/call.rb', line 151

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

lookup a route



167
168
169
# File 'lib/dao/api/call.rb', line 167

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



243
244
245
246
# File 'lib/dao/api/call.rb', line 243

def status(*args)
  context.status.update(*args) unless args.empty?
  context.status
end

#status!(*args) ⇒ Object



247
248
249
250
# File 'lib/dao/api/call.rb', line 247

def status!(*args)
  status.update(*args)
  return!
end

#superclassesObject



43
44
45
# File 'lib/dao/api/initializers.rb', line 43

def superclasses
  @superclasses ||= self.class.superclasses
end