Skip to content

Commit

Permalink
Updated documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
kripy committed Aug 21, 2013
1 parent b1e4890 commit d695256
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 21 deletions.
49 changes: 36 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,50 @@

Use Yes Slash No to, quickly, knock up a website that answers a question in the form of [Is It Christmas](http://isitchristmas.com/), [Is It Thanksgiving Yet?](http://www.isitthanksgivingyet.com/), [Yes, Margaret Thatcher is dead](http://www.isthatcherdeadyet.co.uk/), and [Is Julia Gillard Still Prime Minister?](http://isjuliagillardstillpm.com/). Classy.

Fast forward a few months: as pointed out on [Reddit](http://www.reddit.com/r/ruby/comments/1hmo2b/yes_slash_no_deploy_a_yes_no_website_fast/cavtpgm), Heroku resets the file system every 24 hours so the JSON file I was using to store the YES/NO variable will be reset back to its original state. This is not really good for anyone. In order to rectify this the variable will now be pulled from the apps' config variables which complicates things a little. First off, setting up config vars on Heroku. Second, updating this via their API. Thirdly there's issues with generating the API key. I've knocked out the first two issues but the third will take longer.

## Installation

Firstly, make sure you've [installed Ruby](http://www.ruby-lang.org/en/). Also, install the [Heroku Toolbelt](https://toolbelt.heroku.com/) as it includes [Foreman](https://github.com/ddollar/foreman) for running Procfile-based applications.

Next, if you don't already have one, sign up for a [Heroku](https://www.heroku.com/) account. Everything you need to know and do to deploy is in [Getting Started with Ruby on Heroku](https://devcenter.heroku.com/articles/ruby).

Then in terminal, clone me:

```
$ git clone git@github.com:/kripy/yes-slash-no yes-slash-no
$ git clone https://github.com/kripy/yes-slash-no yes-slash-no
$ cd yes-slash-no
$ git init
$ git add .
$ git commit -m "init"
$ heroku create
$ git push heroku master
```

You'll need to create an ```.env``` file in the root directory of the project for storing a single environment variable (more about this later). The file should look like this:
Make note of the app name.

To run it locally you still need to add the YES/NO variable to Heroku as we pull it from there.

```
$ heroku config:set YSN_ANSWER=no
```

Time to generate your API key. Note, if your API key falls into the wrong hands it could cause you all sorts of issues so be careful with it.

The reason you need to generate it, as per point three above, is for some reason the base64 function returns an incorrect key than the one generated off the command line meaning you need to do it manually.

As the documentation [states](https://devcenter.heroku.com/articles/platform-api-reference#authentication), HTTP basic authentication must be constructed from email address and api token as ```{email-address}:{api-token}```, base64 encoded and passed as the Authorization header for each request.

To get your Heroku API token, run ```heroku auth:token```. Your email address is the email address you used to register with Heroku.

To generate the API key run ```echo email address : auth token | base64``` from the command line. Note the space either side of the colon. They is NOT generated correctly without it. Took me a while to figure that one out.

Now, create an ```.env``` file in the root directory of the project. The file should look like this:

```
YSN_ROUTE=switch
YSN_ANSWER=NO
YSN_APP_NAME=boiling-meadow-1520
YSN_APP_KEY=XXX
YSN_APP_NAME=<your app name as noted above>
YSN_APP_KEY=<the API key you generated above>
```

Now fire it up:
Expand All @@ -34,24 +60,21 @@ The reason for this switch is simple: it allows you to flick it over without hav

## Deployment

If you don't already have one, sign up for a [Heroku](https://www.heroku.com/) account. Everything you need to know and do to deploy is in [Getting Started with Ruby on Heroku](https://devcenter.heroku.com/articles/ruby).

In terminal, cd into your app:
As you've already create the app on Heroku, commit your updated code and push:

```
$ cd yes-slash-no
$ git init
$ git add .
$ git commit -m "init"
$ git commit -m "Deploy time."
$ heroku create
$ git push heroku master
```

You'll need to add the environment variable. I've left it as ```switch``` for the purpose of my documentation.
You'll need to add the environment variables. I recommend updating ```switch``` to something more solid like a [UUID](http://www.famkruithof.net/uuid/uuidgen).

```
$ heroku config:set YSN_ROUTE=switch
$ heroku open
$ heroku config:set YSN_APP_NAME=boiling-meadow-1520
$ heroku config:set YSN_APP_KEY=XXX
```

Say hello to yes / no. Now all you need is to set up a [custom domain](https://devcenter.heroku.com/articles/custom-domains) and away you go.
Expand Down
37 changes: 29 additions & 8 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,38 +47,59 @@ class App < Sinatra::Base
@js_modernizr = js :app_js_modernizr
end

# Function allows both get / post.
def self.get_or_post(path, opts={}, &block)
get(path, opts, &block)
post(path, opts, &block)
end
helpers do
# Function allows both get / post.
def self.get_or_post(path, opts={}, &block)
get(path, opts, &block)
post(path, opts, &block)
end

# Makes the call to Heroku to get live answer.
def get_answer()
response = HTTParty.get("https://api.heroku.com/apps/" + ENV["YSN_APP_NAME"] + "/config-vars",
:headers => { "Accept" => "application/vnd.heroku+json; version=3",
"Authorization" => ENV["YSN_APP_KEY"],
"Content-Type" => "application/json"})
parsed = JSON.parse(response.body)
parsed["YSN_ANSWER"]
end
end

get "/" do
@page_title = "Website Name"
@answer = ENV["YSN_ANSWER"]
@answer = get_answer()

mustache :index
end

# Switches between Yes / No. Handy when you're on the move.
# Email the route to yourself and keep it handy.
get "/" + ENV["YSN_ROUTE"] do
answer = ENV["YSN_ANSWER"]
answer = get_answer()

if answer == "yes"
response = HTTParty.patch("https://api.heroku.com/apps/" + ENV["YSN_APP_NAME"] + "/config-vars",
:body => { :YSN_ANSWER => "no" }.to_json,
:headers => { "Accept" => "application/vnd.heroku+json; version=3",
"Authorization" => ENV["YSN_APP_KEY"],
"Content-Type" => "application/json"})
puts response
else
response = HTTParty.patch("https://api.heroku.com/apps/" + ENV["YSN_APP_NAME"] + "/config-vars",
:body => { :YSN_ANSWER => "yes" }.to_json,
:headers => { "Accept" => "application/vnd.heroku+json; version=3",
"Authorization" => ENV["YSN_APP_KEY"],
"Content-Type" => "application/json"})
puts response
end

mustache :index
end

get "/try" do
answer = get_answer()
answer = answer["YSN_ANSWER"]
puts answer

end
end

0 comments on commit d695256

Please sign in to comment.