Class: Mnemonicker::WordList

Inherits:
Struct
  • Object
show all
Defined in:
lib/mnemonicker/word_list.rb

Defined Under Namespace

Classes: ListNotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keyObject

Returns the value of attribute key

Returns:

  • (Object)

    the current value of key



2
3
4
# File 'lib/mnemonicker/word_list.rb', line 2

def key
  @key
end

Instance Method Details

#fetchObject



5
6
7
8
9
# File 'lib/mnemonicker/word_list.rb', line 5

def fetch
  JSON.parse(File.read(cache_file))
rescue
  raise ListNotFound
end

#update(&block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mnemonicker/word_list.rb', line 11

def update(&block)
  wordlist = {
    version:    1,
    fetched_at: Time.now.utc,
    'words' => {}
  }
  index = fetch_url('index.html')
  index.lines.each do |line|
    line = line.chomp
    words = fetch_url(line)
    words.lines.each do |word|
      word = word.chomp
      number = MajorSystem.word_to_number(word)
      wordlist['words'][number] ||= []
      wordlist['words'][number] << word
    end
    if block
      block.call(line, words.lines.count)
    end
  end

  FileUtils.mkdir_p(File.dirname(cache_file))
  File.open(cache_file, "w") {|f| f.write wordlist.to_json }
end