From e74049ff36d5fb48a47035c7140197a46786dc88 Mon Sep 17 00:00:00 2001 From: drammopo Date: Thu, 13 Feb 2014 22:17:04 +0200 Subject: [PATCH 1/9] Added Shotgun gem and simple link to the /about page in the layout. --- Gemfile | 1 + Gemfile.lock | 3 +++ theweb.rb | 4 ++++ views/about.erb | 2 ++ views/layout.erb | 4 ++-- 5 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 views/about.erb diff --git a/Gemfile b/Gemfile index 416fe44..9d537b6 100644 --- a/Gemfile +++ b/Gemfile @@ -4,3 +4,4 @@ gem 'rake' gem 'activesupport' gem 'sinatra' gem 'sinatra-contrib' +gem 'shotgun' diff --git a/Gemfile.lock b/Gemfile.lock index 3d79568..1f59487 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -14,6 +14,8 @@ GEM rack-test (0.6.1) rack (>= 1.0) rake (0.9.2.2) + shotgun (0.9) + rack (>= 1.0) sinatra (1.3.2) rack (~> 1.3, >= 1.3.6) rack-protection (~> 1.2) @@ -33,5 +35,6 @@ PLATFORMS DEPENDENCIES activesupport rake + shotgun sinatra sinatra-contrib diff --git a/theweb.rb b/theweb.rb index a531ce4..fe5304d 100644 --- a/theweb.rb +++ b/theweb.rb @@ -16,3 +16,7 @@ @the_number = rand(number_as_string) erb :number end + +get '/about' do + erb :about +end diff --git a/views/about.erb b/views/about.erb new file mode 100644 index 0000000..793a68c --- /dev/null +++ b/views/about.erb @@ -0,0 +1,2 @@ +

About Self

+

Lorem ipsum

diff --git a/views/layout.erb b/views/layout.erb index 8797f75..ba22829 100644 --- a/views/layout.erb +++ b/views/layout.erb @@ -46,7 +46,7 @@ @@ -58,7 +58,7 @@
- <%= yield %> + <%= yield %>
From 5ffefbbd782a793eed72e15cc9f6395018a19c46 Mon Sep 17 00:00:00 2001 From: drammopo Date: Fri, 14 Feb 2014 19:08:08 +0200 Subject: [PATCH 2/9] Tiger Level: Added stuff to an Array. --- theweb.rb | 6 ++++++ views/about.erb | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/theweb.rb b/theweb.rb index fe5304d..19fa53a 100644 --- a/theweb.rb +++ b/theweb.rb @@ -18,5 +18,11 @@ end get '/about' 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.shuffle.first erb :about end diff --git a/views/about.erb b/views/about.erb index 793a68c..8b23916 100644 --- a/views/about.erb +++ b/views/about.erb @@ -1,2 +1,2 @@ -

About Self

-

Lorem ipsum

+

About Me

+

A factoid about me - <%= @factoid %>

From e07607d9fba459c6d8553b1b2fe6d05c1381ba1d Mon Sep 17 00:00:00 2001 From: drammopo Date: Fri, 14 Feb 2014 19:11:10 +0200 Subject: [PATCH 3/9] Tiger Level: Moved the about page to the homepage. --- theweb.rb | 8 ++++---- views/layout.erb | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/theweb.rb b/theweb.rb index 19fa53a..75d69f1 100644 --- a/theweb.rb +++ b/theweb.rb @@ -4,9 +4,9 @@ require 'sinatra/reloader' enable :sessions -get '/' do - erb :dashboard -end +#get '/' do +# erb :dashboard +#end post '/number' do @number_of_randoms = session[:number_of_randoms] || 0 @@ -17,7 +17,7 @@ erb :number end -get '/about' do +get '/' do list = [ 'I\'m a new fan of Ice Hockey.', 'My favourite team is the Chicago Blackhawks.', 'I like Purple.', diff --git a/views/layout.erb b/views/layout.erb index ba22829..5b2fec6 100644 --- a/views/layout.erb +++ b/views/layout.erb @@ -45,8 +45,8 @@ Randomizer From 2c96365db222d424b33c37aae6588078d666219e Mon Sep 17 00:00:00 2001 From: drammopo Date: Fri, 14 Feb 2014 22:52:31 +0200 Subject: [PATCH 4/9] Removed commented code. --- theweb.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/theweb.rb b/theweb.rb index 75d69f1..4f15493 100644 --- a/theweb.rb +++ b/theweb.rb @@ -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 From 4d9c89b28463912f67c8f6c3bb127930ce77e9ed Mon Sep 17 00:00:00 2001 From: drammopo Date: Fri, 14 Feb 2014 22:53:54 +0200 Subject: [PATCH 5/9] Changed list.shuffle.first to list.shuffle.sample. --- theweb.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/theweb.rb b/theweb.rb index 4f15493..38ed978 100644 --- a/theweb.rb +++ b/theweb.rb @@ -19,6 +19,6 @@ 'I like Purple.', 'The next language I\'m learning is Python.' ] - @factoid = list.shuffle.first + @factoid = list.shuffle.sample erb :about end From a6cdd77a2bac5fe7db70e67a4499dbefd65832de Mon Sep 17 00:00:00 2001 From: drammopo Date: Fri, 14 Feb 2014 23:10:46 +0200 Subject: [PATCH 6/9] Corrected Array#sample. call. --- theweb.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/theweb.rb b/theweb.rb index 38ed978..a844f8a 100644 --- a/theweb.rb +++ b/theweb.rb @@ -19,6 +19,6 @@ 'I like Purple.', 'The next language I\'m learning is Python.' ] - @factoid = list.shuffle.sample + @factoid = list.sample erb :about end From dd7726adb34138602585c689999ef8e4ae5a6677 Mon Sep 17 00:00:00 2001 From: drammopo Date: Sat, 15 Feb 2014 21:16:39 +0200 Subject: [PATCH 7/9] Added link to adventure. --- views/layout.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/views/layout.erb b/views/layout.erb index 5b2fec6..3c48611 100644 --- a/views/layout.erb +++ b/views/layout.erb @@ -47,6 +47,7 @@ From 989f2799f10b5c0c5cb3e5bb8dfba38653833873 Mon Sep 17 00:00:00 2001 From: drammopo Date: Sat, 15 Feb 2014 21:44:10 +0200 Subject: [PATCH 8/9] Added adventure route and input form. --- theweb.rb | 5 +++++ views/adventure.erb | 7 +++++++ 2 files changed, 12 insertions(+) create mode 100644 views/adventure.erb diff --git a/theweb.rb b/theweb.rb index a844f8a..4f62971 100644 --- a/theweb.rb +++ b/theweb.rb @@ -22,3 +22,8 @@ @factoid = list.sample erb :about end + +get '/adventure' do + erb :adventure +end + diff --git a/views/adventure.erb b/views/adventure.erb new file mode 100644 index 0000000..8a5ee71 --- /dev/null +++ b/views/adventure.erb @@ -0,0 +1,7 @@ +

Choose Your Own Adventure

+

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)

+ +
+ + +
From 34995be9fb1df0e3699563bad591a58cf323a1ac Mon Sep 17 00:00:00 2001 From: drammopo Date: Sun, 16 Feb 2014 19:25:38 +0200 Subject: [PATCH 9/9] Copied the Choose Your Own Adventure code into this project.Ran all tests successfully. --- .gitkeep | 0 Gemfile | 3 +++ Gemfile.lock | 24 +++++++++++++++++++ config/database.yml.sample | 6 +++++ db/migrate/001_creates_page.rb | 10 ++++++++ db/seed.rb | 1 + db/setup.rb | 15 ++++++++++++ models/book.rb | 21 +++++++++++++++++ models/page.rb | 11 +++++++++ spec/.gitkeep | 0 spec/book_spec.rb | 33 ++++++++++++++++++++++++++ spec/page_spec.rb | 43 ++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 5 ++++ theweb.rb | 9 +++++++ views/adventure.erb | 4 ++-- views/adventure_post.erb | 2 ++ 16 files changed, 185 insertions(+), 2 deletions(-) create mode 100644 .gitkeep create mode 100644 config/database.yml.sample create mode 100644 db/migrate/001_creates_page.rb create mode 100644 db/seed.rb create mode 100644 db/setup.rb create mode 100644 models/book.rb create mode 100644 models/page.rb create mode 100644 spec/.gitkeep create mode 100644 spec/book_spec.rb create mode 100644 spec/page_spec.rb create mode 100644 spec/spec_helper.rb create mode 100644 views/adventure_post.erb diff --git a/.gitkeep b/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Gemfile b/Gemfile index 9d537b6..134d800 100644 --- a/Gemfile +++ b/Gemfile @@ -5,3 +5,6 @@ gem 'activesupport' gem 'sinatra' gem 'sinatra-contrib' gem 'shotgun' +gem 'rspec' +gem 'pg' +gem 'activerecord' diff --git a/Gemfile.lock b/Gemfile.lock index 1f59487..9afe109 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,19 +1,39 @@ 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) @@ -28,13 +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 diff --git a/config/database.yml.sample b/config/database.yml.sample new file mode 100644 index 0000000..2e5c2a9 --- /dev/null +++ b/config/database.yml.sample @@ -0,0 +1,6 @@ +host: 'localhost' +adapter: 'postgresql' +database: 'episode5' +username: XXXXXXX +encoding: 'utf8' +pool: 5 diff --git a/db/migrate/001_creates_page.rb b/db/migrate/001_creates_page.rb new file mode 100644 index 0000000..8a293c0 --- /dev/null +++ b/db/migrate/001_creates_page.rb @@ -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 diff --git a/db/seed.rb b/db/seed.rb new file mode 100644 index 0000000..1abe902 --- /dev/null +++ b/db/seed.rb @@ -0,0 +1 @@ +# Cleaning Out diff --git a/db/setup.rb b/db/setup.rb new file mode 100644 index 0000000..0e80690 --- /dev/null +++ b/db/setup.rb @@ -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/") diff --git a/models/book.rb b/models/book.rb new file mode 100644 index 0000000..5eb6f53 --- /dev/null +++ b/models/book.rb @@ -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 diff --git a/models/page.rb b/models/page.rb new file mode 100644 index 0000000..2b88343 --- /dev/null +++ b/models/page.rb @@ -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 diff --git a/spec/.gitkeep b/spec/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/spec/book_spec.rb b/spec/book_spec.rb new file mode 100644 index 0000000..b429112 --- /dev/null +++ b/spec/book_spec.rb @@ -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 diff --git a/spec/page_spec.rb b/spec/page_spec.rb new file mode 100644 index 0000000..5951cdd --- /dev/null +++ b/spec/page_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..bdb95b8 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,5 @@ +require "rspec" +require 'bundler/setup' +require_relative '../db/setup' +require_relative "../models/page" +require_relative "../models/book" diff --git a/theweb.rb b/theweb.rb index 4f62971..d2c5261 100644 --- a/theweb.rb +++ b/theweb.rb @@ -27,3 +27,12 @@ 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 diff --git a/views/adventure.erb b/views/adventure.erb index 8a5ee71..e5d1550 100644 --- a/views/adventure.erb +++ b/views/adventure.erb @@ -2,6 +2,6 @@

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)

- - + +
diff --git a/views/adventure_post.erb b/views/adventure_post.erb new file mode 100644 index 0000000..fa6ec84 --- /dev/null +++ b/views/adventure_post.erb @@ -0,0 +1,2 @@ +

ADVENTURE COMPLETE!

+

<%= @result %>