1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- require 'figlet'
- require 'sinatra'
- require 'dotenv/load'
-
- def fig(input)
- opts = parse(input)
-
- banners = []
-
- font = Figlet::Font.new(File.expand_path('./banner.flf'))
- figlet = Figlet::Typesetter.new(font)
-
- chunks = opts[:message].chars.to_a.each_slice(5).to_a.map{|chunk| chunk.join}
-
- chunks.each do |chunk|
- banner = figlet[chunk.strip].gsub(/#/, opts[:ink]).gsub(/ /, opts[:space])
-
- unless uses_emoji(opts)
- banner = "```#{banner}```"
- end
-
- banners << banner
- end
-
- banners.join("\n")
- end
-
- def parse(input)
- opts = {
- :message => String.new,
- :space => " ",
- :ink => "#",
- }
-
- position = 0
-
- encountered_options = false
-
- while position < input.length
- if m = input.slice(position..-1).match(/^space=(\S+)/)
- encountered_options = true
- opts[:space] = m[1].strip
- position += m[0].length
- elsif m = input.slice(position..-1).match(/^ink=(\S+)/)
- encountered_options = true
- opts[:ink] = m[1].strip
- position += m[0].length
- elsif m = input.slice(position..-1).match(/^font=(\S+)/)
- encountered_options = true
- opts[:font] = m[1].strip
- position += m[0].length
- else
- if not encountered_options
- opts[:message] << input[position]
- end
- position += 1
- end
-
- end
-
- opts[:message] = opts[:message].strip
-
- opts
- end
-
- def uses_emoji(opts)
- (opts[:space][0] == ':' and opts[:space][-1] == ':') or (opts[:ink][0] == ':' and opts[:ink][-1] == ':')
- end
-
- get '/' do
- 'FigBot'
- end
-
- post '/' do
- content_type :json
- {
- response_type: 'in_channel',
- text: fig(params[:text])
- }.to_json
- end
|