forked from vincentsimard/js-montreal.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js-montreal.rb
executable file
·193 lines (156 loc) · 4.3 KB
/
js-montreal.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
182
183
184
185
186
187
188
189
190
191
192
#encoding: UTF-8
require 'rubygems'
require "bundler/setup"
require 'haml'
require 'open-uri'
require 'digest/md5'
require 'json'
require 'sinatra'
require 'date'
disable :run
def special_event
filename = 'data/special_event.html'
if File.exists?('data/special_event.html')
File.open(filename){ |f| f.read }
else
nil
end
end
# What could possibly go wrong?
def read_json_file(path)
JSON.parse(File.open(path){ |f| f.read })
end
# That's right our database is the file system.
module Model
# Reverse chronological
MEETUPS = read_json_file('data/meetups.json').sort{ |a,b|
b["num"] <=> a["num"] }
PURPOSE = read_json_file('data/purpose.json')
LINKS = read_json_file('data/links.json')
SPECIAL = special_event
MENU=[
{ :label => "About",
:href => "about",
:section => "about"},
{ :label => "Where is it?",
:href => "map",
:section => "map"},
{ :label => "Be a presenter",
:href => "present",
:section => "present"},
{ :label => "Archive",
:href => 'archive',
:section => 'archive'}]
# SITE = {
# :index => { :label => "Current", :href => "/", :cls => "current" },
# :archive => { :label => "Archive", :href => "archive" },
# :directions => { :label => "Where is it?", :href => "map" },
# :present => { :label => "Want to present?", :href => "present" },
# :about => { :label => "About", :href => "about" }
# }
end
helpers do
# Returns the URL for the gravatar image associated with the email
def gravaturl(email)
hash = Digest::MD5.hexdigest(email.downcase)
"http://www.gravatar.com/avatar/#{hash}"
end
# Builds the top menu like a boss.
def menu(current)
Model::MENU.map{ |m|
li_class =
[current == m[:section] ? "active" : "", m[:cls].to_s].join(" ")
"<li class=\"#{li_class}\"><a href=\"#{m[:href]}\">#{m[:label]}</a>"
}.join("")
end
# Is this meetup happening in the past?
def past?(meetup)
Date.parse(meetup["on"]) < Date.today
end
# Do we have enough speakers for the next meetup (ie, more than 1)
# If not we're gonna change the header a bit..
def booked?
meetup = Model::MEETUPS.first
meetup["speakers"].size > 1
end
def gogodate( yyyymmdd )
"#{yyyymmdd[0..3]}.#{yyyymmdd[4..5]}.#{yyyymmdd[6..7]}"
end
def zedate(meetup)
Date.parse(meetup["on"]).strftime("%A, %B %d")
end
end
before do
# We're gonna need those
@links = Model::LINKS
end
get "/meetups/*.json" do |index|
content_type :json
body = (if index == "current"
Model::MEETUPS.first
else
meetup = Model::MEETUPS.detect{|m| m["num"] == index.to_i}
meetup ||= {}
end).to_json
end
get "/meetups/*.html" do |index|
content_type :html
meetup = Model::MEETUPS.detect{|m| m["num"] == index.to_i}
haml :_meetup_mobile, :layout => false,
:locals => { :meetup => meetup }
end
get "/meetups.json" do
content_type :json
body Model::MEETUPS.to_json
end
get "/css/mobile.css" do
sass :mobile
end
get "/meetups/current/?" do
# Exclude the current meeting
haml :_meetup_mobile, :layout => false,
:locals => { :meetup => Model::MEETUPS.first }
end
get "/archive/?" do
@section = "previously"
# Exclude the current meeting
haml :meetups, :locals => { :meetups => Model::MEETUPS.reject{
|m| m == Model::MEETUPS.first }}
end
get "/?" do
@section = "index"
haml :index, :locals => { :meetup => Model::MEETUPS.first }
end
get "/about/?" do
@section = "about"
haml :about, :locals => { :purpose => Model::PURPOSE }
end
get "/present/?" do
@section = "present"
haml :present, :locals => { :purpose => Model::PURPOSE }
end
get "/mobile/?" do
haml :mobile, :layout => false
end
# Return the contents of the Yahoo Pipe
# The pipe contains good shit. It is displayed in the rainbow.
get "/data/js-links" do
content_type :json
expires (60*60*24), :public, :must_revalidate
begin
pipe = "_id=8ddf68d81456be270ea845566f3698b2&_render=json"
pipe_content =
open("http://pipes.yahoo.com/pipes/pipe.run?#{pipe}").read
body JSON.generate(JSON.parse(pipe_content)["value"]["items"])
rescue
body "[]"
end
end
get "/map/?" do
@section = "map"
haml :map
end
# Configuration
set :app_file, __FILE__
set :root, File.dirname(__FILE__)
set :public_folder, Proc.new { File.join( root, "public") }