forked from jbhannah/bestsigs-wow-cacher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.rb
91 lines (74 loc) · 2.24 KB
/
web.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
require 'rubygems'
require 'sinatra/base'
require 'active_support/multibyte/chars'
require 'haml'
require 'maruku'
require 'character'
class Web < Sinatra::Base
configure :production do
require 'newrelic_rpm'
require 'gabba/gabba'
@@gabba = Gabba::Gabba.new(ENV["GA_TRACKING_ID"], "bestsigs-wow-cacher.herokuapp.com")
end
configure :production, :development do
enable :logging
end
configure do
Character.dm_setup
end
set :haml, format: :html5
get '/' do
haml :index
end
get '/stats' do
@stats = {
count: Character.count,
regions: Character.aggregate(:all.count, fields: [:region]).sort { |x,y| y[1] <=> x[1] },
realms: Character.aggregate(:all.count, fields: [:region, :realm]).sort { |x,y| y[2] <=> x[2] }
}
@stats[:regions].each do |region|
region << Character.aggregate(:all.count, fields: [:realm], conditions: [ 'region = ?', region[0] ]).count
end
haml :stats
end
post '/get-character-url' do
begin
c = Character.first_or_create({
region: params[:region].downcase,
realm: params[:realm].titleize,
char: params[:char].capitalize
})
c.update_img
@url = url("/#{c.char_path}.png")
@bbcode = "[url=\"#{c.armory_url}\"][img]#{@url}[/img][/url]"
if defined?(@@gabba)
@@gabba.ip(request.ip)
@@gabba.page_view("Get character URL: #{c.char} (#{c.realm}-#{c.region.upcase})",
request.path + "?region=#{c.region}&realm=#{c.realm}&char=#{c.char}")
end
haml :get_character_url
rescue Exception => e
c.destroy if c
logger.error "#{e.class}: #{e.message}"
"Something went wrong: " + e.message
end
end
get '/:region/:realm/:char.png' do
begin
c = Character.first_or_create({
region: params[:region].downcase,
realm: params[:realm].titleize,
char: params[:char].capitalize
})
if defined?(@@gabba)
@@gabba.ip(request.ip)
@@gabba.page_view("#{c.char} (#{c.realm}-#{c.region.upcase})", "/#{c.char_path}.png")
end
redirect c.img_uri, 303
rescue Exception => e
c.destroy if c
logger.error "#{e.class}: #{e.message}"
404
end
end
end