Class: BentoSearch::ResultItem

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::OutputSafetyHelper, BentoSearch::Results::Serialization, ERB::Util
Defined in:
app/models/bento_search/result_item.rb

Overview

Data object representing a single hit from a search, normalized with common data fields. Usually held in a BentoSearch::Results object.

ANY field can be nil, clients should be aware.

Each item has a field for one main link as string url, at #link (which may be nil), as well as array of possibly additional links (with labels and metadata) under #other_links. #other_links is an array of BentoSearch::Link objects.

Constant Summary collapse

@@format_to_schema_org =
{
  :report => "Article",
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BentoSearch::Results::Serialization

#dump_to_json, #internal_state_hash

Constructor Details

#initialize(args = {}) ⇒ ResultItem

Can initialize with a hash of key/values



23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/bento_search/result_item.rb', line 23

def initialize(args = {})
  args.each_pair do |key, value|
    send("#{key}=", value)
  end

  self.authors     ||= []
  self.other_links ||= []
  self.snippets    ||= []

  self.custom_data ||= {}
end

Instance Attribute Details

#authorsObject

An array (order matters) of BentoSearch::Author objects add authors to it with results.authors << Author



244
245
246
# File 'app/models/bento_search/result_item.rb', line 244

def authors
  @authors
end

#decoratorObject

Copied over from engine configuration usually, a string qualified name of a decorator class. Can be nil for default.



253
254
255
# File 'app/models/bento_search/result_item.rb', line 253

def decorator
  @decorator
end

#display_configurationObject

Copied over from engine configuration :for_display key by BentoSearch#search wrapper, here as a convenience t parameterize logic in decorators or other presentational logic, based on configuration, in places where logic has access to an item but not the overall Results obj anymore.

TODO: Consider, should we just copy over the whole Results into a backpointing reference instead? And user cover-methods for it? Nice thing about the configuration has instead is it’s easily serializable, it’s just data.

Although we intentionally do NOT include these in JSON serialization, ha.



267
268
269
# File 'app/models/bento_search/result_item.rb', line 267

def display_configuration
  @display_configuration
end

#engine_idObject

Returns the value of attribute engine_id.



268
269
270
# File 'app/models/bento_search/result_item.rb', line 268

def engine_id
  @engine_id
end

#language_strObject



158
159
160
161
162
163
164
# File 'app/models/bento_search/result_item.rb', line 158

def language_str
  (@language_str ||= nil) || language_code.try do |code|
    LanguageList::LanguageInfo.find(code).try do |lang_obj|
      lang_obj.name
    end
  end
end

usually a direct link to the search provider’s ‘native’ page. Can be changed in actual presentation with a Decorator.

  • schema.org CreativeWork: ‘url’



62
63
64
# File 'app/models/bento_search/result_item.rb', line 62

def link
  @link
end

Array (possibly empty) of BentoSearch::Link objects representing additional links. Often SearchEngine’s themselves won’t include any of these, but Decorators will be used to add them in.



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

def other_links
  @other_links
end

#publication_dateObject

ruby stdlib Date object.



192
193
194
# File 'app/models/bento_search/result_item.rb', line 192

def publication_date
  @publication_date
end

#snippetsObject

An ARRAY of string query-in-context snippets. Will usually have highlighting <b> tags in it. Creator is responsible for making sure it’s otherwise html-safe.

Not all engines may stores Snippets array in addition to abstract, some may only store one or the other. Some may store both but with same content formatted differently (array of multiple vs one combined string), some engines they may be different.



239
240
241
# File 'app/models/bento_search/result_item.rb', line 239

def snippets
  @snippets
end

Instance Method Details

#language_iso_639_1Object

Two letter ISO language code, or nil



174
175
176
# File 'app/models/bento_search/result_item.rb', line 174

def language_iso_639_1
  language_obj.try { |l| l.iso_639_1 }
end

#language_iso_639_3Object

Three letter ISO language code, or nil



179
180
181
# File 'app/models/bento_search/result_item.rb', line 179

def language_iso_639_3
  language_obj.try {|l| l.iso_639_3 }
end

#language_objObject

Returns a LanguageList gem language object– from #language_code if available, otherwise from direct language_str if available and possible.



169
170
171
# File 'app/models/bento_search/result_item.rb', line 169

def language_obj
  @language_obj ||= LanguageList::LanguageInfo.find( self.language_code || self.language_str )
end


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

def link_is_fulltext=(v)
  @link_is_fulltext = v
end

does the #link correspond to fulltext? true or false – or nil for unknown/non-applicable. Not all engines will set.

Returns:

  • (Boolean)


67
68
69
# File 'app/models/bento_search/result_item.rb', line 67

def link_is_fulltext?
  @link_is_fulltext
end

#schema_org_type_urlObject

Translated from internal format vocab at #format. Outputs eg schema.org/Book Uses the @@format_to_schema_org hash for mapping from certain internal symbol values to schema org value, where possible.

Can return nil if we don’t know a schema.org type



124
125
126
127
128
129
130
131
132
# File 'app/models/bento_search/result_item.rb', line 124

def schema_org_type_url
  if format.kind_of? String
    "http://schema.org/#{format}"
  elsif mapped = @@format_to_schema_org[format]
    "http://schema.org/#{mapped}"
  else
    nil
  end
end