Class: SnippetFinderService

Inherits:
Object
  • Object
show all
Includes:
RedisSnippets::Util
Defined in:
app/services/snippet_finder_service.rb

Constant Summary collapse

SECTION_DELIMITER =
"[section]"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RedisSnippets::Util

#snippet_key

Constructor Details

#initialize(key:) ⇒ SnippetFinderService

Returns a new instance of SnippetFinderService.



6
7
8
# File 'app/services/snippet_finder_service.rb', line 6

def initialize(key:)
  @key = key
end

Class Method Details

.random_snippet(content) ⇒ Object



17
18
19
20
21
22
# File 'app/services/snippet_finder_service.rb', line 17

def random_snippet(content)
  return "" if content.blank?

  snippets = content.split("#{SECTION_DELIMITER}").map { |section| section.gsub(/^\n/, "") }
  snippets[rand(snippets.length)]
end

Instance Method Details

#callObject



10
11
12
# File 'app/services/snippet_finder_service.rb', line 10

def call
  self.class.random_snippet(SnippetStoreService.send(snippet_key(@key)))
end