-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.rb
125 lines (94 loc) · 3.45 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
require 'sinatra'
require 'octokit'
require 'Haml'
require 'json'
require 'sinatra/flash'
require 'sinatra/redirect_with_flash'
require 'sinatra/activerecord'
require 'sinatra/assetpack'
require 'will_paginate'
require 'will_paginate/active_record'
require './config/environments' #database configuration
enable :sessions
class Extension < ActiveRecord::Base
validates_uniqueness_of :url, {:message => 'Ooops! It looks like this extension has allready been added.'}
self.per_page = 1
end
# helpers do
# def is_pjax?
# # headers['X-PJAX']
# env['HTTP_X_PJAX']
# end
# 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
get '/' do
@extensions = Extension.paginate(:page => params[:page], :order => 'created_at DESC') #Extension.all(:order => "updated_at desc")
haml :index
end
get '/extensions.json' do
content_type :json
@extensions = Extension.all.to_json
end
get '/extensions' do
haml :index
end
post '/extensions' do
unless params[:project_url].empty?
project_url = %r{github\.com[:/](.+?)/(.+?)(?:\.git|)$}i.match(params[:project_url])
username = project_url[1]
reponame = project_url[2]
else
halt 404, "We appreciate minimalism, but you still actually have to provide a URL!"
end
begin
repo_info = Octokit.repo("#{username}/#{reponame}")
rescue Octokit::NotFound => e
halt 404, "Slow down turbo! Double check that URL because the repo doesn't exist."
end
begin
manifest_data = Octokit.contents("#{username}/#{reponame}", :path => 'sache.json', :accept => "application/vnd.github-blob.raw")
rescue Octokit::NotFound => e
halt 404, "Dang! Make sure you have a sache.json file in your repo."
end
manifest_hash = JSON.parse(manifest_data)
parsed_params = { name: reponame, author: username, url: params[:project_url], last_commit: repo_info.updated_at, watchers: repo_info.watchers, keywords: manifest_hash["tags"].join(', ')}
manifest_hash.merge!(parsed_params)
@extension = Extension.new(manifest_hash)
puts @extension
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 '/user/:user' do
@extensions = Extension.where(:author => params[:user]).paginate(:page => params[:page], :order => 'created_at DESC')
haml :user
end
get '/search' do
@extensions = Extension.where("keywords ILIKE ?", '%' + params[:query] + '%').paginate(:page => params[:page], :order => 'created_at DESC')
haml :search
end
helpers do
def truncate word
return word[0..180].gsub(/\s\w+\s*$/, '...')
end
end