203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
|
# File 'lib/arcus/api.rb', line 203
def self.configure
Arcus.log = Logger.new(STDOUT)
Arcus.log.level = Logger::DEBUG
Arcus.log.formatter = proc do |severity, datetime, progname, msg|
"#{severity} - #{datetime}: #{msg}\n"
end
yield(self.settings)
self.settings.api_xml || (raise ArgumentError, "api_xml file: commands.xml file location required")
self.settings.api_uri || (raise ArgumentError, "api_uri: URI of API required")
api_config = self.load_config_file(self.settings.api_xml)
api_config["commands"]["command"].each do |c|
command_name = c["name"]
command_desc = c["description"]
command_async = c["isAsync"] == "true" ? true : false
action, target = parse_action(command_name)
if c["request"] != nil
all_args = c["request"]["arg"] || []
else
all_args = []
end
if all_args.is_a?(Hash)
all_args = [all_args]
end
all_args.push({"name" => "response", "description" => "valid response types are yaml, xml, prettyxml, json, prettyjson (json is default)", "required" => "false"})
required_args = all_args.select { |a| a["required"] == "true" }
optional_args = all_args.select { |a| a["required"] == "false" }
def self.class_exists?(class_name)
c = self.const_get(class_name)
return c.is_a?(Class)
rescue NameError
return false
end
def self.get_class(class_name)
self.const_get(class_name)
end
def self.create_class(class_name, superclass, &block)
c = Class.new superclass, &block
self.const_set class_name, c
c
end
if !self.class_exists?(target)
target_clazz = self.create_class(target, Target) do
cattr_accessor :actions
self.actions = []
end
@@targets << target_clazz
else
target_clazz = self.get_class(target)
end
target_clazz.class_eval do
if !class_exists?(action.capitalize)
action_clazz = create_class(action.capitalize, Action) do
cattr_accessor :name, :description, :is_async,
:required_args, :optional_args,
:action_name, :sync
self.name = command_name
self.description = command_desc
self.action_name = action
self.required_args = required_args
self.optional_args = optional_args
self.is_async = command_async
self.sync = false
def prepare!(params = {}, callbacks = {})
check_args(params, all_names, required_names)
prepare(params, callbacks)
end
def prepare(params = {}, callbacks = {})
params[:response] ||= Arcus::Api.settings.default_response
Request.new(self.name, params, callbacks)
end
end
target_clazz.actions << action_clazz
target_clazz.instance_eval do
define_method(action.to_sym) do |params = {}, callbacks = {}|
action_clazz.new.prepare(params, callbacks)
end
define_method("#{action}!".to_sym) do |params = {}, callbacks = {}|
action_clazz.new.prepare(params, callbacks)
end
end
end
end
end
end
|