forked from jicksta/adhearsion-website
-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.rb
168 lines (135 loc) · 4.19 KB
/
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
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
require 'rubygems'
require 'bundler'
Bundler.require
require 'open-uri'
Sinatra.register SinatraMore::MarkupPlugin
app_start_time = Time.now
before do
cache_control :public, :must_revalidate, :max_age => 1.hour if ENV['RACK_ENV']
end
$twitter = Twitter::REST::Client.new do |config|
config.consumer_key = ENV["TWITTER_CONSUMER_KEY"]
config.consumer_secret = ENV["TWITTER_CONSUMER_SECRET"]
end
require 'singleton'
class BlogPostAggregator
include Singleton
attr_reader :last_fetch
def posts
fetch? ? fetch : @posts
end
def fetch
@last_fetch = Time.now
@posts = Hash.from_xml(open(rss_url).read)["rss"]["channel"]["item"].to_a.map do |feed_item|
pub_date = DateTime.parse(feed_item["pubDate"]) rescue Time.now
{ :title => feed_item["title"], :date => pub_date, :url => feed_item["link"], :description => feed_item["description"] }
end
end
def expired?
Time.now > last_fetch + 1.hour
end
def fetch?
!@posts || @posts.empty? || expired?
end
def hash
posts.first.hash
end
def first_post_date
first_post = posts.first
first_post && first_post[:date]
end
def rss_url
@rss_url ||= "http://mojolingo.com/tagged/adhearsion/feed/"
end
end
class HTMLwithPygments < Redcarpet::Render::HTML
def block_code(code, language)
Pygments.highlight(code, lexer: language)
end
end
Tilt.register Tilt::RedcarpetTemplate::Redcarpet2, 'markdown', 'mkd', 'md'
set :markdown, renderer: HTMLwithPygments.new(with_toc_data: true), fenced_code_blocks: true
get '/' do
@blog_posts = BlogPostAggregator.instance.posts
if ENV['RACK_ENV']
last_modified [app_start_time, BlogPostAggregator.instance.first_post_date].max
etag BlogPostAggregator.instance.hash
end
haml :index
end
get '/conference*' do
redirect 'http://adhearsionconf.com'
end
get '/examples' do
redirect '/docs/call-controllers'
end
%w{community get-help faq irc media foundation cloud support gsoc}.each do |page|
get "/#{page}" do
haml page.to_sym
end
end
post '/get-help' do
@has_errors = []
@has_errors << "Name can not be blank." if params[:yourName].empty?
@has_errors << "Email is empty or invalid." unless params[:email].match(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i)
@has_errors << "Please fill in your inquiry." if params[:query].empty?
if @has_errors.empty?
Pony.mail({
:to => '[email protected]',
:subject => 'Adhearsion Contact Request',
:body => "Message from #{params[:yourName]} - #{params[:email]}\n\n#{params[:query]}",
:via => :smtp,
:via_options => {
:address => 'smtp.gmail.com',
:port => '587',
:enable_starttls_auto => true,
:user_name => ENV['GMAIL_USERNAME'],
:password => ENV['GMAIL_PASSWORD'],
:authentication => :plain, # :plain, :login, :cram_md5, no auth by default
:domain => "adhearsion.com" # the HELO domain provided by the client to the server
}
})
@contact_status = 'success'
else
@your_name = params[:yourName]
@your_email = params[:email]
@your_query = params[:query]
@contact_status = 'error'
end
haml :'get-help'
end
{
'/api' => 'http://rubydoc.info/gems/adhearsion/frames',
'/wiki' => 'https://github.com/adhearsion/adhearsion/wiki',
'/contributing' => '/docs/contributing',
'/rss' => BlogPostAggregator.instance.rss_url,
'/download' => '/docs/getting-started/installation',
'/blather' => 'http://adhearsion.github.com/blather'
}.each_pair do |local, remote|
get local do
redirect remote
end
end
get '/docs' do
render_docs_page
end
get '/docs/*' do |page|
render_docs_page page
end
def render_docs_page(page = 'index')
toc = markdown :"docs/#{page}", renderer: Redcarpet::Render::HTML_TOC
docs = markdown :"docs/#{page}"
toc.sub! /\n<\/li>\n<\/ul>\n$/, ''
toc.sub! /^<ul>\n<li>\n<a href=\"#toc_0\">.*<\/a>\n/, ''
haml :docs, locals: {docs: docs.sub('[TOC]', toc)}
end
def title(page_title, show_title = true)
content_for(:title) { page_title.to_s }
@show_title = show_title
end
def show_title?
@show_title
end
def tweets
$twitter.user_timeline('adhearsion', count: 2)
end