Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

whitespace fixup

  • Loading branch information...
commit 19ea2ffac5bb65f4a1f8b1d1944656be0b5f5395 1 parent 14c14e2
@steveyen steveyen authored
View
14 app.rb
@@ -1,6 +1,6 @@
require 'rubygems'
-require 'sinatra'
+require 'sinatra'
require 'erb'
require 'rubyredis'
@@ -60,7 +60,7 @@ def redis
get '/:username' do |username|
@user = User.find_by_username(username)
-
+
@posts = @user.posts
@followers = @user.followers
@followees = @user.followees
@@ -79,7 +79,7 @@ def link_to_user(user)
<a href="/#{user.username}">#{user.username}</a>
HTML
end
-
+
def pluralize(singular, plural, count)
if count == 1
count.to_s + " " + singular
@@ -87,7 +87,7 @@ def pluralize(singular, plural, count)
count.to_s + " " + plural
end
end
-
+
def display_post(post)
post.content.gsub(/@\w+/) do |mention|
if user = User.find_by_username(mention[1..-1])
@@ -114,7 +114,7 @@ def time_ago_in_words(time)
return distance_in_minutes.round.to_s + " minutes ago"
when 46..89
return "about an hour ago"
- when 90..1439
+ when 90..1439
return (distance_in_minutes/60).round.to_s + " hours ago"
when 1440..2879
return "about a day ago"
@@ -122,7 +122,7 @@ def time_ago_in_words(time)
(distance_in_minutes / 1440).round.to_s + " days ago"
when 43200..86399
"about a month ago"
- when 86400..525599
+ when 86400..525599
(distance_in_minutes / 43200).round.to_s + " months ago"
when 525600..1051199
"about a year ago"
@@ -131,7 +131,7 @@ def time_ago_in_words(time)
end
end
end
-
+
View
60 domain.rb
@@ -12,30 +12,30 @@ class Model
def initialize(id)
@id = id
end
-
+
def ==(other)
@id.to_s == other.id.to_s
end
-
+
attr_reader :id
-
+
def self.property(name)
klass = self.name.downcase
self.class_eval <<-RUBY
def #{name}
_#{name}
end
-
+
def _#{name}
redis.get("#{klass}:id:" + id.to_s + ":#{name}")
end
-
+
def #{name}=(val)
redis.set("#{klass}:id:" + id.to_s + ":#{name}", val)
end
RUBY
end
-end
+end
class User < Model
def self.find_by_username(username)
@@ -43,13 +43,13 @@ def self.find_by_username(username)
User.new(id)
end
end
-
+
def self.find_by_id(id)
if redis.key?("user:id:#{id}:username")
User.new(id)
end
end
-
+
def self.create(username, password)
user_id = redis.incr("user:uid")
salt = User.new_salt
@@ -60,98 +60,98 @@ def self.create(username, password)
redis.push_head("users", user_id)
User.new(user_id)
end
-
+
def self.new_users
redis.list_range("users", 0, 10).map do |user_id|
User.new(user_id)
end
end
-
+
def self.new_salt
arr = %w(a b c d e f)
(0..6).to_a.map{ arr[rand(6)] }.join
end
-
+
def self.hash_pw(salt, password)
Digest::MD5.hexdigest(salt + password)
end
-
+
property :username
property :salt
property :hashed_password
-
+
def posts(page=1)
from, to = (page-1)*10, page*10
redis.list_range("user:id:#{id}:posts", from, to).map do |post_id|
Post.new(post_id)
end
end
-
+
def timeline(page=1)
from, to = (page-1)*10, page*10
redis.list_range("user:id:#{id}:timeline", from, to).map do |post_id|
Post.new(post_id)
end
end
-
+
def mentions(page=1)
from, to = (page-1)*10, page*10
redis.list_range("user:id:#{id}:mentions", from, to).map do |post_id|
Post.new(post_id)
end
end
-
+
def add_post(post)
redis.push_head("user:id:#{id}:posts", post.id)
redis.push_head("user:id:#{id}:timeline", post.id)
end
-
+
def add_timeline_post(post)
redis.push_head("user:id:#{id}:timeline", post.id)
end
-
+
def add_mention(post)
redis.push_head("user:id:#{id}:mentions", post.id)
end
-
+
def follow(user)
return if user == self
redis.set_add("user:id:#{id}:followees", user.id)
user.add_follower(self)
end
-
+
def stop_following(user)
redis.set_delete("user:id:#{id}:followees", user.id)
user.remove_follower(self)
end
-
+
def following?(user)
redis.set_member?("user:id:#{id}:followees", user.id)
end
-
+
def followers
redis.set_members("user:id:#{id}:followers").map do |user_id|
User.new(user_id)
end
end
-
+
def followees
redis.set_members("user:id:#{id}:followees").map do |user_id|
User.new(user_id)
end
end
-
+
protected
-
+
def add_follower(user)
redis.set_add("user:id:#{id}:followers", user.id)
end
-
+
def remove_follower(user)
redis.set_delete("user:id:#{id}:followers", user.id)
end
end
-
+
class Post < Model
def self.create(user, content)
post_id = redis.incr("post:uid")
@@ -170,15 +170,15 @@ def self.create(user, content)
end
end
end
-
+
property :content
property :user_id
property :created_at
-
+
def created_at
Time.parse(_created_at)
end
-
+
def user
User.new(user_id)
end
View
4 login-signup.rb
@@ -1,7 +1,7 @@
before do
- unless %w(/login /signup).include?(request.path_info) or
- request.path_info =~ /\.css$/ or
+ unless %w(/login /signup).include?(request.path_info) or
+ request.path_info =~ /\.css$/ or
@logged_in_user = User.find_by_id(session["user_id"])
redirect '/login', 303
end
View
2  rubyredis.rb
@@ -41,7 +41,7 @@ class RedisClient
"renamenx"=> ConvertToBool,
"expire"=> ConvertToBool,
"keys" => lambda{|r| r.split(" ")},
- "info" => lambda{|r|
+ "info" => lambda{|r|
info = {}
r.each_line {|kv|
k,v = kv.split(":",2).map{|x| x.chomp}
View
2  views/footer.erb
@@ -2,7 +2,7 @@
</div>
<div class="span-24 last">
<hr />
- Retwis-RB is a simple Twitter clone written in Ruby and Sinatra to show off
+ Retwis-RB is a simple Twitter clone written in Ruby and Sinatra to show off
the <a href="http://code.google.com/p/redis/">Redis key-value database</a>.
The code is on <a href="http://github.com/danlucraft/retwis-rb">Github</a>.
</div>
View
12 views/header.erb
@@ -2,11 +2,11 @@
<head>
<title>Retwis-RB</title>
<link rel="stylesheet" href="/css/screen.css" type="text/css" media="screen, projection">
- <link rel="stylesheet" href="/css/print.css" type="text/css" media="print">
+ <link rel="stylesheet" href="/css/print.css" type="text/css" media="print">
<!--[if IE]>
<link rel="stylesheet" href="/css/ie.css" type="text/css" media="screen, projection">
<![endif]-->
- <link rel="stylesheet" href="/css/custom.css" type="text/css" media="screen, projection">
+ <link rel="stylesheet" href="/css/custom.css" type="text/css" media="screen, projection">
</head>
<body>
<br />
@@ -18,10 +18,10 @@
<div class="span-12 last right-align">
<% if @logged_in_user %>
<br /><br />
- <a href="/">home</a> |
- <a href="/<%= @logged_in_user.username %>/mentions">mentions</a> |
- <%= link_to_user(@logged_in_user) %> |
- <a href="/timeline">timeline</a> |
+ <a href="/">home</a> |
+ <a href="/<%= @logged_in_user.username %>/mentions">mentions</a> |
+ <%= link_to_user(@logged_in_user) %> |
+ <a href="/timeline">timeline</a> |
<a href="/logout">logout</a>
<% end %>
</div>
View
2  views/index.erb
@@ -15,7 +15,7 @@
</form>
</div>
<%= erb :_posts %>
-
+
</div>
<div class="span-7 last" id="rightcol">
View
10 views/login.erb
@@ -2,13 +2,13 @@
<div class="span-11 box">
<h2>Login</h2>
-
+
<% if @login_error %>
<div class="error">
<%= @login_error %>
</div>
<% end %>
-
+
<form action="login" method="post">
<table>
<tr>
@@ -22,17 +22,17 @@
</table>
<input type="submit" value="Log in" />
</form>
-</div>
+</div>
<div class="span-11 last box">
<h2>Sign up</h2>
-
+
<% if @signup_error %>
<div class="error">
<%= @signup_error %>
</div>
<% end %>
-
+
<form action="signup" method="post">
<table>
<tr>
View
4 views/mentions.erb
@@ -4,7 +4,7 @@
<div class="span-16" id="maincol">
<h2>Mentions of: <%= @user.username %></h2>
<% if @logged_in_user != @user %>
- <div class="box">
+ <div class="box">
<% if @logged_in_user.following?(@user) %>
<a href="/<%= @logged_in_user.username %>/stopfollow/<%= @user.username %>">Stop following</a>
<% else %>
@@ -12,7 +12,7 @@
<% end %>
</div>
<% end %>
-
+
<%= erb :_posts %>
</div>
View
4 views/profile.erb
@@ -3,7 +3,7 @@
<div class="span-16" id="maincol">
<h2><%= @user.username %></h2>
<% if @logged_in_user != @user %>
- <div class="box">
+ <div class="box">
<% if @logged_in_user.following?(@user) %>
<a href="/<%= @logged_in_user.username %>/stopfollow/<%= @user.username %>">Stop following</a>
<% else %>
@@ -12,7 +12,7 @@
| <a href="/<%= @user.username %>/mentions">See mentions</a>
</div>
<% end %>
-
+
<%= erb :_posts %>
</div>
<div class="span-7 last" id="rightcol">
View
2  views/timeline.erb
@@ -3,7 +3,7 @@
<div class="span-16" id="maincol">
<h3>Timeline</h3>
-
+
Posts from all users.
<br />
<%= erb :_posts %>
Please sign in to comment.
Something went wrong with that request. Please try again.