class Dynamicloud::API::DynamicloudHelper

This is a class with utility methods @author Eleazar Gomez @version 1.0.0 @since 8/26/15

Public Class Methods

build_fields_json(data) click to toggle source

Builds a compatible string to update a record. This method will get field name and its value form data hash. @param instance Object where data is extracted @return compatible string

# 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) click to toggle source

Builds an array of RecordFieldItems according JSONArray @param array JSONArray with pair value, text.

# 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) click to toggle source

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

@param joins list of join clauses @return the representation of a join tag.

# 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) click to toggle source

Build a compatible string using projection @return string using projection

# 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) click to toggle source

Builds the record hash with data from jr JSON object

# 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) click to toggle source

This utility will build a RecordResults object @param response ServiceResponse from Dynamicloud servers

# 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 = []) click to toggle source

Builds a compatible String to use in service executions @return compatible String

# 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) click to toggle source

This method will extract data and Bind each field with attributes in mapper:getInstance method instance @param data json with all data from Dynamicloud servers @return list of records

# 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) click to toggle source

This method will normalize the response from Dynamicloud servers

# 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