Sha256: 1f4cbb3fd247d78400fd69be1edcd746b058260174af601ac5794b56baf8544d

Contents?: true

Size: 1.88 KB

Versions: 5

Compression:

Stored size: 1.88 KB

Contents

module NetSuite
  module Actions
    class GetList
      include Support::Requests

      def initialize(klass, options = { })
        @klass = klass
        @options = options
      end

      private

      def request
        NetSuite::Configuration.connection.call :get_list, :message => request_body
      end

      def request_body
        if @options[:type_id]
          type = @options[:type_id]
          record_type = 'platformCore:CustomRecordRef'
        else
          type = @klass.to_s.split('::').last.lower_camelcase
          record_type = 'platformCore:RecordRef'
        end

        list = @options.is_a?(Hash) ? @options[:list] : @options

        {
          baseRef: list.map do |internal_id|
            {
              '@internalId' => internal_id,
              '@typeId' => type,
              '@xsi:type' => record_type
            }
          end
        }
      end

      def response_header
        @response_header ||= response_header_hash
      end

      def response_header_hash
        @response_header_hash = @response.header[:document_info]
      end

      def response_body
        @response_body ||= @response.body[:get_list_response][:read_response_list][:read_response]
      end

      def success?
        # each returned record has its own status; for now if one fails, the entire operation has failed
        @success ||= response_body.detect { |r| r[:status][:@is_success] != 'true' }.nil?
      end

      module Support
        def self.included(base)
          base.extend(ClassMethods)
        end

        module ClassMethods
          def get_list(options = { })
            response = NetSuite::Actions::GetList.call(self, options)

            if response.success?
              response.body.map do |record|
                new(record[:record])
              end
            else
              false
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
netsuite-0.2.4 lib/netsuite/actions/get_list.rb
netsuite-0.2.3 lib/netsuite/actions/get_list.rb
netsuite-0.2.2 lib/netsuite/actions/get_list.rb
netsuite-0.2.1 lib/netsuite/actions/get_list.rb
netsuite-0.2.0 lib/netsuite/actions/get_list.rb