Class: FandomWord::FandomRandomizer

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/fandom_word.rb

Overview

Provides singleton interface with fandom word catalog to allow retrieval of random words, random words based on fandoms and list of all available fandoms

Instance Method Summary collapse

Constructor Details

#initializeFandomRandomizer

Caches word catalog into memory for performance



36
37
38
# File 'lib/fandom_word.rb', line 36

def initialize
  load_word_catalog
end

Instance Method Details

#available_fandomsObject

Returns the list of fandoms (categories)



59
60
61
# File 'lib/fandom_word.rb', line 59

def available_fandoms
  @catalog.fandoms
end

#random_word(fandom_name_or_names = random_fandom) ⇒ String

Returns random word from any fandom in catalog. Fandom category can be specified.

Parameters:

  • fandom_name_or_names (String, Array) (defaults to: random_fandom)

    Fandom category or array of categories

Returns:

  • (String)

    Random word



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fandom_word.rb', line 44

def random_word(fandom_name_or_names = random_fandom)
  fandom_name = case fandom_name_or_names
                when Array
                  validate_fandom_list fandom_name_or_names
                  random_fandom_name_from fandom_name_or_names
                when String
                  fandom_name_or_names
                else
                  raise ArgumentError, "Argument must be [Array, String] not #{fandom_name_or_names.class}"
                end

  fetch_fandom fandom_name
end