Class: BentoSearch::Results

Inherits:
Array
  • Object
show all
Defined in:
app/models/bento_search/results.rb

Overview

An array-like object (in fact it-subclasses Array) that holds a page of search results. But also has some meta-data about the search itself (query, paging, etc).

If #error is non-nil, object may not have real results, but be an error. You can use failed? to see.

Serializes as a Hash including ONLY the serialized results themselves, and the engine ID. Search context (total items, start, etc) are not included in serialization. Configuration context is also not serialized, although since the engineID is, it can be reconstructed on de-serialization.

Serialization isn’t actually implemented with the BentoSearch::Results::Serialization module, cause it didn’t work out, just re-implemented here with same methods.

Defined Under Namespace

Modules: Serialization Classes: Pagination

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#display_configurationObject

simply copied over from search engine configuration :display key, useful for making config available at display time in a DRY way.



27
28
29
# File 'app/models/bento_search/results.rb', line 27

def display_configuration
  @display_configuration
end

#engine_idObject

Registered id of engine used to create these results, may be nil if used with an unregistered engine.



60
61
62
# File 'app/models/bento_search/results.rb', line 60

def engine_id
  @engine_id
end

#errorObject

If error is non-nil, it’s an error condition with no real results. error should be a hash with these (and possibly other) keys, although none of these are required to be non-nil.

:status

A (usually) non-succesful HTTP status code. May be nil.

:message

A short message explaining error, usually provided by external service. NOT suitable for showing to end-users. May be nil.

:end_user_message

A message suitable for showing to end-users. May be nil.

:error_info

A service-specific way of reporting more error info, for developers, not suitable for end-users. Might be a string, might be a hash, depends on the service. may be nil.

:exception

Possibly a ruby exception object. may be nil.



45
46
47
# File 'app/models/bento_search/results.rb', line 45

def error
  @error
end

#per_pageObject

per_page setting, can be used for pagination.



23
24
25
# File 'app/models/bento_search/results.rb', line 23

def per_page
  @per_page
end

#search_argsObject

search arguments as normalized by SearchEngine, not neccesarily directly as input. A hash.



57
58
59
# File 'app/models/bento_search/results.rb', line 57

def search_args
  @search_args
end

#startObject

0-based index into total results, used for pagination



21
22
23
# File 'app/models/bento_search/results.rb', line 21

def start
  @start
end

#timingObject

time it took to do search, in seconds as float



48
49
50
# File 'app/models/bento_search/results.rb', line 48

def timing
  @timing
end

#total_itemsObject

Returns the value of attribute total_items.



19
20
21
# File 'app/models/bento_search/results.rb', line 19

def total_items
  @total_items
end

Class Method Details

.from_internal_state_hash(hash) ⇒ Object

Creates a Results object from an internal_state_hash, and restores it’s configuration from engine_id



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/bento_search/results.rb', line 89

def self.from_internal_state_hash(hash)
  results = BentoSearch::Results.new
  results.engine_id = hash["engine_id"]
  hash["result_items"].each do |item_hash|
    results << BentoSearch::ResultItem.from_internal_state_hash(item_hash)
  end

  if results.engine_id
    BentoSearch.get_engine(results.engine_id).(results, {})
  end

  return results
end

.load_json(json_str) ⇒ Object



107
108
109
# File 'app/models/bento_search/results.rb', line 107

def self.load_json(json_str)
  from_internal_state_hash JSON.parse(json_str)
end

Instance Method Details

#dump_to_jsonObject



103
104
105
# File 'app/models/bento_search/results.rb', line 103

def dump_to_json
  JSON.dump self.internal_state_hash
end

#failed?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'app/models/bento_search/results.rb', line 70

def failed?
  ! error.nil?
end

#inspectObject



74
75
76
# File 'app/models/bento_search/results.rb', line 74

def inspect
  "<BentoSearch::Results #{super} #{'FAILED' if self.failed?}>"
end

#internal_state_hashObject

Serialization



79
80
81
82
83
84
85
# File 'app/models/bento_search/results.rb', line 79

def internal_state_hash
  {
    "engine_id" => self.engine_id,
    "result_items" => self.collect {|i| i.internal_state_hash},
    "bento_search_version" => BentoSearch::VERSION
  }
end

#paginationObject

Returns a BentoSearch::Results::Pagination, that should be suitable for passing right to Kaminari (although Kaminari isn’t good about doc/specing it’s api, so might break), or convenient methods for your own custom UI.



66
67
68
# File 'app/models/bento_search/results.rb', line 66

def pagination
  Pagination.new( total_items, search_args)
end

#timing_msObject

timing from #timing, but in miliseconds as int



50
51
52
53
# File 'app/models/bento_search/results.rb', line 50

def timing_ms
  return nil if timing.nil?
  (timing * 1000).to_i
end