-
Notifications
You must be signed in to change notification settings - Fork 1
/
webapp.rb
181 lines (154 loc) · 4.83 KB
/
webapp.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
# frozen_string_literal: true
require 'sinatra'
require 'sinatra/namespace'
require 'haml'
require_relative 'lib/eventkalender'
set :views, File.dirname(__FILE__) + '/views'
set :parser, Eventkalender::Parser.new
set :public_folder, File.dirname(__FILE__) + '/views/public'
set :sub_path, ''
# HTML pages
namespace settings.sub_path.to_s do
get '/' do
@events = settings.parser.events[:conferences].events.map do |event|
# orange: #FF8C00
<<-EOF
{
title: "#{event.name}",
start: "#{event.start_date}",
end: "#{event.end_date + 1}",
url: "#{event.description}",
color: '#28c3ab'
},
EOF
end << settings.parser.events[:meetings].events.map do |event|
# orange: #FF8C00
<<-EOF
{
title: "#{event.name}",
start: "#{event.start_date}",
end: "#{event.end_date}",
url: "#{event.description}",
color: 'orange'
},
EOF
end
@gotodate = if params[:gotodate]
@params[:gotodate]
else
Date.today
end
haml :index
end
get '/events.html' do
@events = filter(filter_params,
settings.parser.events[meetings_or_conferences].events)
haml :events
end
# Formats
get '/events.ical' do
content_type 'text/calendar'
events = filter
cal = settings.parser.to_ical_calendar(events)
cal.to_ical
end
get '/events.atom' do
content_type 'application/atom+xml'
events = filter
feed = settings.parser.events[meetings_or_conferences].to_atom(events)
feed.to_s
end
get '/events.txt' do
content_type 'text/plain'
events = filter
settings.parser.events[meetings_or_conferences].to_txt(events)
end
get '/events.json' do
content_type 'application/json'
events = filter
json = settings.parser.events[meetings_or_conferences].to_json(events)
json.to_s
end
end
# helper function
def filter_params
{ general: params[:filter], streaming: params[:streaming], idea: params[:idea] }
end
def meetings_or_conferences
params[:meetings].nil? ? :conferences : :meetings
end
# Filter for specific keywords or parameter.
# @todo
# Filtering for time and streaming is ugly implemented.
# Looping and checking multiple times should be removed, soon.
#
# @example Filter for past events
# parser.filter({ general: past, streaming: true }) #=> [#<Eventkalender::Conference:0x00000002ab5b88 … >, …]
#
# @param filter [Hash] used for filtering
# @option filter [String] :general Normal filter option
# @option filter [String] :streaming Streaming status
# @param events [Array<Event>, #events] witch schould be filtered
#
# @return [Array] with filtered events
def filter(filter = filter_params,
events = settings.parser.events[meetings_or_conferences].events)
filtered_events = case filter[:general]
# All past events
when /past/
events.find_all(&:past?)
# All upcoming events
when /upcoming/
events.find_all(&:upcoming?)
# Currently running events
when /now|today/
events.find_all(&:now?)
# Match a year
when /\d{4}/
events.find_all { |event| event.start_date.year == filter[:general].to_i }
# Return all events if no filter is set
when /meeting/
events.find_all { |event| event.class == Eventkalender::Meeting }
when /conference/
events.find_all { |event| event.class == Eventkalender::Conference }
# Return all events if no filter is set
else
events
end
# filter for idea events
# default: remove idea events
if filter[:idea] == 'true'
filtered_events
else
filtered_events = remove_idea_events(filtered_events)
end
filter_streaming(filter[:streaming], filtered_events)
end
# Filter for events with streaming status.
#
# @example Filter for events with streaming
# parser.filter_streaming('true') #=> [#<Eventkalender::Conference:0x000000036b6810 … @streaming=true>, …]
#
# @param filter [String] witch is used for filtering
# @param events [Array<Event>, #events] to filter
#
# @return [Array] events witch match the given filter
def filter_streaming(filter, events)
case filter
when /true|yes/
events.find_all(&:streaming)
when /false|no/
events.find_all { |event| event.streaming == false }
when /undefined|nil|null/
events.find_all { |e| e.streaming.nil? }
else
events
end
end
# Remove events with status idea
#
# @param events [Array<Event>, #events] to filter
# @return [Array] events with status idea
def remove_idea_events(events)
events.delete_if(&:idea?)
end