-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
69 lines (57 loc) · 1.57 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
# 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 = ""
begin
puts "[LOG] #{params}"
unless params[:token] != ENV["OUTGOING_WEBHOOK_TOKEN"]
response = { text: "From the collection:" }
response[:attachments] = [ generate_attachment ]
response[:username] = ENV["BOT_USERNAME"] unless ENV["BOT_USERNAME"].nil?
response[:icon_emoji] = ENV["BOT_ICON"] unless ENV["BOT_ICON"].nil?
response = response.to_json
end
end
status 200
body response
end
def generate_attachment
uri = "http://scrapi.org/random?fields=title,primaryImageUrl,url"
request = HTTParty.get(uri)
puts "[LOG] #{request.body}"
@scrapiresults = JSON.parse(request.body)
# so many nil checks this is garbage rewrite me
if @scrapiresults["title"].nil?
get_title = "Unknown"
else
get_title = @scrapiresults["title"]
end
if @scrapiresults["primaryImageUrl"].nil?
get_imageurl = ""
textresponse = "No image is available at this time."
else
get_imageurl = @scrapiresults["primaryImageUrl"]
textresponse = ""
end
if @scrapiresults["url"].nil?
get_url = ""
else
get_url = @scrapiresults["url"]
end
checkurl = URI.parse(get_url)
unless checkurl.kind_of?(URI::HTTP) or checkurl.kind_of?(URI::HTTPS)
get_url = "http://metmuseum.org#{get_url}"
end
response = { title: "#{get_title}", title_link: "#{get_url}", image_url: "#{get_imageurl}", text: "#{textresponse}" }
end