Browse Source

Initial commit

master
Dylan Baker 5 years ago
commit
85b020d414
5 changed files with 115 additions and 0 deletions
  1. 2
    0
      .gitignore
  2. 9
    0
      Gemfile
  3. 26
    0
      Gemfile.lock
  4. 2
    0
      config.ru
  5. 76
    0
      figbot.rb

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
1
+bin/
2
+.bundle/

+ 9
- 0
Gemfile View File

@@ -0,0 +1,9 @@
1
+# frozen_string_literal: true
2
+
3
+ruby '2.4.1'
4
+
5
+source "https://rubygems.org"
6
+
7
+gem "figlet"
8
+gem "sinatra"
9
+gem "dotenv"

+ 26
- 0
Gemfile.lock View File

@@ -0,0 +1,26 @@
1
+GEM
2
+  remote: https://rubygems.org/
3
+  specs:
4
+    dotenv (2.5.0)
5
+    figlet (1.1.0)
6
+    mustermann (1.0.3)
7
+    rack (2.0.5)
8
+    rack-protection (2.0.3)
9
+      rack
10
+    sinatra (2.0.3)
11
+      mustermann (~> 1.0)
12
+      rack (~> 2.0)
13
+      rack-protection (= 2.0.3)
14
+      tilt (~> 2.0)
15
+    tilt (2.0.8)
16
+
17
+PLATFORMS
18
+  ruby
19
+
20
+DEPENDENCIES
21
+  dotenv
22
+  figlet
23
+  sinatra
24
+
25
+BUNDLED WITH
26
+   1.16.1

+ 2
- 0
config.ru View File

@@ -0,0 +1,2 @@
1
+require './figbot'
2
+run Sinatra::Application

+ 76
- 0
figbot.rb View File

@@ -0,0 +1,76 @@
1
+require 'figlet'
2
+require 'sinatra'
3
+require 'dotenv/load'
4
+
5
+def fig(input)
6
+  opts = parse(input)
7
+
8
+  banners = []
9
+
10
+  font = Figlet::Font.new(File.expand_path('./lib/banner.flf'))
11
+  figlet = Figlet::Typesetter.new(font)
12
+
13
+  chunks = opts[:message].chars.to_a.each_slice(5).to_a.map{|chunk| chunk.join}
14
+
15
+  chunks.each do |chunk|
16
+    banner = figlet[chunk.strip].gsub(/#/, opts[:ink]).gsub(/ /, opts[:space])
17
+
18
+    unless uses_emoji(opts)
19
+      banner = "```#{banner}```"
20
+    end
21
+
22
+    banners << banner
23
+  end
24
+
25
+  banners.join("\n")
26
+end
27
+
28
+def parse(input)
29
+  opts = {
30
+    :message => String.new,
31
+    :space => " ",
32
+    :ink => "#",
33
+  }
34
+
35
+  position = 0
36
+
37
+  encountered_options = false
38
+
39
+  while position < input.length
40
+    if m = input.slice(position..-1).match(/^space=(\S+)/)
41
+      encountered_options = true
42
+      opts[:space] = m[1].strip
43
+      position += m[0].length
44
+    elsif m = input.slice(position..-1).match(/^ink=(\S+)/)
45
+      encountered_options = true
46
+      opts[:ink] = m[1].strip
47
+      position += m[0].length
48
+    elsif m = input.slice(position..-1).match(/^font=(\S+)/)
49
+      encountered_options = true
50
+      opts[:font] = m[1].strip
51
+      position += m[0].length
52
+    else
53
+      if not encountered_options
54
+        opts[:message] << input[position]
55
+      end
56
+      position += 1
57
+    end
58
+
59
+  end
60
+
61
+  opts[:message] = opts[:message].strip
62
+
63
+  opts
64
+end
65
+
66
+def uses_emoji(opts)
67
+  (opts[:space][0] == ':' and opts[:space][-1] == ':') or (opts[:ink][0] == ':' and opts[:ink][-1] == ':')
68
+end
69
+
70
+post '/' do
71
+    content_type :json
72
+    {
73
+      response_type: 'in_channel',
74
+      text: fig(params[:text])
75
+    }.to_json
76
+end

Loading…
Cancel
Save