Class: NetSuite::Support::SearchResult

Inherits:
Object
  • Object
show all
Defined in:
lib/netsuite/support/search_result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, result_class) ⇒ SearchResult

<platformCore:searchResult xmlns:platformCore=“urn:core_2012_1.platform.webservices.netsuite.com”>

<platformCore:status isSuccess="true"/>
<platformCore:totalRecords>2770</platformCore:totalRecords>
<platformCore:pageSize>5</platformCore:pageSize>
<platformCore:totalPages>554</platformCore:totalPages>
<platformCore:pageIndex>1</platformCore:pageIndex>
<platformCore:searchId>WEBSERVICES_738944_SB2_03012013650784545962753432_28d96bd280</platformCore:searchId>


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/netsuite/support/search_result.rb', line 20

def initialize(response, result_class)
  @result_class = result_class
  @response = response
  
  @total_records = response.body[:total_records].to_i
  @total_pages = response.body[:total_pages].to_i
  @current_page = response.body[:page_index].to_i

  if @total_records > 0
    if response.body.has_key?(:record_list)
      # basic search results
      record_list = response.body[:record_list][:record]
      record_list = [record_list] unless record_list.is_a?(Array)

      record_list.each do |record|
        results << result_class.new(record)
      end
    elsif response.body.has_key? :search_row_list
      # advanced search results
      record_list = response.body[:search_row_list][:search_row]
      record_list = [record_list] unless record_list.is_a?(Array)

      record_list.each do |record|
        # TODO because of customFieldList we need to either make this recursive
        #      or handle the customFieldList as a special case

        record.each_pair do |search_group, search_data|
          # skip all attributes: look for :basic and all :xxx_join
          next if search_group.to_s.start_with?('@')

          record[search_group].each_pair do |k, v|
            # all return values are wrapped in a <SearchValue/>
            # extract the value from <SearchValue/> to make results easier to work with

            if v.is_a?(Hash) && v.has_key?(:search_value)
              # search return values that are just internalIds are stored as attributes on the searchValue element
              if v[:search_value].is_a?(Hash) && v[:search_value].has_key?(:'@internal_id')
                record[search_group][k] = v[:search_value][:'@internal_id']
              else
                record[search_group][k] = v[:search_value]
              end
            end
          end
        end

        result_wrapper = result_class.new(record.delete(:basic))
        result_wrapper.search_joins = record
        results << result_wrapper
      end
    else
      raise "uncaught search result"
    end
  end
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



8
9
10
# File 'lib/netsuite/support/search_result.rb', line 8

def current_page
  @current_page
end

#responseObject

Returns the value of attribute response.



4
5
6
# File 'lib/netsuite/support/search_result.rb', line 4

def response
  @response
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



7
8
9
# File 'lib/netsuite/support/search_result.rb', line 7

def total_pages
  @total_pages
end

#total_recordsObject (readonly)

Returns the value of attribute total_records.



6
7
8
# File 'lib/netsuite/support/search_result.rb', line 6

def total_records
  @total_records
end

Instance Method Details

#resultsObject



75
76
77
# File 'lib/netsuite/support/search_result.rb', line 75

def results
  @results ||= []
end

#results_in_batches {|results| ... } ⇒ Object

Yields:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/netsuite/support/search_result.rb', line 79

def results_in_batches
  return if self.total_records.zero?

  while @response.body[:total_pages] != @response.body[:page_index]
    yield results

    next_search = @result_class.search(
      search_id: @response.body[:search_id],
      page_index: @response.body[:page_index].to_i + 1
    )

    @results = next_search.results
    @response = next_search.response
    @current_page = response.body[:page_index].to_i
  end

  yield results
end