Class: WordleDictionariesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/wordle_dictionaries_controller.rb

Overview

Handles modifications to the WordleDictionary by a Puzzle Setter

Instance Method Summary collapse

Instance Method Details

#amend_dictObject

PATCH /wordle_dictionaries/amend_dict or /wordle_dictionaries/amend_dict.json

Parameters:

  • new_words (String)

    ‘\n’ separated string of words to be used for update

  • update_opt (String)

    ‘add’ for simple insert, ‘replace’ to overwrite existing dictionary or ‘remove’ to remove provided words. On ‘add’, existing words will be updated to provided parameters.

  • valid_solutions (Boolean)

    specifies whether words are valid solutions or not



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/wordle_dictionaries_controller.rb', line 31

def amend_dict
  errors = []
  if validate_amend_dict_params
    new_words = parse_words_from_str
    delete_opt = params[:update_opt] == "replace"
    add_opt = delete_opt || params[:update_opt] == "add"
    errors = update_db(new_words, delete_opt, add_opt)
  else
    errors << "Please provide a list of valid words and select an update option"
  end

  if errors.empty?
    render json: { success: true }, status: 200
  else
    render json: { success: false, errors: errors }, status: 500
  end
end

#indexObject

GET /wordle_dictionaries or /wordle_dictionaries.json Returns both HTML content and JSON content, the request must specify required response format

For JSON requests, the following parameters can be specified (all the parameters can be requested together):

Parameters:

  • only_solutions (Boolean)

    requests only valid solutions to be returned

  • sort_asc (Boolean)

    toggles alphabetical sorting on the returned words

  • word_part (String)

    requests only words that are prefixed by specified string



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/wordle_dictionaries_controller.rb', line 12

def index
  if request.format.json?
    query = WordleDictionary
      .where("word LIKE ?", "#{params[:word_part]}%")
      .order(word: sort_order)

    @wordle_dictionaries = filter_only_solutions(query)
    render json: { success: true, words: @wordle_dictionaries }, status: 200
  else
    @wordle_dictionaries = WordleDictionary.order(:word)
    render :index
  end
end

#reset_dictObject

PATCH /wordle_dictionaries/reset_dict or /wordle_dictionaries/reset_dict.json

Resets the active WordleDictionary to the default original copy (WordleDictionaryBackup)



52
53
54
55
56
57
58
59
60
# File 'app/controllers/wordle_dictionaries_controller.rb', line 52

def reset_dict
  new_words = WordleDictionaryBackup.all.map { |record| { word: record.word, is_valid_solution: record.is_valid_solution } }
  errors = update_db(new_words, true, true)
  if errors.empty?
    render json: { success: true }, status: 200
  else
    render json: { success: false, errors: errors }, status: 500
  end
end