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



679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
# File 'lib/dynamic_api.rb', line 679

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



698
699
700
701
702
703
704
705
706
707
708
709
710
711
# File 'lib/dynamic_api.rb', line 698

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



765
766
767
768
769
770
771
772
773
774
775
776
777
778
# File 'lib/dynamic_api.rb', line 765

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



746
747
748
749
750
751
752
753
754
755
756
757
758
# File 'lib/dynamic_api.rb', line 746

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



812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
# File 'lib/dynamic_api.rb', line 812

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



782
783
784
785
786
787
788
789
790
791
792
793
794
# File 'lib/dynamic_api.rb', line 782

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



715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
# File 'lib/dynamic_api.rb', line 715

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



799
800
801
802
803
804
805
806
807
808
809
# File 'lib/dynamic_api.rb', line 799

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



647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
# File 'lib/dynamic_api.rb', line 647

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