-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
46 lines (36 loc) · 997 Bytes
/
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
# frozen_string_literal: true
ENV['RACK_ENV'] ||= 'development'
require './lib/dotenv'
require 'bundler/setup'
Bundler.require 'default', ENV['RACK_ENV']
require 'active_record'
require './models/item'
# The App
class App < Sinatra::Base
MIN_SCORES = [0, 50, 100, 200, 300, 400, 500, 750, 1000].freeze
def self.boot
url = ENV['DATABASE_URL']
ActiveRecord::Base.establish_connection url
end
def self.logger
@@logger ||= Logger.new STDOUT # rubocop:disable Style/ClassVars
end
before do
@title = 'HackiNews'
end
get '/' do
@q = params['q']
@min_score = (params['min_score'] || 50).to_i
@next_min_score =
MIN_SCORES[(MIN_SCORES.index(@min_score) || 0) + 1] || Float::INFINITY
@stories = Item.min_score(@min_score).by_time.limit 500
@stories = @stories.title_matches @q if @q
erb :index
end
get '/stories/:id' do
@story = Item.find params[:id]
@title += " - #{@story.data['title']}"
erb :story
end
end
App.boot