-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
executable file
·117 lines (101 loc) · 2.75 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
require 'rubygems'
require 'sinatra'
require 'active_record'
require 'sinatra/activerecord'
require 'haml'
require 'less'
require 'json'
require 'open-uri'
require 'uri'
require 'nokogiri'
require 'net/http'
require 'net/https'
require 'dalli'
# Helpers
require './config/environments'
require './config/cache'
require './config/profanity'
# Models
require './models/hackathon'
require './models/repository'
# Library code
require './lib/render_partial'
require './lib/round_time'
# Stats
require './lib/stats/hackathon_profiler'
require './lib/stats/repository_profiler'
require './lib/stats/commit'
# Set Sinatra variables
set :app_file, __FILE__
set :root, File.dirname(__FILE__)
set :views, 'views'
set :public_folder, 'public'
set :haml, {:format => :html5} # default Haml format is :xhtml
# Assets
get '/application.css' do
get_cache('application-css', settings.asset_cache_for) {
less :application
}
end
get '/application.js' do
get_cache('application-js', settings.asset_cache_for) {
coffee :application
}
end
# Pages
get '/' do
redirect '/hackathons'
end
get '/hackathons' do
@hackathons = Hackathon.all
haml :hackathons, :layout => :'layouts/application'
end
get '/hackathons/:id/data.json' do
content_type :json
begin
@hackathon = Hackathon.find(params[:id])
rescue ActiveRecord::RecordNotFound
return {error: "Invalid Hackathon ID"}.to_json
end
begin
hackathon_profiler = HacktivityStats::HackathonProfiler.new(@hackathon)
hackathon_profiler.get_stats.to_json
rescue HacktivityStats::GithubRateError, JSON::ParserError => e
puts "ERROR: #{e.inspect}"
return {api_error: true}.to_json
end
end
get '/hackathons/:hackathon_id' do
begin
@hackathon = Hackathon.find(params[:hackathon_id])
rescue ActiveRecord::RecordNotFound
haml :repositories, :layout => :'layouts/application'
end
@repository = Repository.new({:hackathon_id => @hackathon.id})
@repositories = @hackathon.repositories
haml :repositories, :layout => :'layouts/application'
end
post '/hackathons/:hackathon_id/repositories' do
begin
@hackathon = Hackathon.find(params[:hackathon_id])
rescue ActiveRecord::RecordNotFound
haml :repositories, :layout => :'layouts/application'
end
@repository = Repository.new(params[:repository])
@repository.hackathon_id = @hackathon.id
@repository.original_url = @repository.original_url.gsub('.git', '')
if @repository.save
redirect "/hackathons/#{@hackathon.id}"
else
@repositories = @hackathon.repositories
haml :repositories, :layout => :'layouts/application'
end
end
delete '/repositories/:id' do
begin
@repository = Repository.find(params[:id])
rescue ActiveRecord::RecordNotFound
return { error: "Repository not found" }.to_json
end
@repository.destroy
end