trugul-bot/app.rb

61 lines
1.1 KiB
Ruby
Raw Normal View History

2015-04-15 11:00:32 -04:00
require 'sinatra'
require 'sinatra/activerecord'
require 'json'
require './environments'
set :public_folder, 'public'
2015-04-23 14:46:51 -04:00
# CONTROLLER
2015-04-15 11:00:32 -04:00
get '/' do
2015-04-23 14:46:51 -04:00
@raids = Raid.all.reverse
2015-04-15 11:00:32 -04:00
erb :index
end
post '/' do
response['Access-Control-Allow-Origin'] = 'http://trugul.com'
@raid = Raid.new(params[:raid])
if @raid.save
content_type :json
{ :message => 'Raid saved' }.to_json
else
content_type :json
{ :message => 'Raid failed to save' }.to_json
end
end
2015-04-23 14:46:51 -04:00
#HELPERS
def readable_number(value)
numbers.each do |number, symbol|
if value.to_f / number.to_f > 1
return (value.to_f / number.to_f).to_s + symbol
end
end
return value
end
def numbers
{
'1000000000000000' => 'Q',
'1000000000000' => 'T',
'1000000000' => 'B',
'1000000' => 'M',
'1000' => 'K'
}
end
# MODEL
2015-04-15 11:00:32 -04:00
class Raid < ActiveRecord::Base
def money=(value)
value = value.to_s.gsub(/[$,]/, '').to_f
write_attribute(:money, value)
end
def soldiers=(value)
value = value.to_s.gsub(/[$,]/, '').to_f
write_attribute(:soldiers, value)
end
end