Class: SiteSearch::ResultItem

Inherits:
Data
  • Object
show all
Defined in:
app/services/site_search/result_item.rb

Overview

One row in a site-search result set, whatever engine produced it.

The adapters (SwiftypeAdapter, PublicationsAdapter)
each map their own payload into this shape, so Query can rank and
interleave them without knowing where they came from — and so replacing an
engine never changes what the browser receives.

highlight holds engine-supplied snippets with <em>-style emphasis, keyed
title/body. The client sanitizes those narrowly, so adapters must not put
arbitrary HTML there.

Constant Summary collapse

TYPE_ALIASES =

Canonical type labels the UI filters on. Engines report these inconsistently
(faqs, faq, Blog, post, …), so normalization happens here — once —
rather than in every browser.

{
  'faq' => 'FAQ',
  'faqs' => 'FAQ',
  'product' => 'Product',
  'products' => 'Product',
  'blog' => 'Blog',
  'post' => 'Blog',
  'video' => 'Video',
  'videos' => 'Video',
  'support' => 'Support',
  'publication' => 'Publication',
  'publications' => 'Publication'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, url: nil, image: nil, title: nil, body: nil, highlight: {}, type: nil) ⇒ ResultItem

Returns a new instance of ResultItem.

Parameters:

  • id (String, nil) (defaults to: nil)

    the engine's own identifier

  • url (String, nil) (defaults to: nil)

    a relative path for same-site results

  • image (String, nil) (defaults to: nil)

    thumbnail URL

  • title (String, nil) (defaults to: nil)
  • body (String, nil) (defaults to: nil)

    may be HTML; the client strips it to text

  • highlight (Hash) (defaults to: {})

    engine snippets keyed title/body

  • type (String, nil) (defaults to: nil)

    a canonical label from TYPE_ALIASES



38
39
40
# File 'app/services/site_search/result_item.rb', line 38

def initialize(id: nil, url: nil, image: nil, title: nil, body: nil, highlight: {}, type: nil)
  super
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



13
14
15
# File 'app/services/site_search/result_item.rb', line 13

def body
  @body
end

#highlightObject (readonly)

Returns the value of attribute highlight

Returns:

  • (Object)

    the current value of highlight



13
14
15
# File 'app/services/site_search/result_item.rb', line 13

def highlight
  @highlight
end

#idObject (readonly)

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



13
14
15
# File 'app/services/site_search/result_item.rb', line 13

def id
  @id
end

#imageObject (readonly)

Returns the value of attribute image

Returns:

  • (Object)

    the current value of image



13
14
15
# File 'app/services/site_search/result_item.rb', line 13

def image
  @image
end

#titleObject (readonly)

Returns the value of attribute title

Returns:

  • (Object)

    the current value of title



13
14
15
# File 'app/services/site_search/result_item.rb', line 13

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



13
14
15
# File 'app/services/site_search/result_item.rb', line 13

def type
  @type
end

#urlObject (readonly)

Returns the value of attribute url

Returns:

  • (Object)

    the current value of url



13
14
15
# File 'app/services/site_search/result_item.rb', line 13

def url
  @url
end

Class Method Details

.normalize_type(raw, fallback: nil) ⇒ String?

Parameters:

  • raw (String, nil)

    the engine's own type string

  • fallback (String, nil) (defaults to: nil)

    used when the engine reported nothing

Returns:

  • (String, nil)


45
46
47
48
49
50
# File 'app/services/site_search/result_item.rb', line 45

def self.normalize_type(raw, fallback: nil)
  value = raw.to_s.strip
  return fallback if value.blank?

  TYPE_ALIASES[value.downcase] || value.upcase_first
end

Instance Method Details

#as_jsonHash

Returns one result row as the browser consumes it.

Returns:

  • (Hash)

    one result row as the browser consumes it



53
54
55
# File 'app/services/site_search/result_item.rb', line 53

def as_json(*)
  { id:, url:, image:, title:, body:, highlight:, type: }
end