-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.rb
88 lines (78 loc) · 2.13 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# encoding: utf-8
require "rubygems"
require "bundler/setup"
Bundler.require(:default)
configure do
# Load .env vars
Dotenv.load
# Disable output buffering
$stdout.sync = true
end
get "/" do
""
end
post "/" do
response['content-type'] = 'application/json'
response = ""
begin
puts "[LOG] #{params}"
params[:text] = params[:text]
unless params[:token] != ENV["OUTGOING_WEBHOOK_TOKEN"]
response = { text: generate_text }
response[:response_type] = "in_channel"
response[:attachments] = [ generate_attachment ]
response = response.to_json
end
end
status 200
body response
end
def generate_request
@user_query = params[:text].gsub(/\s/,'+')
if @user_query.length == 0
uri = "https://api.deckbrew.com/mtg/colors"
else
uri = "https://api.deckbrew.com/mtg/cards?name=#{@user_query}"
end
request = HTTParty.get(uri)
puts "[LOG] #{request.body}"
result = JSON.parse(request.body)
end
def generate_text
if generate_request[0].nil?
response = "No matching card found."
else
@cardname = generate_request[0]["name"]
response = "#{@cardname}"
end
response
end
def generate_attachment
replacements = [ ["{W}", ":white_circle:"], ["{U}", ":large_blue_circle:"],["{B}", ":black_circle:"],["{R}", ":red_circle:"],["{G}", ":tennis:"],["{", ""],["}", ""] ]
if generate_request[0].nil?
response = ""
else
@cardtext = generate_request[0]["text"]
@imageurl = generate_request[0]["editions"][0]["image_url"]
@types = generate_request[0]["types"][0]
@cost = generate_request[0]["cost"]
replacements.each {|replacement| @cost.gsub!(replacement[0], replacement[1])}
replacements.each {|replacement| @cardtext.gsub!(replacement[0], replacement[1])}
response = {
text: "#{@cardtext}",
fields: [
{
"title": "Types",
"value": "#{@types}",
"short": true
},
{
"title": "Cost",
"value": "#{@cost}",
"short": true
},
],
image_url: "#{@imageurl}" }
end
response
end