diff --git a/.gitkeep b/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Gemfile b/Gemfile index 416fe44..134d800 100644 --- a/Gemfile +++ b/Gemfile @@ -4,3 +4,7 @@ gem 'rake' gem 'activesupport' gem 'sinatra' gem 'sinatra-contrib' +gem 'shotgun' +gem 'rspec' +gem 'pg' +gem 'activerecord' diff --git a/Gemfile.lock b/Gemfile.lock index 3d79568..9afe109 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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 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 a531ce4..d2c5261 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 @@ -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 diff --git a/views/about.erb b/views/about.erb new file mode 100644 index 0000000..8b23916 --- /dev/null +++ b/views/about.erb @@ -0,0 +1,2 @@ +