-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnumber_facts.rb
95 lines (87 loc) · 2.34 KB
/
number_facts.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
89
90
91
92
93
94
95
require 'rubygems'
require 'net/http'
require 'open-uri'
require 'json'
require 'nokogiri'
require 'cgi'
require 'digest'
require 'action_view'
include ActionView::Helpers::DateHelper
class NumberFacts
FACTS_PER_REQUEST = 10
# http://numbersapi.com/random/trivia
# http://numbersapi.com/random/trivia?notfound=ceil
ENDPOINT = "http://numbersapi.com/random/trivia"
# TODO: get more at once, and cache them for faster facts
VALID_WORDS = %w{number}
CACHE_DURATION = 60 #seconds
APP_ROOT = File.expand_path(File.dirname(__FILE__))
CACHE_FILE = APP_ROOT+"/cache/"
attr_accessor :regex
def initialize
@regex = /^!(#{VALID_WORDS.join('|')})/i
@last_fact = 0
end
def check(query)
return trycheck(query)
rescue Exception => e
puts e.message
m = e.message
m << "\n\n --->"
m << e.backtrace.join("\n|\n")
" SWEATSTINY tell hephaestus something broke with Number Facts. Exception: #{m.to_s}"
end
def trycheck(query)
return open(ENDPOINT).read
# temporarily for demo purposes
cached = getcached(ENDPOINT) || {}
cached["date"] ||= 0
# expire cache if...
if cached["date"].to_i < (Time.now.to_i - CACHE_DURATION)
jsn = getjson(ENDPOINT)
if jsn.nil?
raise "Failed to GET Number Facts"
else
jsn["date"] ||= Time.now.to_i
setcached(ENDPOINT, jsn)
end
else
jsn = cached
end
fact = jsn['facts'][@last_fact]
@last_fact = @last_fact + 1
if @last_fact >= FACTS_PER_REQUEST
@last_fact = 0
jsn["date"] = 0
setcached(ENDPOINT, jsn)
end
return fact
end
def getjson(url)
content = open(url).read
return JSON.parse(content)
end
# safe cache! won't die if the bot dies
def getcached(url)
_cached = instance_variable_get "@cached_#{hashed(url)}"
return _cached unless _cached.nil?
path = CACHE_FILE + "#{hashed(url)}.json"
if File.exists?(path)
f = File.open(path)
_cached = JSON.parse(f.read)
instance_variable_set("@cached_#{hashed(url)}", _cached)
return _cached
end
return nil
end
def setcached(url, jsn)
instance_variable_set("@cached_#{hashed(url)}", jsn)
path = CACHE_FILE + "#{hashed(url)}.json"
File.open(path, 'w') do |f2|
f2.puts JSON.unparse(jsn)
end
end
def hashed(url)
return Digest::MD5.hexdigest(url).to_s
end
end