Sha256: 37d5e018635c360d46e81d61e17784af12343d96204c3737f9dbe2c79d04896e

Contents?: true

Size: 1.24 KB

Versions: 25

Compression:

Stored size: 1.24 KB

Contents

module RockRMS
  module Response
    class Base
      attr_reader :data

      BASE_MAPPING = {
        id: 'Id',
        guid: 'Guid',
        created_date_time: 'CreatedDateTime',
        modified_date_time: 'ModifiedDateTime',
        attributes: 'Attributes',
        attribute_values: 'AttributeValues'
      }.freeze

      def self.format(data)
        new(data).format
      end

      def initialize(data)
        @data = data
      end

      def format
        if data.is_a?(Array)
          data.map { |item| format_single(item) }
        else
          format_single(data)
        end
      end

      def to_h(dict, data)
        return {} if data.nil?

        dict
          .merge(BASE_MAPPING)
          .each_with_object({}) do |(l, r), object|
            if l == :attributes || l == :attribute_values
              format_klass = l == :attributes ? Attribute : AttributeValue
              object[l] = format_attributes(data[r], format_klass)
            else
              object[l] = data[r]
            end
          end
      end

      def format_attributes(res, klass)
        return res if res.nil?

        res.each_with_object({}) do |(attr, val), object|
          object[attr] = klass.format(val)
        end
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
rock_rms-8.23.0 lib/rock_rms/response/base.rb
rock_rms-8.22.0 lib/rock_rms/response/base.rb
rock_rms-8.21.0 lib/rock_rms/response/base.rb
rock_rms-8.20.0 lib/rock_rms/response/base.rb
rock_rms-8.19.0 lib/rock_rms/response/base.rb
rock_rms-8.18.0 lib/rock_rms/response/base.rb
rock_rms-8.17.0 lib/rock_rms/response/base.rb
rock_rms-8.16.0 lib/rock_rms/response/base.rb
rock_rms-8.15.2 lib/rock_rms/response/base.rb
rock_rms-8.15.1 lib/rock_rms/response/base.rb
rock_rms-8.15.0 lib/rock_rms/response/base.rb
rock_rms-8.14.0 lib/rock_rms/response/base.rb
rock_rms-8.13.0 lib/rock_rms/response/base.rb
rock_rms-8.12.0 lib/rock_rms/response/base.rb
rock_rms-8.11.0 lib/rock_rms/response/base.rb
rock_rms-8.10.0 lib/rock_rms/response/base.rb
rock_rms-8.9.0 lib/rock_rms/response/base.rb
rock_rms-8.8.0 lib/rock_rms/response/base.rb
rock_rms-8.7.0 lib/rock_rms/response/base.rb
rock_rms-8.6.0 lib/rock_rms/response/base.rb