Module: Elephrame::Trace

Includes:
Tracery
Included in:
Bots::TraceryBot
Defined in:
lib/elephrame/mix/tracery.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#grammarObject

grammar is a hash { FILENAME => TRACERY RULES }



9
10
11
# File 'lib/elephrame/mix/tracery.rb', line 9

def grammar
  @grammar
end

Instance Method Details

#expand_and_post(text, *options) ⇒ Object Also known as: post

a shortcut fuction for expanding text with tracery before posting

Parameters:

  • text (String)

    the tracery text to expand before posting

  • options (Hash)

    a hash of arguments to pass to post

Options Hash (*options):

  • rules (String)

    the grammar rules to load

  • visibility (String)

    visibility level

  • spoiler (String)

    text to use as content warning

  • reply_id (String)

    id of post to reply to

  • hide_media (Bool)

    should we hide media?

  • media (Array<String>)

    array of file paths



56
57
58
59
60
61
62
63
# File 'lib/elephrame/mix/tracery.rb', line 56

def expand_and_post(text, *options)
  opts = Hash[*options]
  rules = opts.fetch(:rules, 'default')
  actually_post(@grammar[rules].flatten(text),
                **opts.reject {|k|
                  k == :rules
                })
end

#setup_tracery(*dirs) ⇒ Object

loads all of our tracery files into our files hash if a file is named ‘default’ then we load that into grammar

Parameters:

  • dirs (String)

    path to the directory containing the tracery rules



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/elephrame/mix/tracery.rb', line 17

def setup_tracery *dirs
  
  @grammar = {}

  dirs.each do |directory|
    Dir.open(directory) do |dir|
      dir.each do |file|
        # skip our current and parent dir
        next if file =~ /^\.\.?$/
        
        # read the rule file into the files hash
        @grammar[file.split('.').first] =
          createGrammar(JSON.parse(File.read("#{directory}/#{file}")))
      end
    end
  end
    
  # go ahead and makes a default mention-handler
  #  if we have a reply rule file
  unless @grammar['reply'].nil?
    on_reply do |bot|
      bot.reply_with_mentions('#default#', rules: 'reply')
    end
  end
end