class Gemwarrior::WordList

Constants

STATIC_ADJECTIVE_VALUES
STATIC_NOUN_PLURAL_VALUES
STATIC_NOUN_VALUES
STATIC_VERB_VALUES

Attributes

error[RW]
limit[RW]
type[RW]
words[RW]

Public Class Methods

new(type = 'noun', limit = 10) click to toggle source
# File lib/gemwarrior/misc/wordlist.rb, line 26
def initialize(type = 'noun', limit = 10)
  self.type         = type
  self.limit        = limit
  self.words        = populate_words(type, limit)
  self.error        = nil
end

Public Instance Methods

get_random_value() click to toggle source
# File lib/gemwarrior/misc/wordlist.rb, line 33
def get_random_value
  random_value = words[rand(0..limit)]

  return random_value.nil? ? get_random_value : random_value
end
list_words() click to toggle source
# File lib/gemwarrior/misc/wordlist.rb, line 39
def list_words
  words.join(',')
end

Private Instance Methods

get_static_values(type = nil) click to toggle source
# File lib/gemwarrior/misc/wordlist.rb, line 95
def get_static_values(type = nil)
  static_values = []
  0.upto(10) do
    case type
    when 'noun'
      static_values.push(STATIC_NOUN_VALUES[rand(0..STATIC_NOUN_VALUES.length-1)])
    when 'noun-plural'
      static_values.push(STATIC_NOUN_PLURAL_VALUES[rand(0..STATIC_NOUN_PLURAL_VALUES.length-1)])
    when 'adjective'
      static_values.push(STATIC_ADJECTIVE_VALUES[rand(0..STATIC_ADJECTIVE_VALUES.length-1)])
    when 'verb'
      static_values.push(STATIC_VERB_VALUES[rand(0..STATIC_VERB_VALUES.length-1)])
    else
      error = 'invalid wordlist type'
      return 
    end
  end
  return static_values
end
populate_words(type, limit = 10) click to toggle source
# File lib/gemwarrior/misc/wordlist.rb, line 45
def populate_words(type, limit = 10)
  url = 'http://api.wordnik.com:80/v4/words.json/randomWords'
  api_key = ENV['WORDNIK_API_KEY']

  unless api_key.nil? || GameOptions.data['use_wordnik'] == false
    case type
    when 'noun', 'noun-plural', 'adjective', 'verb'
    else
      error = 'invalid wordlist type'
      return
    end

    json_return = HTTP.get(
      url, 
      params: { 
        hasDictionaryDef:     true,
        includePartOfSpeech:  type,
        minCorpusCount:       1,
        maxCorpusCount:       -1,
        minDictionaryCount:   1,
        maxDictionaryCount:   -1,
        minLength:            5,
        maxLength:            12,
        sortBy:               'alpha',
        sortOrder:            'asc',
        limit:                limit,
        api_key:              api_key
      }
    )

    json_data = JSON.parse(json_return.to_s)

    if json_data.include?('type') && json_data.include?('message')
      error = "wordnik #{json_data['type']}: #{json_data['message']}"
      return get_static_values
    else
      word_list = []
      json_data.map {|j| word_list.push(j['word'])}
      if word_list.length > 0
        return word_list
      else
        error = 'Empty array from Wordnik'
        return get_static_values(type)
      end
    end
  end

  return get_static_values(type)
end