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
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ gem 'rake'
gem 'activesupport'
gem 'sinatra'
gem 'sinatra-contrib'
gem 'eventmachine', '~> 1.0.0.beta.4.1'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious -- why event machine?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first thing I had to do after cloning the repository and trying to run theweb.rb was handle this error:
http://stackoverflow.com/questions/6927907/ruby-problem-installing-eventmachine-under-windows-7

gem 'activerecord'
gem 'pg'
19 changes: 18 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
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)
eventmachine (0.12.10)
builder (3.0.4)
eventmachine (1.0.3)
eventmachine (1.0.3-x86-mingw32)
i18n (0.6.0)
multi_json (1.3.5)
pg (0.15.0-x86-mingw32)
rack (1.4.1)
rack-protection (1.2.0)
rack
Expand All @@ -26,12 +38,17 @@ GEM
sinatra (~> 1.3.0)
tilt (~> 1.3)
tilt (1.3.3)
tzinfo (0.3.37)

PLATFORMS
ruby
x86-mingw32

DEPENDENCIES
activerecord
activesupport
eventmachine (~> 1.0.0.beta.4.1)
pg
rake
sinatra
sinatra-contrib
1 change: 1 addition & 0 deletions config/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
keep the config directory
13 changes: 13 additions & 0 deletions db/migrate/001_creates_page.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreatesPage < ActiveRecord::Migration
def change
create_table :pages do |t|
t.text :content
t.text :preview
t.integer :parent_id
t.integer :option_a_id
t.integer :option_b_id
t.boolean :starting_point, default: false
t.boolean :conclusion, default: false
end
end
end
19 changes: 19 additions & 0 deletions db/seed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Page.delete_all

campfire = Page.create(conclusion: true, preview: "Approach the campfire",
content: "Bandits steal your bag. You lose everything!!")
sit = Page.create(conclusion: true, preview: "Sit down to eat your sandwich",
content: "You are attacked by a hungry bear who smells bacon. You lose.")
walk = Page.create(conclusion: true, preview: "Keep walking",
content: "You made it back to the inn safely. You win 100 gold pieces!!")

forest = Page.create(option_a_id: sit.id, option_b_id: campfire.id,
preview: "Go into the forest",
content: "You see a campfire in the distance.")
road = Page.create(option_a_id: sit.id, option_b_id: walk.id,
preview: "Walk down the road",
content: "You are hungry.")

start = Page.create(starting_point: true, option_a_id: forest.id, option_b_id: road.id,
preview: "Welcome, adventurer.",
content: "You wake up on a road. It's foggy and damp. Your bag conatains 30 gold pieces and a bacon sandwich.")
44 changes: 44 additions & 0 deletions db/setup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'pg'
require 'active_record'

if ENV['DATABASE_URL'].nil?
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/")
else

db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/mydb')

ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)
ActiveRecord::Base.connection.drop_database (db.path[1..-1]) rescue nil
ActiveRecord::Base.connection.create_database(db.path[1..-1]) rescue nil
# connect to it
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)
# Migrate all the things
ActiveRecord::Migrator.migrate("db/migrate/")
end
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
13 changes: 13 additions & 0 deletions models/page.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Page < ActiveRecord::Base

attr_reader :options

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

def options
options = Page.find(option_a_id, option_b_id)
end

end
60 changes: 59 additions & 1 deletion theweb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,67 @@
require 'bundler/setup'
require 'sinatra'
require 'sinatra/reloader'

#ENV.to_hash.each do |key, value|
# puts("#{key}\t#{value}")
#end
require_relative 'db/setup'
require_relative 'models/page'
require_relative 'models/book'
require "./db/seed"

enable :sessions

get '/' do
erb :dashboard
@about_text = []
@about_text << "I've been learning Ruby for about five months."
@about_text << "I live in Atlanta, Georgia."
@about_text << "I like reading, knitting, and gardening."
@about_text << "I went to Clemson University. Go Tigers!!"
session[:random_text] = @about_text
@random_text = session[:random_text].sample
erb :about
end

get '/about' do
@random_text = session[:random_text].sample
erb :about
end

get '/random' do
erb :random
end

get '/adventure' do
session[:book] = book = Book.new(Page.starting_point)
@content = session[:book].current_page.content
@option_1 = session[:book].current_page.options.first.preview
@option_2 = session[:book].current_page.options.last.preview
erb :adventure
end

post '/option_1' do
session[:book].input("A")
@content = session[:book].current_page.content
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how you have the option_1 and option_2... however, since the options are the same, I'd recommend moving the rest of the method out into another method. could be (maybe?)

def process_option(choice)
  @content = session[:book].current_page.content
  if session[:book].complete_game? 
    erb :conclusion
  else
    @option_1 = session[:book].current_page.options.first.preview
    @option_2 = session[:book].current_page.options.last.preview
    erb :adventure
  end
end

Also, you could do the following to only have one endpoint. The choice becomes a param.

post '/options/:choice' do
  session[:book].input(params[:choice])
  # existing stuff
end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I'll work on reducing the repeated lines since each page has the same basic logic.

if session[:book].complete_game?
erb :conclusion
else
@option_1 = session[:book].current_page.options.first.preview
@option_2 = session[:book].current_page.options.last.preview
erb :adventure
end
end

post '/option_2' do
session[:book].input("B")
@content = session[:book].current_page.content
if session[:book].complete_game?
erb :conclusion
else
@option_1 = session[:book].current_page.options.first.preview
@option_2 = session[:book].current_page.options.last.preview
erb :adventure
end
end

post '/number' do
Expand All @@ -16,3 +73,4 @@
@the_number = rand(number_as_string)
erb :number
end

8 changes: 8 additions & 0 deletions views/about.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h1>About me:</h1>
<h2>Hi! I'm Melissa, and <%= @random_text %></h2>
<break>
<br>
<form method="GET" action="/adventure">
<label for="adventure"><h2>Are you ready for an adventure?</h2></label>
<input type="submit" class="btn btn-large btn-primary" value="Yes!"/>
</form>
13 changes: 13 additions & 0 deletions views/adventure.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h1>Your story so far:</h1>

<h2><%= @content %></h2>
<br>
<h2>What do you want to do next?</h2>

<form method="POST" action="/option_1">
<input type="submit" class="btn btn-large btn-primary" value="<%= @option_1 %>"/>
</form>

<form method="POST" action="/option_2">
<input type="submit" class="btn btn-large btn-primary" value="<%= @option_2 %>"/>
</form>
5 changes: 5 additions & 0 deletions views/conclusion.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>Your story so far:</h1>

<h2><%= @content %></h2>
<br>
<h2>ADVENTURE COMPLETE</h2>
33 changes: 7 additions & 26 deletions views/layout.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="/">Randomizer</a>
<a class="brand" href="/">Demo App</a>
<div class="nav-collapse">
<ul class="nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="about">Home</a></li>
<li><a href="about">About</a></li>
<li><a href="adventure">Adventure</a></li>
<li><a href="random">Random Number</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</div><!--/.nav-collapse -->
Expand All @@ -60,32 +62,11 @@
<div class="hero-unit">
<%= yield %>
</div>

<!-- Example row of columns -->
<div class="row">
<div class="span4">
<h2>Heading</h2>
<p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
<p><a class="btn" href="#">View details &raquo;</a></p>
</div>
<div class="span4">
<h2>Heading</h2>
<p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
<p><a class="btn" href="#">View details &raquo;</a></p>
</div>
<div class="span4">
<h2>Heading</h2>
<p>Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.</p>
<p><a class="btn" href="#">View details &raquo;</a></p>
</div>
</div>


<hr>

<footer>
<p>Number of Randoms: <%= @number_of_randoms %></p>
OH HAI THERE
<p>&copy; Company 2012</p>
<p>&copy; Company 2013</p>
</footer>

</div> <!-- /container -->
Expand Down
4 changes: 2 additions & 2 deletions views/number.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<h1>OH HAI THERE</h1>
<h2>The number is <%= @the_number %>
<h1>Random number:</h1>
<h2>The number is <%= @the_number %></h2>
2 changes: 1 addition & 1 deletion views/dashboard.erb → views/random.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h1>OH HAI THERE</h1>
<h1>Random number:</h1>
<form method="POST" action="/number">
<label for="number">Max number for Randomitude</label>
<input type="text" id="number" name="number"/>
Expand Down