Skip to content

Commit e6b55e8

Browse files
committed
c3s2 mostly done
1 parent 3628f2a commit e6b55e8

11 files changed

+498
-87
lines changed

_sessions/c3s1/7_homework.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,41 @@ title: Homework
55
{% exercise %}
66
1. Finish the exercises from class.
77
2. Do the [Codecademy Ruby track](http://www.codecademy.com/tracks/ruby) Sections 1 and 2.
8+
3. Get a [Heroku](https://www.heroku.com/) account and upload an ssh key (see below).
9+
{% endexercise %}
10+
11+
### About Heroku
12+
13+
[Heroku](https://www.heroku.com/) is a cloud-based web hosting solution, specifically designed for hosting web applications. It started out as a Rails host, but has since branched out into other frameworks (e.g. Sinatra, Node.js) and languages (e.g. Java, Python).
14+
15+
They aim to completely manage your apps online infrastructure: you provide them with the code, they set it up and run it online. If you have a lot of users and want to scale your app up, they make it easy to allocate more resources so you can handle the increased traffic (for a price!).
16+
17+
### Setting up an account
18+
19+
Before you deploy your app to heroku you need to create an account.
20+
21+
{% exercise %}
22+
1. Sign up for an account at [Heroku](https://www.heroku.com/).
23+
2. After you log in, download and install the heroku toolbelt. (This will allow you to interact with heroku from the command line).
24+
{% endexercise %}
25+
26+
### Setting up ssh with heroku
27+
28+
The final part of the set up is to upload your ssh keys to Heroku. You might remember about ssh keys from the initial installation instructions: they're basically a way of identifying yourself to a server without typing in your password each time.
29+
30+
The heroku toolbelt makes this very easy. On the command line (in any folder) type:
31+
32+
$ heroku keys:add
33+
34+
You can check it worked by typing:
35+
36+
$ heroku keys
37+
38+
It should list the ssh key that you just added.
39+
40+
{% exercise %}
41+
Upload your ssh key to heroku by running
42+
43+
$ heroku keys:add
44+
845
{% endexercise %}

_sessions/c3s2/1_conditionals.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
title: Conditionals
3+
---
4+
5+
So far we've just used ruby to evaluate simple expressions. Coding becomes a lot more interesting when you can use your code for simple logic tasks:
6+
7+
{% highlight ruby %}
8+
if age >= 18
9+
"Here's the vodka you wanted."
10+
else
11+
"Move along please."
12+
end
13+
{% endhighlight %}
14+
15+
If the code within the `if` block will only be run if the statement is true. If the statement is false, the code in the `else` block will be run instead.
16+
17+
### Comparisons
18+
19+
There are some basic comparisons that we will be useful when using if statments
20+
21+
Symbol | Meaning
22+
---- | ----
23+
`==` | Is equal to
24+
`>` | Greater than
25+
`<` | Less than
26+
`>=` | Greater than or equal to
27+
`<=` | Less than or equal to
28+
`!=` | Not equal to
29+
30+
Comparisons evaluate to `true` or `false`.
31+
32+
{% exercise %}
33+
Working with a partner, try out each of these comparisons in irb:
34+
{% highlight ruby %}
35+
4 == 5
36+
37+
'five'.length > 5
38+
39+
a = 20
40+
a <= 20
41+
42+
true >= false
43+
44+
'aardvark' < 'anteater'
45+
46+
'we' != 'done yet'
47+
{% endhighlight %}
48+
{% endexercise %}
49+

_sessions/c3s2/2_post_requests.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
title: Post requests revisited
3+
---
4+
5+
Last time we looked at responding to `get` requests:
6+
7+
{% highlight ruby %}
8+
get '/' do
9+
"Hello there!"
10+
end
11+
{% endhighlight %}
12+
13+
As you know from the Introduction to Web Programming course, `GET` is only one of several types of web requests. Another is the `POST` request, which is commonly used for submitting data from a web form.
14+
15+
Suppose we have the following HTML form:
16+
17+
{% highlight html %}
18+
<form method="post" action='/'>
19+
<input type='text' name='user_name'>
20+
<input type='text' name='user_age'>
21+
22+
<input type='submit'>
23+
</form>
24+
{% endhighlight %}
25+
26+
The form will submit via a `POST` request to the root url, `/`. We can respond to this using the following sinatra block:
27+
28+
{% highlight ruby %}
29+
post '/' do
30+
name = params[:user_name]
31+
age = params[:user_age]
32+
33+
"Hello #{name}. You are #{age} years old."
34+
end
35+
{% endhighlight %}
36+
37+
Note that, like the words matched in the url, the value of the `user_name` field is made available in the `params` hash. (If you want to have a look at the params hash you could put a `raise params.inspect` at the beginning of the method.)
38+
39+
{% exercise %}
40+
1. **Fork** the repository [https://github.com/code61/sinatra_c3s2](https://github.com/code61/sinatra_c3s2) (using the fork button on the top right of the repository page on github).
41+
2. Clone down your fork of the repository onto your laptop.
42+
3. Open the `sinatra_c3s2` project in Sublime Text and have a read through `app.rb`. See if you can predict what the app will do.
43+
4. Run the app from the command line (`ruby app.rb`), to see if you were right.
44+
{% endexercise %}

_sessions/c3s2/3_deploying.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
title: App deployment
3+
---
4+
5+
We're now going to look at how to get the Sinatra app online, a process known as *deployment*. We're going to deploy the app to [Heroku](heroku.com), which offers easy hosting for a variety of web frameworks - all you need to do is push up a git repository, and they take care of everything else!
6+
7+
You should have set up a heroku account and uploaded your ssh keys for homework.
8+
9+
### Preparation
10+
11+
There are a few files `sinatra_c3s2` that we need if we're going to deploy the app to heroku:
12+
13+
* config.ru
14+
* Gemfile
15+
16+
`config.ru` contains the lines
17+
18+
{% highlight ruby %}
19+
require './app.rb'
20+
run Sinatra::Application
21+
{% endhighlight %}
22+
23+
These lines are to tell Heroku which file to find your application (`app.rb`) and how to run it (using Sinatra).
24+
25+
The `Gemfile` is a way of specifying which gems your project uses. Heroku needs to know this so it knows which libraries it needs to run your app. The `Gemfile` is also really helpful way of remembering yourself which gems you need and is invaluable when working with others.
26+
27+
{% highlight ruby %}
28+
# Gemfile
29+
30+
source 'https://rubygems.org'
31+
32+
gem 'sinatra'
33+
{% endhighlight %}
34+
35+
Along with rubygems, ruby has a tool called bundler. Bundler helps you manage your ruby gems. If you run `bundle install` it will look in the `Gemfile` and install any gems that you don't already have.
36+
37+
$ bundle install
38+
39+
will also create a `Gemfile.lock` which specifies the exact version of the gems (e.g. the sinatra library) that you are using. This means that heroku (and anyone else you're collaborating with using git) now knows exactly which gems you are using and can pick to use the same versions. You need to check this in to your repository:
40+
41+
$ git add --all
42+
$ git commit -m "Added Gemfile.lock"
43+
44+
### Deploying to Heroku
45+
46+
Once your app is prepared, the first thing you need to do is create a new empty heroku application. To do this you use the `heroku create` command:
47+
48+
$ heroku create
49+
Creating arcane-gorge-2129... done, stack is cedar
50+
http://arcane-gorge-2129.herokuapp.com/ | [email protected]:arcane-gorge-2129.git
51+
Git remote heroku added
52+
53+
You'll see that it created an app for you. In my case the app is called `arcane-gorge-2129` and can be found at http://arcane-gorge-2129.herokuapp.com. It also added a git remote for you.
54+
55+
$ git remote -v
56+
heroku [email protected]:arcane-gorge-2129.git (fetch)
57+
heroku [email protected]:arcane-gorge-2129.git (push)
58+
origin [email protected]:code61/sinatra_project_1.git (fetch)
59+
origin [email protected]:code61/sinatra_project_1.git (push)
60+
61+
### A bit about git remotes
62+
63+
64+
### Deploying
65+
66+
You deploy your repository by pushing to this remote. Heroku will serve whatever is in the `master` branch. Here we push the `master` branch up to the `heroku` remote:
67+
68+
$ git push heroku master
69+
Counting objects: 11, done.
70+
Delta compression using up to 2 threads.
71+
Compressing objects: 100% (6/6), done.
72+
Writing objects: 100% (11/11), 1.08 KiB, done.
73+
Total 11 (delta 1), reused 0 (delta 0)
74+
75+
-----> Ruby/Rack app detected
76+
-----> Using Ruby version: ruby-1.9.3
77+
-----> Installing dependencies using Bundler version 1.3.2
78+
Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin --deployment
79+
Fetching gem metadata from https://rubygems.org/..........
80+
Fetching gem metadata from https://rubygems.org/..
81+
Installing rack (1.5.2)
82+
Installing rack-protection (1.5.0)
83+
Installing tilt (1.4.1)
84+
Installing sinatra (1.4.3)
85+
Using bundler (1.3.2)
86+
Your bundle is complete! It was installed into ./vendor/bundle
87+
Cleaning up the bundler cache.
88+
-----> Discovering process types
89+
Procfile declares types -> (none)
90+
Default types for Ruby/Rack -> console, rake, web
91+
92+
-----> Compiled slug size: 25.0MB
93+
-----> Launching... done, v3
94+
http://arcane-gorge-2129.herokuapp.com deployed to Heroku
95+
96+
To [email protected]:arcane-gorge-2129.git
97+
* [new branch] master -> master
98+
99+
After you push, heroku automatically updates the app and launches it for you. You can now see the app by visiting the url (in this case http://arcane-gorge-2129.herokuapp.com).
100+
101+
{% exercise %}
102+
Deploy your `sinatra_c3s2` app to Heroku:
103+
1. Install your bundle to get a `Gemfile.lock`. In your `sintara_c3s2` directory run:
104+
105+
$ bundle install
106+
107+
2. Add your work (and new `Gemfile.lock`) to your repository:
108+
109+
$ git add --all
110+
$ git commit -m "Added Gemfile.lock"
111+
112+
3. Create a new heroku app:
113+
114+
$ heroku create
115+
116+
4. Push your work to Heroku
117+
118+
$ git push heroku master
119+
120+
(for subsequent updates you should be ok with just `git push heroku`).
121+
5. Visit the url that heroku provide, to check that your app is running!
122+
{% endexercise %}

_sessions/c3s2/3_templates.md

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)