Class: Dynamicloud::API::DynamicProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamic_api.rb

Overview

This class has two attributes CSK and ACI keys. This information is provided at moment the registration in Dynamicloud.

Author:

  • Eleazar Gomez

Since:

  • 8/25/15

Version:

  • 1.0.0

Instance Method Summary collapse

Constructor Details

#initialize(credential) ⇒ DynamicProvider

Returns a new instance of DynamicProvider.

Since:

  • 8/25/15



301
302
303
# File 'lib/dynamic_api.rb', line 301

def initialize(credential)
  @credential = credential
end

Instance Method Details

#create_query(mid) ⇒ Object

Will create a RecordQuery and sets to this provider

Parameters:

  • mid

    model id to use to execute operations

Returns:

  • this Dynamicloud::API::RecordQuery instance

Since:

  • 8/25/15



413
414
415
416
417
418
# File 'lib/dynamic_api.rb', line 413

def create_query(mid)
  query = Dynamicloud::API::RecordQuery.new mid
  query.set_credentials @credential

  query
end

#delete(query, mid) ⇒ Object

Executes a delete using query as a selection

Parameters:

  • query

    selection

  • mid

    model id

Since:

  • 8/25/15



543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# File 'lib/dynamic_api.rb', line 543

def delete(query, mid)
  selection = Dynamicloud::API::DynamicloudHelper.build_string(query.get_conditions, nil, nil, nil)

  url = Configuration::PROPERTIES.get_property :url
  url_get_records = Configuration::PROPERTIES.get_property :url_delete_selection

  url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
  url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
  url_get_records = url_get_records.gsub '{mid}', mid.to_s

  params = {
      :selection => selection
  }

  response = DynamicService::ServiceCaller.call_service url + url_get_records, params, 'post'

  json = JSON.parse(response)
  unless json['status'] == 200
    raise json['message']
  end
end

#delete_record(mid, rid) ⇒ Object

This method will call a delete operation in DynamiCloud servers using model id and Record id

Parameters:

  • mid

    model id

  • rid

    record id

Since:

  • 8/25/15



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/dynamic_api.rb', line 393

def delete_record(mid, rid)
  url = Configuration::PROPERTIES.get_property :url
  url_get_records = Configuration::PROPERTIES.get_property :url_delete_record

  url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
  url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
  url_get_records = url_get_records.gsub '{mid}', mid.to_s
  url_get_records = url_get_records.gsub '{rid}', rid.to_s

  response = DynamicService::ServiceCaller.call_service url + url_get_records, {}, 'delete'

  json = JSON.parse(response)
  unless json['status'] == 200
    raise json['message']
  end
end

#load_fields(mid) ⇒ Object

Loads all model’s fields according ModelID

Parameters:

  • mid

    model id

Returns:

  • list of model’s fields.

Since:

  • 8/25/15



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/dynamic_api.rb', line 477

def load_fields(mid)
  url = Configuration::PROPERTIES.get_property :url
  url_get_records = Configuration::PROPERTIES.get_property :url_get_fields

  url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
  url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
  url_get_records = url_get_records.gsub '{mid}', mid.to_s

  response = DynamicService::ServiceCaller.call_service url + url_get_records, {}, 'get'

  json = JSON.parse(response)
  unless json['status'] == 200
    raise json['message']
  end

  fields = []
  fs = json['fields']
  fs.each do |key, jf|
    field = Dynamicloud::API::Model::RecordField.new
    field.id = jf['id'].to_i
    field.identifier = jf['identifier']
    field.label = jf['label']
    field.comment = jf['comment']
    field.uniqueness = jf['uniqueness']
    field.required = jf['required']
    field.type = Dynamicloud::API::Model::RecordFieldType.get_field_type jf['field_type'].to_i
    field.items = Dynamicloud::API::DynamicloudHelper.build_items jf['items']
    field.mid = mid.to_i

    fields.push field
  end

  fields
end

#load_model(mid) ⇒ Object

Gets model record information from DynamiCloud servers.

Parameters:

  • mid

    model id in DynamiClod servers

Returns:

  • RecordModel object

Since:

  • 8/25/15



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/dynamic_api.rb', line 423

def load_model(mid)
  url = Configuration::PROPERTIES.get_property :url
  url_get_records = Configuration::PROPERTIES.get_property :url_get_model_info

  url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
  url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
  url_get_records = url_get_records.gsub '{mid}', mid.to_s

  response = DynamicService::ServiceCaller.call_service url + url_get_records, {}, 'get'

  json = JSON.parse(response)
  unless json['status'] == 200
    raise json['message']
  end

  model = Dynamicloud::API::Model::RecordModel.new mid
  model.name = json['name']
  model.description = json['description']

  model
end

#load_modelsObject

Loads all models related to CSK and ACI keys in Dynamicloud servers

Returns:

  • list of models

Since:

  • 8/25/15



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/dynamic_api.rb', line 447

def load_models
  url = Configuration::PROPERTIES.get_property :url
  url_get_records = Configuration::PROPERTIES.get_property :url_get_models

  url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
  url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])

  response = DynamicService::ServiceCaller.call_service url + url_get_records, {}, 'get'

  json = JSON.parse(response)
  unless json['status'] == 200
    raise json['message']
  end

  models = []
  array = json['models']
  array.each do |item|
    model = Dynamicloud::API::Model::RecordModel.new item['id'].to_i
    model.name = item['name']
    model.description = item['description']

    models.push model
  end

  models
end

#load_record(rid, mid) ⇒ Object

This method will load a record using rid and will instantiate a hash with attributes bound to Model’s fields.

Parameters:

  • rid

    record id

  • mid

    model id

Returns:

  • a hash with record information

Since:

  • 8/25/15



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/dynamic_api.rb', line 309

def load_record(rid, mid)
  url = Configuration::PROPERTIES.get_property :url
  url_get_records = Configuration::PROPERTIES.get_property :url_get_record_info

  url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
  url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
  url_get_records = url_get_records.gsub '{mid}', mid.to_s
  url_get_records = url_get_records.gsub '{rid}', rid.to_s

  response = DynamicService::ServiceCaller.call_service url + url_get_records, {}, 'get'

  json = JSON.parse(response)
  record = json['record']

  unless record
    raise 'Record key doesn\'t present in response from Dynamicloud server.'
  end

  Dynamicloud::API::DynamicloudHelper.normalize_record record
end

#save_record(mid, data) ⇒ Object

This method will call a save operation in DynamiCloud servers using model and BoundInstance object

Parameters:

  • mid

    model id

  • data

    all data needed to save a record in model (mid)

Since:

  • 8/25/15



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/dynamic_api.rb', line 365

def save_record(mid, data)
  url = Configuration::PROPERTIES.get_property :url
  url_get_records = Configuration::PROPERTIES.get_property :url_save_record

  url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
  url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
  url_get_records = url_get_records.gsub '{mid}', mid.to_s

  params = {
      :fields => Dynamicloud::API::DynamicloudHelper.build_fields_json(data)
  }

  response = DynamicService::ServiceCaller.call_service url + url_get_records, params, 'post'

  json = JSON.parse(response)
  unless json['status'] == 200
    raise json['message']
  end

  data['rid'] = json['rid']

  data
end

#update(query, data = {}) ⇒ Object

Executes an update using query as a selection and data with values Dynamicloud will normalize the key pair values. That is, will be used field identifiers only.

Parameters:

  • data (defaults to: {})

    data that will be sent to Dynamicloud servers

  • query

    selection

Since:

  • 8/25/15



516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/dynamic_api.rb', line 516

def update(query, data = {})
  selection = Dynamicloud::API::DynamicloudHelper.build_string(query.get_conditions, nil, nil, nil)
  fields = '{"updates": ' + Dynamicloud::API::DynamicloudHelper.build_fields_json(data) + '}'

  url = Configuration::PROPERTIES.get_property :url
  url_get_records = Configuration::PROPERTIES.get_property :url_update_selection

  url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
  url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
  url_get_records = url_get_records.gsub '{mid}', query.get_model_id.to_s

  params = {
      :fields => fields,
      :selection => selection
  }

  response = DynamicService::ServiceCaller.call_service url + url_get_records, params, 'post'

  json = JSON.parse(response)
  unless json['status'] == 200
    raise json['message']
  end
end

#update_record(mid, data) ⇒ Object

This method will call an update operation in Dynamicloud servers using model and data object

Parameters:

  • mid

    model id

  • data

    this hash should contain a key rid (RecordId) otherwise an error will be thrown

Since:

  • 8/25/15



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/dynamic_api.rb', line 334

def update_record(mid, data)
  rid = data['rid']

  unless rid
    raise "rid attribute isn't present in hash data."
  end

  url = Configuration::PROPERTIES.get_property :url
  url_get_records = Configuration::PROPERTIES.get_property :url_update_record

  url_get_records = url_get_records.gsub '{csk}', URI::encode(@credential[:csk])
  url_get_records = url_get_records.gsub '{aci}', URI::encode(@credential[:aci])
  url_get_records = url_get_records.gsub '{mid}', mid.to_s
  url_get_records = url_get_records.gsub '{rid}', rid.to_s

  params = {
      :fields => Dynamicloud::API::DynamicloudHelper.build_fields_json(data)
  }

  response = DynamicService::ServiceCaller.call_service url + url_get_records, params, 'post'

  json = JSON.parse(response)
  unless json['status'] == 200
    raise json['message']
  end
end