/
app.rb
248 lines (204 loc) · 6.82 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
require 'sinatra/base'
require 'json'
require 'slim'
require 'net/http'
require 'cocoapods-core'
require 'yaml'
require_relative 'spec_extensions'
require_relative 'lib/pod_quality_estimate'
require 'sprockets'
require 'set'
require 'digest/md5'
class App < Sinatra::Base
configure do
use Rack::Deflater
end
helpers do
# Note: This is a hack that needs to be extracted into the
# shared directory for comfortable inclusion.
#
def shared_partial(source)
slim :"../shared/includes/_#{source}"
end
def partial(source)
slim :"partials/_#{source}"
end
def data(name)
YAML.load_file "data/#{name}.yaml"
end
def quality_indicator_svg
@quality_indicator_svg ||= File.read('assets/images/pod.svg')
end
def quality_indicator_group input
input >= 100 ? "5" : (input / 20) + 1
end
end
not_found do
status 404
slim :not_found
end
# Some special cases for the routing
#
get '/' do
slim :index
end
get '/get-started' do
redirect "https://github.com/cocoapods/cocoapods/issues?q=is%3Aopen+is%3Aissue+label%3Ad1%3Aeasy"
end
get '/opensearch.xml' do
content_type "application/xml"
slim :opensearch, :layout => false
end
# Set up dynamic part.
#
require_relative 'domain'
# Methods for the dynamic part.
#
DB.entities.each do |entity|
name = entity.plural
define_method name do
DB[name]
end
end
# Gets a Pod Page
#
get '/pods/:name' do
STDOUT.sync = true
result = metrics.where(pods[:deleted] => false, pods[:normalized_name] => params[:name].downcase).first
unless result
result = pods.where(pods[:deleted] => false, pods[:normalized_name] => params[:name].downcase).first
halt 404, "404 - Pod not found" unless result
# Support redirecting to the pods homepage if we can't do it.
version = pod_versions.where(pod_id: result["id"]).sort_by { |v| Pod::Version.new(v.name) }.last
commit = commits.where(pod_version_id: version.id, deleted_file_during_import: false).first
pod = Pod::Specification.from_json commit.specification_data
redirect pod.homepage
end
@content = pod_page_for_result result
slim :pod_page
end
get '/pods/:name/inline' do
response['Access-Control-Allow-Origin'] = '*'
result = metrics.where(pods[:deleted] => false, pods[:name] => params[:name]).first
halt 404, "404 - Pod not found" unless result
pod_page_for_result result
end
get '/pods/:name/changelog' do
response['Access-Control-Allow-Origin'] = '*'
result = metrics.where(pods[:deleted] => false, pods[:name] => params[:name]).first
halt 404, "404 - Pod not found" unless result
changelog_url = result.cocoadocs_pod_metric["rendered_changelog_url"]
halt 404, "404 - Pod does not have an associated CHANGELOG" unless changelog_url
res = Net::HTTP.get_response(URI(changelog_url))
return res.body.force_encoding('UTF-8') if res.is_a?(Net::HTTPSuccess)
halt 404, "404 - CHANGELOG not found at #{changelog_url}"
end
get '/owners/:id' do
@owner = owners.where(:id => params[:id]).first
halt 404, "404 - Owner not found" unless @owner
pod_ids = Set.new owners_pods.where(:owner_id => @owner[:id]).map do |owners_pod|
owners_pod[:pod_id]
end
all_dbs = metrics.join(:stats_metrics).on(:id => :pod_id)
@pods = all_dbs.where(pods[:deleted] => false, pods[:id] => pod_ids).sort_by { |pod| pod[:cocoadocs_pod_metric][:quality_estimate] || 0 }.reverse
gravatar = Digest::MD5.hexdigest(@owner.email.downcase)
@gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar}.png?d=retro&r=PG&s=240"
@page_title = "#{@owner[:name]}'s account on CocoaPods.org"
@page_description = "#{@pods.length} pods by #{@owner[:name]}."
slim :owner
end
get '/pods/:name/quality' do
@name = params[:name]
@quality, response_code = PodQualityEstimate.load_quality_estimate(@name)
@page_title = "#{@name}s Quality Estimate on CocoaPods.org"
if response_code == 404
not_found
else
slim :pod_quality
end
end
def pod_page_for_result result
@pod_db = result.pod
@metrics = result.github_pod_metric
@cocoadocs = result.cocoadocs_pod_metric
@stats = stats_metrics.where(pod_id: @pod_db.id).first
@version = pod_versions.where(pod_id: @pod_db.id).sort_by { |v| Pod::Version.new(v.name) }.last
@owners = owners_pods.join(:owners).on(:owner_id => :id).where(pod_id: @pod_db.id).to_a
@commit = commits.where(pod_version_id: @version.id, deleted_file_during_import: false).order_by(:created_at.desc).first
@pod = Pod::Specification.from_json @commit.specification_data
@page_title = "#{@pod.name} on CocoaPods.org"
@page_description = @pod.summary
uri = URI(@cocoadocs["rendered_readme_url"])
res = Net::HTTP.get_response(uri)
@readme_html = res.body.force_encoding('UTF-8') if res.is_a?(Net::HTTPSuccess)
# @readme_html = ""
slim :pod, :layout => false
end
# Helper method that will give you a
# joined pods/metrics query proxy.
#
def metrics
pods.outer_join(:github_pod_metrics).on(:id => :pod_id).join(:cocoadocs_pod_metrics).on(:id => :pod_id)
end
# Setup assets.
#
sprockets = Sprockets::Environment.new
# Generate an assets hash once on startup.
#
require 'securerandom'
ASSETS_HASH = SecureRandom.hex
add_asset_hash = ->(path) do
head, dot, ext = path.rpartition('.')
"#{head}-#{ASSETS_HASH}#{dot}#{ext}"
end
sprockets.context_class.class_eval do
define_method :asset, &add_asset_hash
end
define_method :asset, &add_asset_hash
define_method :deasset do |path|
path.slice! "-#{ASSETS_HASH}"
path
end
set :assets, sprockets
# Configure sprockets
["img", "js", "fonts", "includes", "sass"].each do |shared|
settings.assets.append_path "shared/#{shared}"
end
Dir["assets/*"].each do |file|
settings.assets.append_path file
end
get "/javascripts/:file.js" do
content_type "application/javascript"
settings.assets["#{deasset(params[:file])}.js"]
end
get "/stylesheets/:file.css" do
content_type "text/css"
settings.assets["#{deasset(params[:file])}.css"]
end
get "/images/:file.svg" do
content_type "image/svg+xml"
settings.assets["#{deasset(params[:file])}.svg"]
end
get "/flashes/:file.swf" do
content_type "application/x-shockwave-flash"
settings.assets["#{deasset(params[:file])}.swf"]
end
["images", "favicons"].each do |folder|
get "/#{folder}/:file" do
content_type "image/png"
settings.assets["#{deasset(params[:file])}"]
end
end
# If it can't be found elsewhere, it's
# probably represented by a slim file.
# E.g. /about -> /about.slim
#
get '/:filename' do
name = params[:filename]
if File.exists? "views/#{name}.slim"
slim :"#{name}"
else
halt 404
end
end
end