Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .gitkeep
Empty file.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ gem 'rake'
gem 'activesupport'
gem 'sinatra'
gem 'sinatra-contrib'
gem 'shotgun'
gem 'rspec'
gem 'pg'
gem 'activerecord'
27 changes: 27 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
GEM
remote: http://rubygems.org/
specs:
activemodel (3.2.3)
activesupport (= 3.2.3)
builder (~> 3.0.0)
activerecord (3.2.3)
activemodel (= 3.2.3)
activesupport (= 3.2.3)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activesupport (3.2.3)
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
backports (2.5.3)
builder (3.0.4)
diff-lcs (1.2.4)
eventmachine (0.12.10)
i18n (0.6.0)
multi_json (1.3.5)
pg (0.13.2)
rack (1.4.1)
rack-protection (1.2.0)
rack
rack-test (0.6.1)
rack (>= 1.0)
rake (0.9.2.2)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-core (2.14.6)
rspec-expectations (2.14.3)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.4)
shotgun (0.9)
rack (>= 1.0)
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
Expand All @@ -26,12 +48,17 @@ GEM
sinatra (~> 1.3.0)
tilt (~> 1.3)
tilt (1.3.3)
tzinfo (0.3.37)

PLATFORMS
ruby

DEPENDENCIES
activerecord
activesupport
pg
rake
rspec
shotgun
sinatra
sinatra-contrib
6 changes: 6 additions & 0 deletions config/database.yml.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
host: 'localhost'
adapter: 'postgresql'
database: 'episode5'
username: XXXXXXX
encoding: 'utf8'
pool: 5
10 changes: 10 additions & 0 deletions db/migrate/001_creates_page.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreatesPage < ActiveRecord::Migration
def change
create_table :pages do |t|
t.text :content
t.integer :parent_id
t.boolean :starting_point, default: false
t.boolean :conclusion, default: false
end
end
end
1 change: 1 addition & 0 deletions db/seed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Cleaning Out
15 changes: 15 additions & 0 deletions db/setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'pg'
require 'active_record'
require 'yaml'

connection_details = YAML::load(File.open('config/database.yml'))

# Setup out connection details
ActiveRecord::Base.establish_connection(connection_details.merge({'database'=> 'postgres', 'schema_search_path'=> 'public'}))
# create the 'tv' database
ActiveRecord::Base.connection.drop_database (connection_details.fetch('database')) rescue nil
ActiveRecord::Base.connection.create_database(connection_details.fetch('database')) rescue nil
# connect to it
ActiveRecord::Base.establish_connection(connection_details)
# Migrate all the things
ActiveRecord::Migrator.migrate("db/migrate/")
21 changes: 21 additions & 0 deletions models/book.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Book

attr_reader :current_page

def initialize(starting_page)
@current_page = starting_page
end

def input(input_string)
if input_string.chomp == "A"
@current_page = current_page.options.first
elsif input_string.chomp == "B"
@current_page = current_page.options.last
end
end

def complete_game?
current_page.conclusion?
end

end
11 changes: 11 additions & 0 deletions models/page.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Page < ActiveRecord::Base

def self.starting_point
Page.where(starting_point: true).first
end

def options
Page.where(:parent_id => id).limit(2)
end

end
Empty file added spec/.gitkeep
Empty file.
33 changes: 33 additions & 0 deletions spec/book_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require_relative "spec_helper"

describe Book do
let!(:page) {Page.create(starting_point: true)}
subject { Book.new(page) }

it "should have a page" do
subject.current_page.should eq(page)
end

describe "#input" do
let!(:option_a) { Page.create(parent_id: page.id)}
let!(:option_b) { Page.create(parent_id: page.id)}

it "should receive input and opens page" do
subject.input("A")
subject.current_page.should eq(option_a)
end
it "should receive input and opens page" do
subject.input("B")
subject.current_page.should eq(option_b)
end

end

describe "#complete_game?" do

it "should know when it's done" do
subject.stub(:current_page) { stub(:conclusion? => true)}
subject.complete_game?.should be_true
end
end
end
43 changes: 43 additions & 0 deletions spec/page_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require_relative "spec_helper"

describe Page do

before(:each) do
Page.delete_all
end

it "should know if it's at the end of the road" do
page = Page.create(conclusion: true)
page.conclusion?.should be_true
end

it "should have awesome content" do
page = Page.create(content: "The fox and hound get along")
Page.find(page.id).content.should eq("The fox and hound get along")
end

context "#options" do
subject {Page.create}
let(:option_a) {Page.create(parent_id: subject.id) }
let(:option_b) {Page.create(parent_id: subject.id) }
let(:option_c) {Page.create(parent_id: subject.id) }

it "should have options for the next pages" do
subject.options.should eq([option_a, option_b])
end
end

it "should not be a starting point by default" do
Page.create.starting_point.should eq(false)
end
it "should not be a conclusion by default" do
Page.create.conclusion.should eq(false)
end


it "should have a starting point" do
the_page = Page.create(starting_point: true)
Page.starting_point.should eq(the_page)
end

end
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "rspec"
require 'bundler/setup'
require_relative '../db/setup'
require_relative "../models/page"
require_relative "../models/book"
28 changes: 24 additions & 4 deletions theweb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
require 'sinatra/reloader'
enable :sessions

get '/' do
erb :dashboard
end

post '/number' do
@number_of_randoms = session[:number_of_randoms] || 0
@number_of_randoms += 1
Expand All @@ -16,3 +12,27 @@
@the_number = rand(number_as_string)
erb :number
end

get '/' do
list = [ 'I\'m a new fan of Ice Hockey.',
'My favourite team is the Chicago Blackhawks.',
'I like Purple.',
'The next language I\'m learning is Python.'
]
@factoid = list.sample
erb :about
end

get '/adventure' do
erb :adventure
end

post '/adventure' do
answer = params.fetch('answer')
@result = case answer.downcase
when 'a' then "Go into the forest."
when 'b' then "Walk down the road."
else redirect '/adventure'
end
erb :adventure_post
end
2 changes: 2 additions & 0 deletions views/about.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>About Me</h1>
<h2>A factoid about me - <%= @factoid %></h2>
7 changes: 7 additions & 0 deletions views/adventure.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1>Choose Your Own Adventure</h1>
<h2>You wake up on a road. It's foggy and dampy. In your bag is 30 gold pieces and a bacon sandwich. Which do you choose? (Enter A or B)</h2>

<form method="POST" action="/adventure">
<input type="text" id="answer" name="answer"/>
<input type="submit" class="btn btn-large btn-primary" value="Submit"/>
</form>
2 changes: 2 additions & 0 deletions views/adventure_post.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>ADVENTURE COMPLETE!</h1>
<h2><%= @result %></h2>
5 changes: 3 additions & 2 deletions views/layout.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@
<a class="brand" href="/">Randomizer</a>
<div class="nav-collapse">
<ul class="nav">
<li class="active"><a href="#">Home</a></li>
<li class="active"><a href="/">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="/adventure">Adventure</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</div><!--/.nav-collapse -->
Expand All @@ -58,7 +59,7 @@

<!-- Main hero unit for a primary marketing message or call to action -->
<div class="hero-unit">
<%= yield %>
<%= yield %>
</div>

<!-- Example row of columns -->
Expand Down