Class: WordleDecoder::WordleShare

Inherits:
Object
  • Object
show all
Defined in:
lib/wordle_decoder/wordle_share.rb

Constant Summary collapse

ANSWER_LINES =
["🟩🟩🟩🟩🟩",
"🟧🟧🟧🟧🟧"].freeze
SHORTCODES_TO_EMOJIS =
{ ":black_large_square:" => "",
":white_large_square:" => "",
":large_green_square:" => "🟩",
":large_yellow_square:" => "🟨",
":large_orange_square:" => "🟧",
":large_blue_square:" => "🟦" }.freeze
NEEDS_HELP_INPUTS =
%w[help what wordle wat ? man okay yes no test].freeze
EXIT_PROGRAM_INPUTS =
%w[exit no nvm].freeze
GAME_DAY_REGEX =
/wordle\s(\d+)\s/i.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, answer_input = nil) ⇒ WordleShare

Returns a new instance of WordleShare.



55
56
57
58
# File 'lib/wordle_decoder/wordle_share.rb', line 55

def initialize(input, answer_input = nil)
  @input = input
  self.answer_input = answer_input
end

Instance Attribute Details

#answerObject

Returns the value of attribute answer.



63
64
65
# File 'lib/wordle_decoder/wordle_share.rb', line 63

def answer
  @answer
end

#answer_inputObject

Returns the value of attribute answer_input.



60
61
62
# File 'lib/wordle_decoder/wordle_share.rb', line 60

def answer_input
  @answer_input
end

#inputObject (readonly)

Returns the value of attribute input.



60
61
62
# File 'lib/wordle_decoder/wordle_share.rb', line 60

def input
  @input
end

Class Method Details

.exit_program?(input) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/wordle_decoder/wordle_share.rb', line 37

def self.exit_program?(input)
  EXIT_PROGRAM_INPUTS.include?(input.strip)
end

.final_line?(input_line) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
# File 'lib/wordle_decoder/wordle_share.rb', line 8

def self.final_line?(input_line)
  line = normalize_wordle_line(input_line.strip)
  ANSWER_LINES.include?(line)
end

.help_to_terminalObject



41
42
43
44
# File 'lib/wordle_decoder/wordle_share.rb', line 41

def self.help_to_terminal
  "\n  {{italic:Copy and paste those 🟨, 🟩, and ⬛" \
    " emojis from your wordle share.}} \n\n{{blue:>}} "
end

.load_worldle_ansersObject



50
51
52
53
# File 'lib/wordle_decoder/wordle_share.rb', line 50

def self.load_worldle_ansers
  file_path = File.join(File.dirname(__FILE__), "..", "wordle_answers.json")
  JSON.parse File.read(file_path)
end

.needs_help?(input) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/wordle_decoder/wordle_share.rb', line 31

def self.needs_help?(input)
  NEEDS_HELP_INPUTS.include?(input.strip.downcase)
end

.normalize_wordle_line(line) ⇒ Object



13
14
15
16
# File 'lib/wordle_decoder/wordle_share.rb', line 13

def self.normalize_wordle_line(line)
  line = translate_emoji_shortcodes(line)
  line.each_grapheme_cluster.map { |cluster| cluster.codepoints.first }.pack("U*")
end

.translate_emoji_shortcodes(line) ⇒ Object



25
26
27
# File 'lib/wordle_decoder/wordle_share.rb', line 25

def self.translate_emoji_shortcodes(line)
  SHORTCODES_TO_EMOJIS.reduce(line) { |acc, (key, val)| acc.gsub(key, val) }
end

.wordle_answersObject



46
47
48
# File 'lib/wordle_decoder/wordle_share.rb', line 46

def self.wordle_answers
  @wordle_answers ||= load_worldle_ansers
end

Instance Method Details

#answer_charsObject



80
81
82
# File 'lib/wordle_decoder/wordle_share.rb', line 80

def answer_chars
  @answer_chars ||= answer&.strip&.chars
end

#decoderObject



100
101
102
# File 'lib/wordle_decoder/wordle_share.rb', line 100

def decoder
  @decoder ||= WordleDecoder.new(self)
end

#find_answerObject



72
73
74
75
76
77
78
# File 'lib/wordle_decoder/wordle_share.rb', line 72

def find_answer
  title_line = input_lines.detect { |line| line.match?(GAME_DAY_REGEX) }
  return unless title_line

  game_day = title_line.match(GAME_DAY_REGEX).captures.first&.to_i
  self.answer = self.class.wordle_answers[game_day]
end

#hint_linesObject



84
85
86
# File 'lib/wordle_decoder/wordle_share.rb', line 84

def hint_lines
  @hint_lines ||= parse_hint_lines!
end

#input_linesObject



92
93
94
# File 'lib/wordle_decoder/wordle_share.rb', line 92

def input_lines
  @input_lines ||= normalize_input_lines(parse_input_lines!)
end

#inspectObject



104
105
106
107
# File 'lib/wordle_decoder/wordle_share.rb', line 104

def inspect
  "<#{self.class.name} input: #{input}, answer_input: #{answer_input}" \
    " answer_chars: #{answer_chars.inspect} hint_lines: #{hint_lines.inspect}>"
end

#to_terminalObject



96
97
98
# File 'lib/wordle_decoder/wordle_share.rb', line 96

def to_terminal
  "{{blue:>}} #{wordle_lines.join("\n  ")}"
end

#wordle_linesObject



88
89
90
# File 'lib/wordle_decoder/wordle_share.rb', line 88

def wordle_lines
  @wordle_lines ||= parse_wordle_lines!
end