Class: Dynamicloud::API::DynamicloudHelper

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

Overview

This is a class with utility methods

Author:

  • Eleazar Gomez

Since:

  • 8/26/15

Version:

  • 1.0.0

Class Method Summary collapse

Class Method Details

.build_fields_json(data) ⇒ Object

Builds a compatible string to update a record. This method will get field name and its value form data hash.

Parameters:

  • instance

    Object where data is extracted

Returns:

  • compatible string

Since:

  • 8/26/15



604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# File 'lib/dynamic_api.rb', line 604

def self.build_fields_json(data)
  result = data.clone
  data.each do |key, value|
    if value.respond_to?(:each)
      array = ''
      value.each do |item|
        if item
          array = array + (array == '' ? '' : ',') + item.to_s
        end
      end
      result[key] = array
    end
  end

  result.to_json
end

.build_items(array) ⇒ Object

Builds an array of RecordFieldItems according JSONArray

Parameters:

  • array

    JSONArray with pair value, text.

Since:

  • 8/26/15



623
624
625
626
627
628
629
630
631
632
633
634
635
636
# File 'lib/dynamic_api.rb', line 623

def self.build_items(array)
  items = []
  if array.length > 0
    array.each do |item|
      ri = Dynamicloud::API::Model::RecordFieldItem.new
      ri.text = item['text']
      ri.value = item['value']

      items.push ri
    end
  end

  items
end

.build_join_tag(joins) ⇒ Object

This method builds the tag joins as follows: i.e: “joins”: [ { “type”: “full”, “alias”: “user”, “target”: “3456789”, “on”: { “user.id” : “languages.id” } } ]

Parameters:

  • joins

    list of join clauses

Returns:

  • the representation of a join tag.

Since:

  • 8/26/15



690
691
692
693
694
695
696
697
698
699
700
701
702
703
# File 'lib/dynamic_api.rb', line 690

def self.build_join_tag(joins)
  tag = '"joins": ['

  unless joins.nil?
    first_time = true
    joins.each do |clause|
      tag += (first_time ? '' : ', ') + clause.to_record_string(Dynamicloud::API::Criteria::Condition::ROOT)

      first_time = false
    end
  end

  return tag + ']'
end

.build_projection(projection) ⇒ Object

Build a compatible string using projection

Returns:

  • string using projection

Since:

  • 8/26/15



671
672
673
674
675
676
677
678
679
680
681
682
683
# File 'lib/dynamic_api.rb', line 671

def self.build_projection(projection)
  unless (projection) && (projection.length > 0)
    return ''
  end

  columns = '"columns": ['
  cols = ''
  projection.each do |field|
    cols = cols + (cols == '' ? '' : ',') + '"' + field + '"'
  end

  columns + cols + ']'
end

.build_record(jr) ⇒ Object

Builds the record hash with data from jr JSON object

Since:

  • 8/26/15



737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
# File 'lib/dynamic_api.rb', line 737

def self.build_record(jr)
  record = {}

  jr.each do |key, value|
    if value.is_a?(Hash)
      value.each do |k, v|
        if v.respond_to?(:each)
          values = []
          v.each do |item|
            values.push(item.to_s)
          end

          record[key] = values
        else
          record[key] = value
        end

        break
      end
    else
      record[key] = value
    end
  end

  record
end

.build_record_results(response) ⇒ Object

This utility will build a RecordResults object

Parameters:

  • response

    ServiceResponse from Dynamicloud servers

Since:

  • 8/26/15



707
708
709
710
711
712
713
714
715
716
717
718
719
# File 'lib/dynamic_api.rb', line 707

def self.build_record_results(response)
  results = Dynamicloud::API::RecordResults.new

  json = JSON.parse(response)

  data = json['records']

  results.total_records = data['total']
  results.fast_returned_size = data['size']
  results.records = get_record_list(data)

  results
end

.build_string(conditions, group_by, order_by, projection, aliass = nil, joins = []) ⇒ Object

Builds a compatible String to use in service executions

Returns:

  • compatible String

Since:

  • 8/26/15



640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
# File 'lib/dynamic_api.rb', line 640

def self.build_string(conditions, group_by, order_by, projection, aliass = nil, joins = [])
  built = '{' + (aliass == nil ? '' : '"alias": "' + aliass + '", ') + build_join_tag(joins) +
      ((projection.nil? || projection.eql?('') || projection.strip!.eql?('')) ? '' : (', ' + projection)) + ', "where": {'

  if conditions.length > 0
    global = conditions[0]
    if conditions.length > 1
      conditions = conditions[1..conditions.length]
      conditions.each do |condition|
        global = Dynamicloud::API::Criteria::ANDCondition.new global, condition
      end
    end

    built = built + global.to_record_string(Dynamicloud::API::Criteria::Condition::ROOT)
  end

  built = built + '}'

  if group_by
    built = built + ',' + group_by.to_record_string(Dynamicloud::API::Criteria::Condition::ROOT)
  end

  if order_by
    built = built + ',' + order_by.to_record_string(Dynamicloud::API::Criteria::Condition::ROOT)
  end

  built + '}'
end

.get_record_list(data) ⇒ Object

This method will extract data and Bind each field with attributes in mapper:getInstance method instance

Parameters:

  • data

    json with all data from Dynamicloud servers

Returns:

  • list of records

Since:

  • 8/26/15



724
725
726
727
728
729
730
731
732
733
734
# File 'lib/dynamic_api.rb', line 724

def self.get_record_list(data)
  record_list = []

  records = data['records']

  records.each do |jr|
    record_list.push(build_record(jr))
  end

  record_list
end

.normalize_record(record) ⇒ Object

This method will normalize the response from Dynamicloud servers

Since:

  • 8/26/15



572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# File 'lib/dynamic_api.rb', line 572

def self.normalize_record(record)
  normalized = {}
  record.each do |key, value|
    if value.is_a?(Hash)
      #This hash has only one key
      value.each do |ik, iv|
        if iv.respond_to?(:each)
          array = []
          iv.each do |item|
            array.push item.to_s
          end

          normalized[key] = array
        elsif iv.is_a?(String)
          normalized[key] = iv.to_s
        end

        #This hash has only one key
        break
      end
    else
      normalized[key] = value
    end
  end

  normalized
end