Sha256: de7171345e292326f42ea9c5b020648141be388ef6446da66f37a0b08474a921

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module LedgerSync
  module Ledgers
    module NetSuite
      class Searcher < Ledgers::Searcher
        include Mixins::OffsetAndLimitPaginationSearcherMixin

        def query_attributes
          @query_attributes ||= searcher_deserializer_class.attributes.values.map(&:hash_attribute)
        end

        def query_string
          "SELECT #{query_attributes.join(', ')} FROM #{query_table}"
        end

        def query_table
          @query_table ||= client.class.ledger_resource_type_for(resource_class: self.class.inferred_resource_class)
        end

        def request_url
          client.api_base_url.gsub('/record/v1', '') + "/query/v1/suiteql?limit=#{limit}&offset=#{offset}"
        end

        def resources
          resource_class = self.class.inferred_resource_class

          @resources ||= begin
            @request = client
                       .post(
                         body: { q: query_string.to_s },
                         request_url: request_url
                       )

            case request.status
            when 200
              request.body
                     .fetch('items')
                     .map do |c|
                searcher_deserializer_class.new.deserialize(hash: c, resource: resource_class.new)
              end
            when 404
              []
            when 400
              if request.body["title"].include? "Invalid search query Search error occurred: Record ‘subsidiary’ was not found"
                raise LedgerSync::Ledgers::NetSuite::Error::SubsidiariesNotEnabled
              end
            end
          end
        end

        def searcher_deserializer_class
          @searcher_deserializer_class ||= self.class.inferred_serialization_class(type: 'SearcherDeserializer')
        end

        private

        def default_offset
          0
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ledger_sync-1.4.2 lib/ledger_sync/ledgers/netsuite/searcher.rb
ledger_sync-1.4.1 lib/ledger_sync/ledgers/netsuite/searcher.rb
ledger_sync-1.4.0 lib/ledger_sync/ledgers/netsuite/searcher.rb