forked from igneus/church-calendar-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.rb
115 lines (92 loc) · 2.28 KB
/
web.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
require 'scorched'
require 'haml'
require 'calendarium-romanum'
require 'yaml'
module ChurchCalendar
class Web < Scorched::Controller
include CalendariumRomanum
config << {
static_dir: 'public'
}
render_defaults << {
layout: :_layout,
engine: :haml
}
get '/' do
render :index
end
get '/browse' do
redirect '/browse/default'
end
get '/browse/:cal' do |cal|
start_year = Time.now.year - 5
end_year = start_year + 10
l = {
start_year: start_year,
end_year: end_year,
today: Date.today,
cal: cal,
calendars: ChurchCalendar.sanctorale_repository.metadata,
}
render :browse, locals: l
end
get '/browse/:cal/:year/:month' do |cal,year, month|
year = year.to_i
month = month.to_i
date = Date.new(year, month, 1)
prepare_calendar(date, cal)
entries = []
begin
begin
entries << @cal.day(date)
rescue RangeError
if month >= 11
prepare_calendar(date, cal)
retry
end
end
date = date.succ
end until date.month != month
l = {
year: year,
month: month,
entries: entries,
cal: cal,
calendars: ChurchCalendar.sanctorale_repository.metadata,
}
render :month, locals: l
end
get '/api-doc' do
render :apidoc
end
get '/about' do
render :about
end
get '/showcase' do
render :showcase
end
def ordinal(i)
suff = {1 => 'st', 2 => 'nd', 3 => 'rd'}
"#{i}#{suff[i] || 'th'}"
end
def format_weekday(i)
%w{Sun Mon Tue Wed Thu Fri Sat}[i]
end
def format_season(s)
ss = s.to_s
ss[0].upcase + ss[1..-1]
end
def celebration_text(day, celeb)
unless celeb.title.empty?
r = celeb.rank.short_desc
return "#{celeb.title}#{', ' if r}#{r}"
end
return "#{format_weekday day.weekday}, #{ordinal day.season_week} week of #{format_season day.season}"
end
def prepare_calendar(date, cal)
repo = ChurchCalendar.sanctorale_repository
factory = repo.get_calendar_factory cal
@cal = factory.for_day date
end
end
end