Skip to content

Commit fabc277

Browse files
committed
Adding docco.
Doing some literate programming with Docco. Sweeet.
1 parent df9fcda commit fabc277

3 files changed

Lines changed: 138 additions & 24 deletions

File tree

Gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ gem "pony", "~>1.0.1"
1818
gem "sinatra-content-for"
1919
gem "exceptional"
2020

21+
group :development do
22+
gem "rocco"
23+
gem "pygmentize"
24+
end
25+
2126
group :test do
2227
gem "launchy", "~>0.3.7"
2328
gem "capybara", "~>0.3.9"

Gemfile.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,24 @@ GEM
4242
activesupport (>= 2.3.4)
4343
jnunemaker-validatable (~> 1.8.4)
4444
plucky (~> 0.3.6)
45+
mustache (0.12.0)
4546
nokogiri (1.4.4)
4647
plucky (0.3.6)
4748
mongo (~> 1.1)
4849
polyglot (0.3.1)
4950
pony (1.0.1)
5051
mail (> 2.0)
52+
pygmentize (0.0.2)
5153
rack (1.2.1)
5254
rack-flash (0.1.1)
5355
rack
5456
rack-test (0.5.6)
5557
rack (>= 1.0)
5658
rake (0.8.7)
5759
rdiscount (1.6.5)
60+
rocco (0.5)
61+
mustache
62+
rdiscount
5863
rspec (2.1.0)
5964
rspec-core (~> 2.1.0)
6065
rspec-expectations (~> 2.1.0)
@@ -98,8 +103,10 @@ DEPENDENCIES
98103
launchy (~> 0.3.7)
99104
mongo_mapper (~> 0.8.4)
100105
pony (~> 1.0.1)
106+
pygmentize
101107
rack-flash (~> 0.1.1)
102108
rdiscount (~> 1.6.5)
109+
rocco
103110
rspec (~> 2.1.0)
104111
sinatra (~> 1.1)
105112
sinatra-content-for

hackety.rb

Lines changed: 126 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,128 @@
11
# encoding: utf-8
22

3-
#first off, we need to include rubygems and sinatra
4-
require 'rubygems'
3+
# This is the source code for the [Hackety Hack website][hw]. Hackety Hack is
4+
# the easiest way to learn programming, and so our documentation should be
5+
# top-notch.
6+
#
7+
# To get started, you'll need to install some prerequisite software:
8+
#
9+
# **Ruby** is used to power the site. We're currently using ruby 1.9.2p0. I
10+
# highly reccomend that you use [rvm][rvm] to install and manage your Rubies.
11+
# It's a fantastic tool. If you do decide to use `rvm`, you can install the
12+
# appropriate Ruby and create a gemset by simply `cd`-ing into the root project
13+
# directory; I have a magical `.rvmrc` file that'll set you up.
14+
#
15+
# **MongoDB** is a really awesome document store. We use it to persist all of
16+
# the data on the website. To get MongoDB, please visit their
17+
# [downloads page](http://www.mongodb.org/downloads) to find a package for your
18+
# system.
19+
#
20+
# After installing Ruby and MongoDB, you need to aquire all of the Ruby gems
21+
# that we use. This is pretty easy, since we're using **bundler**. Just do
22+
# this:
23+
#
24+
# $ gem install bundler
25+
# $ bundle install
26+
#
27+
# That'll set it all up! Then, you need to make sure you're running MongoDB.
28+
# I have to open up another tab in my terminal and type
29+
#
30+
# $ mongod
31+
#
32+
# to get this to happen. When you're done hacking, you can hit ^-c to stop
33+
# `mongod` from running.
34+
#
35+
# To actually start up the site, just
36+
#
37+
# $ rackup
38+
#
39+
# and then visit [http://localhost:9292/](http://localhost:9292). You're good
40+
# to go!
41+
#
42+
# [hw]: http://hackety-hack.com
43+
# [rvm]: http://rvm.beginrescueend.com/
44+
45+
#### About hackety.rb
46+
#
47+
# This file is the main entry point to the application. It has three main
48+
# purposes:
49+
#
50+
# 1. Include all relevant gems and library code.
51+
# 2. Configure all settings based on our environment.
52+
# 3. Set up a few basic routes.
53+
#
54+
# Everything else is handled by code that's included from this file.
55+
56+
#### Including gems
57+
58+
# We need to require rubygems and bundler to get things going. Then we call
59+
# `Bundler.setup` to get all of the magic started.
560

6-
#this bundler stuff needed for heroku
61+
require 'rubygems'
762
require 'bundler'
863
Bundler.setup
964

65+
# We use [sinatra](http://sinatrarb.com/) for our web framework. Sinatra is
66+
# very light and simple. Good stuff.
1067
require 'sinatra'
1168

12-
#this lets us send emails
69+
# [Pony](https://github.com/adamwiggins/pony) is used to send emails, just like the Pony express. Also, running `gem install pony` is really satisfying.
1370
require 'pony'
1471

15-
#haml gives us all of our templates
72+
# [haml](http://haml-lang.com/) creates all of our templates. haml is concise
73+
# and expressive. I really enjoy it.
1674
require 'haml'
1775

18-
#mongomapper connects us to our database
76+
# [MongoMapper](http://mongomapper.com/) is a library we use to make it easy to
77+
# store our model classes into MongoDB.
1978
require 'mongo_mapper'
2079

21-
#rack-flash gives us nice flash messages
80+
# If you've used Rails' flash messages, you know how convenient they are.
81+
# rack-flash lets us use them.
2282
require 'rack-flash'
83+
use Rack::Flash
2384

24-
#rdiscount lets us write things using markdown
85+
# [rdiscount](https://github.com/rtomayko/rdiscount) is a fast implementation
86+
# of the Markdown markup language. The web site renders most user submitted
87+
# comment with Markdown.
2588
require 'rdiscount'
2689

27-
#we need to set up a secret to encrypt our sessions with
28-
use Rack::Session::Cookie, :secret => ENV['COOKIE_SECRET']
29-
30-
#we also have to let the world know we want to use flashes
31-
use Rack::Flash
32-
90+
# Rails has a `content_for` helper that lets you place different parts of your
91+
# view into different places in your template. This helps a lot with
92+
# javascript, and conditional stylesheets or other includes. It's so nice that
93+
# foca has written
94+
# [a Sinatra version](https://github.com/foca/sinatra-content-for).
3395
require 'sinatra/content_for'
3496

97+
# We moved lots of helpers into a separate file. These are all things that are
98+
# useful throughout the rest of the application. This file
3599
require_relative 'helpers'
36100

101+
#### Configure settings
102+
103+
# We need a secret for our sessions. This is set via an environment variable so
104+
# that we don't have to give it away in the source code. Heroku makes it really
105+
# easy to keep environment variables set up, so this ends up being pretty nice.
106+
use Rack::Session::Cookie, :secret => ENV['COOKIE_SECRET']
107+
108+
# We use [Exceptional](http://www.getexceptional.com/) to keep track of errors
109+
# that happen. This code is from their
110+
# [example documentation](http://docs.getexceptional.com/getting-started/sinatra/)
111+
# for Sinatra. It _might_ be better off inside of a config block, but I haven't
112+
# tested it in that role yet.
37113
if ENV['RACK_ENV'] == 'production'
38114
set :raise_errors, true
39115

40116
require 'exceptional'
41117
use Rack::Exceptional, ENV['EXCEPTIONAL_API_KEY']
42118
end
43119

44-
#this makes Haml escape any html by default. See here: http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#options
120+
# This makes [Haml escape any html](http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#options) by default.
45121
set :haml, :escape_html => true
46122

47-
#these configure blocks only run in one environment
123+
# The `PONY_VIA_OPTIONS` hash is used to configure `pony`. Basically, we only
124+
# want to actually send mail if we're in the production environment. So we set
125+
# the hash to just be `{}`, except when we want to send mail.
48126
configure :test do
49127
PONY_VIA_OPTIONS = {}
50128
end
@@ -53,8 +131,10 @@
53131
PONY_VIA_OPTIONS = {}
54132
end
55133

134+
# We're using [SendGrid](http://sendgrid.com/) to send our emails. It's really
135+
# easy; the Heroku addon sets us up with environment variables with all of the
136+
# configuration options that we need.
56137
configure :production do
57-
58138
PONY_VIA_OPTIONS = {
59139
:address => "smtp.sendgrid.net",
60140
:port => "25",
@@ -66,7 +146,11 @@
66146

67147
end
68148

69-
#for all environments,
149+
# We don't want to bother with running our own MongoDB server in production;
150+
# that's what The Cloud (tm) is for! So we want to double check our environment
151+
# variables, and if it appears that we'd like to connect to
152+
# [MongoHQ](https://mongohq.com/), let's do that. Otherwise, just connect to
153+
# our local server running on localhost.
70154
configure do
71155
if ENV['MONGOHQ_URL']
72156
MongoMapper.connection = Mongo::Connection.new(ENV['MONGOHQ_HOST'], ENV['MONGOHQ_PORT'])
@@ -78,27 +162,45 @@
78162
MongoMapper.connection = Mongo::Connection.new('localhost')
79163
MongoMapper.database = "hackety-development"
80164
end
81-
#require all of our models!
82-
require_directory "models"
83165
end
84166

167+
# Since Sinatra doesn't automatically load anything, we have to do it
168+
# ourselves. Remember that helpers.rb file? Well, we made a handy
169+
# `require_directory` method that, well, `require`s a whole directory. So let's
170+
# include both of our models as well as our controllers.
171+
require_directory "models"
85172
require_directory "controllers"
86173

174+
#### Set up basic routes
175+
176+
# The first thing you'll ever see when going to the website is here. It all
177+
# starts with `/`. If we're logged in, we want to just redirect to the main
178+
# activity stream. If not, let's show that pretty splash page that sings all of
179+
# our praises.
180+
#
181+
# One small note about rendering, though: Our main layout doesn't exactly work
182+
# for the main page, it's an exception. So we don't want to use our regular old
183+
# `layout.haml` file. So we tell Sinatra not to.
87184
get '/' do
88185
if logged_in?
89186
redirect "/stream"
90187
end
91188
haml :index, :layout => :plain
92189
end
93190

94-
get '/post' do
95-
haml :post
96-
end
97-
191+
# Hopefully, anyone visiting the site will think that Hackety Hack sounds
192+
# pretty cool. If they do, they'll visit the downloads page. This'll direct
193+
# them to download Hackety, and sign up for an account.
194+
#
195+
# Similar to the home page, we also don't want our layout here, either.
98196
get '/download' do
99197
haml :download, :layout => :plain
100198
end
101199

200+
# The main activity stream is the main page for the site when a user is logged
201+
# in. It lets them share what they're doing with others, and also view all of
202+
# the content that others have posted. So we grab it all, and sort it in the
203+
# opposite order that it's been updated. Wouldn't want to see old stuff!
102204
get '/stream' do
103205
@content_list = Content.all.sort{|a, b| b.updated_at <=> a.updated_at }
104206
haml :stream

0 commit comments

Comments
 (0)