Skip to content

Commit 1814722

Browse files
committed
Notes and sample code
1 parent 435438a commit 1814722

File tree

4 files changed

+184
-0
lines changed

4 files changed

+184
-0
lines changed

session-notes/2015-03-10/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Fundamentals of Web Development
2+
3+
**March 10, 2015**
4+
5+
## Project Building
6+
7+
- Sagar
8+
- Theodore
9+
- Quinn
10+
- Mark
11+
- Ric
12+
- Guillermo
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
deaddrop.date_of_self_destruct if params[:date]
2+
deaddrop.views_til_self_destruct if params[:views]
3+
deaddrop.keep_history if params[:history]
4+
5+
deaddrop.apply_attributes(params)
6+
7+
# other file
8+
9+
class DeadDrop
10+
# property declarations
11+
12+
def apply_attributes(params)
13+
self.date_of_self_destruct = DateTime.now if params[:date] && !params[:date].empty?
14+
self.views_til_self_destruct if params[:views]
15+
self.keep_history if params[:history]
16+
end
17+
end
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
require 'data_mapper'
2+
require 'bcrypt'
3+
4+
if ENV['RACK_ENV'] != 'production'
5+
require 'dotenv'
6+
Dotenv.load('.env')
7+
end
8+
9+
DataMapper.setup(:default, ENV['DATABASE_URL'])
10+
11+
# USERS
12+
# =====================================
13+
14+
class User
15+
include DataMapper::Resource
16+
property :id, Serial
17+
property :first_name, String, :required => true
18+
property :last_name, String, :required => true
19+
property :email, String, { :required => true,
20+
:unique => true,
21+
:format => :email_address,
22+
:messages => { :presence => "Please enter a valid email address.",
23+
:is_unique => "It looks like that email is already taken.",
24+
:format => "Please enter a valid email address"
25+
}
26+
}
27+
property :phone_number, String, { :required => true,
28+
:unique => true,
29+
:messages => { :presence => "Please enter your phone number.",
30+
:is_unique => "It looks like that phone number is taken."
31+
}
32+
}
33+
34+
property :password, BCryptHash, :required => true
35+
validates_confirmation_of :password
36+
37+
attr_accessor :password_confirmation
38+
validates_length_of :password_confirmation, :min => 6, :if => :new_user
39+
40+
def new_user
41+
self.new?
42+
end
43+
44+
has n, :memberships
45+
has n, :groups, through: :memberships
46+
47+
# has n, :bets, :child_key => [:source_id]
48+
# has n, :bettors, self, :through => :bets, :via => :target
49+
50+
has n, :created_bets, 'Bets', :child_key => [:bettor_id]
51+
has n, :joined_bets, 'Bets', :child_key => [:bettee_id]
52+
53+
def bets
54+
self.created_bets + self.joined_bets
55+
end
56+
end
57+
58+
# bettor = User.new
59+
# bettee = User.new
60+
#
61+
# bettor.bets # => all bets that bettor has create
62+
# bettor.bets.first.bettee
63+
64+
# bookings
65+
# - user_id
66+
# - bet_id
67+
# - is_creator Boolean
68+
69+
# create instance of bet and assign bettee
70+
Bet.new(bettor: User.find(3), bettee: User.find(7))
71+
72+
73+
# MEMBERSHIP JOIN TABLE
74+
# =====================================
75+
76+
class Membership
77+
include DataMapper::Resource
78+
property :id, Serial
79+
belongs_to :user, :required => true
80+
belongs_to :group, :required => true
81+
82+
end
83+
84+
# GROUPS
85+
# =====================================
86+
87+
class Group
88+
include DataMapper::Resource
89+
property :id, Serial
90+
property :group_name, String, { :required => true,
91+
:unique => true,
92+
:messages => { :is_unique => "Group name is taken. Please choose another." }
93+
}
94+
95+
property :password, BCryptHash, :required => true
96+
validates_confirmation_of :password
97+
98+
attr_accessor :password_confirmation
99+
validates_length_of :password_confirmation, :min => 6, :if => :new_group
100+
101+
102+
def new_group
103+
self.new?
104+
end
105+
106+
has n, :memberships
107+
has n, :users, through: :memberships
108+
109+
end
110+
111+
# BETS
112+
# =====================================
113+
114+
class Bet
115+
include DataMapper::Resource
116+
property :id, Serial
117+
property :amount, Float
118+
property :expiration, Date
119+
property :description, Text
120+
121+
belongs_to :bettor, 'User', :key => true
122+
belongs_to :bettee, 'User', :key => true
123+
end
124+
125+
DataMapper.finalize
126+
DataMapper.auto_upgrade!
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'rubygems'
2+
require 'twilio-ruby'
3+
require 'sinatra'
4+
5+
$incoming = []
6+
7+
get '/' do
8+
@received_messages = $incoming
9+
10+
erb :index
11+
end
12+
13+
get '/sms-quickstart' do
14+
# capturing incoming text data
15+
incoming_message = params[:Body]
16+
phone_number = params[:From]
17+
18+
# persisting text data (DB would be much better, but this is GEFN)
19+
$incoming << { body: incoming_message, phone: phone_number }
20+
21+
# building a TWIML response for the Twilio API
22+
twiml = Twilio::TwiML::Response.new do |r|
23+
r.Message do |message|
24+
message.Body "Hey Monkey. Thanks for the message!"
25+
message.MediaUrl "https://demo.twilio.com/owl.png"
26+
end
27+
end
28+
twiml.text
29+
end

0 commit comments

Comments
 (0)