-
Notifications
You must be signed in to change notification settings - Fork 5
/
shoe_store.rb
92 lines (76 loc) · 2.07 KB
/
shoe_store.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
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require 'active_record'
#require 'sinatra/activerecord'
require 'sinatra/flash'
require 'active_support/all'
require 'haml'
require 'sass'
require 'yaml'
require 'user-agent'
require 'pry'
require 'uri'
require_relative 'models/achievement'
require_relative 'models/request'
require_relative 'models/brand'
require_relative 'models/issue'
require_relative 'models/promo_code'
require_relative 'models/shoe'
require 'json'
class ShoeStore < Sinatra::Application
configure do
set :haml, { :format => :html5 }
set :protection, :except => [:json_csrf]
enable :sessions
end
configure :development, :test do
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'shoes.db'
)
end
configure :production do
db = URI.parse(ENV['DATABASE_URL'] || 'postgresql://localhost/shoe_store')
ActiveRecord::Base.establish_connection(
:adapter => 'postgresql',
:host => db.host,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)
end
end
before do
unless request.path_info.match /^\/(admin|stylesheet\.css)/
previous = Request.order(:created_at).last
@request_model = Request.create! :data => request.to_yaml, :ip_address => (request.env["HTTP_X_REAL_IP"] || request.ip)
if previous && previous.ip_address != (request.env["HTTP_X_REAL_IP"] || request.ip)
achieve! MultipleIpAddresses
end
Achievement.user_agent_achievements(UserAgent::ParsedUserAgent.new(request.user_agent || ""), @request_model)
end
@month_names = Date::MONTHNAMES.compact
end
get '/' do
@title = 'Welcome to the Shoe Store'
@brand_names = Brand.all
haml :index
end
get '/stylesheet.css' do
content_type 'text/css', :charset => 'utf-8'
sass :stylesheet
end
helpers do
def to_id(name)
name.gsub(' ', '').underscore.downcase
end
end
private
def achieve!(key)
Achievement.achieve! key, @request_model
end
def sort_shoes!
@shoes.to_a.sort! { |s1, s2| Date::MONTHNAMES.index(s1.release_month) <=> Date::MONTHNAMES.index(s2.release_month) }
end