forked from leereilly/github-high-scores
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
167 lines (139 loc) · 4.32 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
$:.unshift File.join(File.dirname(__FILE__),'lib')
require 'rubygems'
require 'net/http'
require 'sinatra'
require 'json'
require 'erb'
require 'uri'
require 'User'
require 'Repo'
DataMapper::Logger.new(STDOUT, :debug)
disable :show_exceptions
set :environment, :production
error do
@title = "404"
@text = "Sorry, but this cat is in another castle!"
@display_small_search = false
erb :not_found
end
get '/' do
begin
if params[:github_url]
@title = 'High Scores'
@github_url = sanitize_input params[:github_url]
@user = get_user_from_github_url(@github_url)
@repo = get_repo_from_github_url(@github_url)
@high_scores = get_high_scores(@user, @repo)
@display_small_search = true
redirect "/#{@user}/#{@repo}/high_scores/"
else
@title = 'High Scores'
@text = 'Please enter a Github repository URL'
@display_small_search = false
erb :index
end
rescue
@title = "404"
@text = "Sorry, but this cat is in another castle!"
@display_small_search = false
erb :not_found
end
end
get '/recent_searches/?' do
@repos = Repo.all(:limit => 5, :order => [ :updated_at.desc ])
puts @repos.inspect
@display_small_search = true
erb :recent_searches
end
get '/credits/?' do
erb :credits
end
get '/help/?' do
@display_small_search = true
erb :help
end
get '/about/?' do
@display_small_search = true
erb :about
end
get '/:user/:repo/?' do
@user = User::create_from_username(params[:user])
@repo = Repo::create_from_username_and_repo(params[:user], params[:repo])
@display_small_search = true
erb :repo
end
get '/:user/?' do
@user = User::create_from_username(params[:user])
@display_small_search = true
erb :user
end
not_found do
@title = "404"
@text = "Sorry, but this cat is in another castle!"
erb :not_found
end
def sanitize_input(url)
url = url.downcase
# Special rules for Github URLs starting with 'github.com'
if url[0..9] == 'github.com'
url = 'https://www.github.com' + url[9..url.size]
# Special rules for Github URLs starting with 'www.github.com'
elsif url[0..13] == 'www.github.com'
url = 'https://www.github.com' + url[13..url.size]
end
# Special rules for Github URLs ending in 'git'
if url[-4,4] == '.git'
url = url[0..-5]
end
url = url.gsub("http://", "https://")
url = url.gsub("git@github.com:", "https://www.github.com/")
url = url.gsub("git://", "https://www.")
# If someone just passes in user/repo e.g. leereilly/leereilly.net
tokens = url.split('/')
if tokens.size == 2
url = "https://www.github.com/#{tokens[0]}/#{tokens[1]}"
end
return url
end
def get_user_from_github_url(sanitized_github_url)
return sanitized_github_url.split('/')[3]
end
def get_repo_from_github_url(sanitized_github_url)
return sanitized_github_url.split('/')[4]
end
def get_high_scores(user, repo)
begin
# Kludge - three API calls
stored_user = User::create_from_username(user)
puts "Storing user: #{stored_user}"
stored_repo = Repo::create_from_username_and_repo(user, repo)
puts "Storing repo: #{stored_repo}"
contributors_url = "http://github.com/api/v2/json/repos/show/#{user}/#{repo}/contributors/anon"
contributors_feed = Net::HTTP.get_response(URI.parse(contributors_url))
contributors = contributors_feed.body
contributors_result = JSON.parse(contributors)
repository_contributors = contributors_result['contributors']
contributors_array = Array.new
repository_contributors.each do |repository_contributor|
user_hash = Hash.new
user_hash[:login] = repository_contributor['login']
user_hash[:name] = repository_contributor['name']
user_hash[:email] = repository_contributor['email']
user_hash[:gravatar_id] = repository_contributor['gravatar_id']
user_hash[:location] = repository_contributor['location']
user_hash[:contributions] = repository_contributor['contributions'].to_i
contributors_array << user_hash
end
return contributors_array
rescue
raise "Sorry, this GitHub repository doesn't seem to exist or is private"
end
end
get '/:user/:repo/high_scores/?' do
@title = "New"
@user = params[:user]
@repo = params[:repo]
@high_scores = get_high_scores(@user, @repo)
@display_small_search = true
erb :high_scores
end