-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.rb
251 lines (200 loc) · 7.27 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
249
250
251
require "bundler/setup"
require 'sinatra'
require "sinatra/reloader"
require 'octokit'
require 'haml'
require 'json'
require 'sinatra/flash'
require 'sinatra/redirect_with_flash'
require 'sinatra/activerecord'
require 'sinatra/assetpack'
require "sinatra/namespace"
require 'will_paginate'
require 'will_paginate/active_record'
require 'friendly_id'
require './config/environments' #database configuration
require './env' if File.exists?('env.rb')
enable :sessions
module WillPaginate
module ViewHelpers
def page_entries_info(collection, options = {})
entry_name = options[:entry_name] || (collection.empty?? 'entry' : collection.first.class.name.underscore.sub('_', ' '))
if collection.total_pages < 2
case collection.size
when 0; "(No Extensions)"
when 1; "(1 Extension Available)"
else; " "
end
else
%{(%d - %d of %d)} % [
collection.offset + 1,
collection.offset + collection.length,
collection.total_entries
]
end
end
end
end
class Extension < ActiveRecord::Base
include FriendlyId
friendly_id :name, :use => :slugged
validates_uniqueness_of :url, {:message => 'Ooops! It looks like this extension has already been added.'}
self.per_page = 20
end
configure do
set :root, File.dirname(__FILE__) # You must set app root
register Sinatra::AssetPack
assets {
serve '/js', from: 'app/js' # Default
serve '/css', from: 'app/css' # Default
serve '/img', from: 'app/img' # Default
# The second parameter defines where the compressed version will be served.
# (Note: that parameter is optional, AssetPack will figure it out.)
# The final parameter is an array of glob patterns defining the contents
# of the package (as matched on the public URIs, not the filesystem)
js :app, ['/js/main.js']
}
end
configure :development do
register Sinatra::Reloader
end
get '/' do
#hacky, TODO make this more robust
ids = [37, 28, 12]
begin
@featured = Extension.find(ids)
rescue ActiveRecord::RecordNotFound => e
@featured = nil
end
@extensions = Extension.paginate(:page => params[:page], :order => sort_column + ' ' + sort_direction)
haml :index
end
get '/extensions' do
haml :index
end
post '/extensions' do
unless params[:project_url].empty?
#%r{github\.com[:/](.+?)/(.+?)(?:\.git|)$}i.match(params[:project_url])
begin
project_url = %r{github\.com[:/](.+?)/(.+?)(?:\.git)$}i.match(params[:project_url])
username = project_url[1]
reponame = project_url[2]
rescue NoMethodError => e
halt 400, "That URL looks funny, make sure you use the github SSH URL for your repo"
end
else
halt 400, "We appreciate minimalism, but you still actually have to provide a URL!"
end
begin
client = Octokit::Client.new \
:client_id => ENV['Github_Client_ID'],
:client_secret => ENV['Github_Client_Secret']
repo_info = client.repository("#{username}/#{reponame}")
rescue Octokit::NotFound => e
halt 400, "Slow down turbo! Double check that URL because the repo doesn't exist."
end
begin
manifest_data = get_manifest_data(client, username, reponame)
rescue Octokit::NotFound => e
halt 400, "Dang! Make sure you have a sache.json file in your repo."
end
manifest_hash = set_manifest_hash(manifest_data, repo_info, username, reponame, params[:project_url])
@extension = Extension.new(manifest_hash)
if @extension.save
flash.now[:notice] = 'Sweeeeet! Thanks for adding your exension!'
else
status 409
flash.now[:error] = @extension.errors.first[1]
end
end
get '/tag/:tag' do
@extensions = Extension.where("? = ANY (tags)", params[:tag]).paginate(:page => params[:page], :order => 'created_at DESC')
haml :tag
end
get '/search' do
@extensions = search(params[:query]).paginate(:page => params[:page], :order => 'created_at DESC')
haml :search
end
get '/:user' do
@extensions = Extension.where(:author => params[:user]).paginate(:page => params[:page], :order => 'created_at DESC')
haml :user
end
get '/project/:project_name' do
@extensions = Extension.friendly.find(params[:project_name])
haml :project
end
get '/promote' do
haml :promote
end
namespace '/api' do
before {content_type :json}
get '/extensions' do
@extensions = Extension.all.to_json
end
get '/extensions/:id' do
@extension = Extension.find(params[:id]).to_json
end
get '/users/:user' do
@extensions = Extension.where(:author => params[:user]).to_json
end
get '/search' do
@extensions = search(params[:query]).to_json
end
end
not_found { haml :'404' }
helpers do
def search q
Extension.where("(keywords ILIKE ?) OR (name ILIKE ?)", '%' + q + '%', '%' + q + '%')
end
def truncate w
words = w.split()
return words[0..40].join(' ') + (words.length > 40 ? '...' : '')
end
def sort_column
Extension.column_names.include?(params[:sort]) ? params[:sort] : "created_at"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
end
def sortable(column, title)
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
"<a data-sort='#{column}' data-direction='#{direction}' class='#{css_class}'>" + title + "</a>"
end
def get_manifest_data(client, username, reponame)
#begin
manifest_data = client.contents("#{username}/#{reponame}", :path => 'sache.json', :accept => "application/vnd.github-blob.raw")
#rescue Octokit::NotFound => e
#halt 400, "Dang! Make sure you have a sache.json file in your repo."
#end
end
def set_manifest_hash(data, repo, username, reponame, url)
hash = JSON.parse(data)
parsed_params = { project_name: reponame, author: username, url: url, last_commit: repo.updated_at, stars: repo.watchers, keywords: hash["tags"].join(', '), website: repo.website, docs: repo.docs }
hash.merge!(parsed_params)
end
def set_github_client
client = Octokit::Client.new \
:client_id => ENV['Github_Client_ID'],
:client_secret => ENV['Github_Client_Secret']
end
def update_all
@extensions = Extension.all
@extensions.each do |e|
update(e.id)
end
end
def update id
client = set_github_client()
e = Extension.find(id)
begin
repo = client.repository("#{e.author}/#{e.project_name}")
manifest_data = get_manifest_data(client, e.author, e.project_name)
manifest_hash = set_manifest_hash(manifest_data, repo, e.author, e.project_name, e.url)
manifest_hash.each{|k, v|; e[k] = v; }
e.save
rescue Octokit::NotFound => k
e.destroy
end
end
end