Skip to content

Commit

Permalink
whitespace fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
steveyen committed Jan 6, 2011
1 parent 14c14e2 commit 19ea2ff
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 58 deletions.
14 changes: 7 additions & 7 deletions app.rb
@@ -1,6 +1,6 @@


require 'rubygems' require 'rubygems'
require 'sinatra' require 'sinatra'
require 'erb' require 'erb'
require 'rubyredis' require 'rubyredis'


Expand Down Expand Up @@ -60,7 +60,7 @@ def redis


get '/:username' do |username| get '/:username' do |username|
@user = User.find_by_username(username) @user = User.find_by_username(username)

@posts = @user.posts @posts = @user.posts
@followers = @user.followers @followers = @user.followers
@followees = @user.followees @followees = @user.followees
Expand All @@ -79,15 +79,15 @@ def link_to_user(user)
<a href="/#{user.username}">#{user.username}</a> <a href="/#{user.username}">#{user.username}</a>
HTML HTML
end end

def pluralize(singular, plural, count) def pluralize(singular, plural, count)
if count == 1 if count == 1
count.to_s + " " + singular count.to_s + " " + singular
else else
count.to_s + " " + plural count.to_s + " " + plural
end end
end end

def display_post(post) def display_post(post)
post.content.gsub(/@\w+/) do |mention| post.content.gsub(/@\w+/) do |mention|
if user = User.find_by_username(mention[1..-1]) if user = User.find_by_username(mention[1..-1])
Expand All @@ -114,15 +114,15 @@ def time_ago_in_words(time)
return distance_in_minutes.round.to_s + " minutes ago" return distance_in_minutes.round.to_s + " minutes ago"
when 46..89 when 46..89
return "about an hour ago" return "about an hour ago"
when 90..1439 when 90..1439
return (distance_in_minutes/60).round.to_s + " hours ago" return (distance_in_minutes/60).round.to_s + " hours ago"
when 1440..2879 when 1440..2879
return "about a day ago" return "about a day ago"
when 2880..43199 when 2880..43199
(distance_in_minutes / 1440).round.to_s + " days ago" (distance_in_minutes / 1440).round.to_s + " days ago"
when 43200..86399 when 43200..86399
"about a month ago" "about a month ago"
when 86400..525599 when 86400..525599
(distance_in_minutes / 43200).round.to_s + " months ago" (distance_in_minutes / 43200).round.to_s + " months ago"
when 525600..1051199 when 525600..1051199
"about a year ago" "about a year ago"
Expand All @@ -131,7 +131,7 @@ def time_ago_in_words(time)
end end
end end
end end







Expand Down
60 changes: 30 additions & 30 deletions domain.rb
Expand Up @@ -12,44 +12,44 @@ class Model
def initialize(id) def initialize(id)
@id = id @id = id
end end

def ==(other) def ==(other)
@id.to_s == other.id.to_s @id.to_s == other.id.to_s
end end

attr_reader :id attr_reader :id

def self.property(name) def self.property(name)
klass = self.name.downcase klass = self.name.downcase
self.class_eval <<-RUBY self.class_eval <<-RUBY
def #{name} def #{name}
_#{name} _#{name}
end end
def _#{name} def _#{name}
redis.get("#{klass}:id:" + id.to_s + ":#{name}") redis.get("#{klass}:id:" + id.to_s + ":#{name}")
end end
def #{name}=(val) def #{name}=(val)
redis.set("#{klass}:id:" + id.to_s + ":#{name}", val) redis.set("#{klass}:id:" + id.to_s + ":#{name}", val)
end end
RUBY RUBY
end end
end end


class User < Model class User < Model
def self.find_by_username(username) def self.find_by_username(username)
if id = redis.get("user:username:#{username}") if id = redis.get("user:username:#{username}")
User.new(id) User.new(id)
end end
end end

def self.find_by_id(id) def self.find_by_id(id)
if redis.key?("user:id:#{id}:username") if redis.key?("user:id:#{id}:username")
User.new(id) User.new(id)
end end
end end

def self.create(username, password) def self.create(username, password)
user_id = redis.incr("user:uid") user_id = redis.incr("user:uid")
salt = User.new_salt salt = User.new_salt
Expand All @@ -60,98 +60,98 @@ def self.create(username, password)
redis.push_head("users", user_id) redis.push_head("users", user_id)
User.new(user_id) User.new(user_id)
end end

def self.new_users def self.new_users
redis.list_range("users", 0, 10).map do |user_id| redis.list_range("users", 0, 10).map do |user_id|
User.new(user_id) User.new(user_id)
end end
end end

def self.new_salt def self.new_salt
arr = %w(a b c d e f) arr = %w(a b c d e f)
(0..6).to_a.map{ arr[rand(6)] }.join (0..6).to_a.map{ arr[rand(6)] }.join
end end

def self.hash_pw(salt, password) def self.hash_pw(salt, password)
Digest::MD5.hexdigest(salt + password) Digest::MD5.hexdigest(salt + password)
end end

property :username property :username
property :salt property :salt
property :hashed_password property :hashed_password

def posts(page=1) def posts(page=1)
from, to = (page-1)*10, page*10 from, to = (page-1)*10, page*10
redis.list_range("user:id:#{id}:posts", from, to).map do |post_id| redis.list_range("user:id:#{id}:posts", from, to).map do |post_id|
Post.new(post_id) Post.new(post_id)
end end
end end

def timeline(page=1) def timeline(page=1)
from, to = (page-1)*10, page*10 from, to = (page-1)*10, page*10
redis.list_range("user:id:#{id}:timeline", from, to).map do |post_id| redis.list_range("user:id:#{id}:timeline", from, to).map do |post_id|
Post.new(post_id) Post.new(post_id)
end end
end end

def mentions(page=1) def mentions(page=1)
from, to = (page-1)*10, page*10 from, to = (page-1)*10, page*10
redis.list_range("user:id:#{id}:mentions", from, to).map do |post_id| redis.list_range("user:id:#{id}:mentions", from, to).map do |post_id|
Post.new(post_id) Post.new(post_id)
end end
end end

def add_post(post) def add_post(post)
redis.push_head("user:id:#{id}:posts", post.id) redis.push_head("user:id:#{id}:posts", post.id)
redis.push_head("user:id:#{id}:timeline", post.id) redis.push_head("user:id:#{id}:timeline", post.id)
end end

def add_timeline_post(post) def add_timeline_post(post)
redis.push_head("user:id:#{id}:timeline", post.id) redis.push_head("user:id:#{id}:timeline", post.id)
end end

def add_mention(post) def add_mention(post)
redis.push_head("user:id:#{id}:mentions", post.id) redis.push_head("user:id:#{id}:mentions", post.id)
end end

def follow(user) def follow(user)
return if user == self return if user == self
redis.set_add("user:id:#{id}:followees", user.id) redis.set_add("user:id:#{id}:followees", user.id)
user.add_follower(self) user.add_follower(self)
end end

def stop_following(user) def stop_following(user)
redis.set_delete("user:id:#{id}:followees", user.id) redis.set_delete("user:id:#{id}:followees", user.id)
user.remove_follower(self) user.remove_follower(self)
end end

def following?(user) def following?(user)
redis.set_member?("user:id:#{id}:followees", user.id) redis.set_member?("user:id:#{id}:followees", user.id)
end end

def followers def followers
redis.set_members("user:id:#{id}:followers").map do |user_id| redis.set_members("user:id:#{id}:followers").map do |user_id|
User.new(user_id) User.new(user_id)
end end
end end

def followees def followees
redis.set_members("user:id:#{id}:followees").map do |user_id| redis.set_members("user:id:#{id}:followees").map do |user_id|
User.new(user_id) User.new(user_id)
end end
end end

protected protected

def add_follower(user) def add_follower(user)
redis.set_add("user:id:#{id}:followers", user.id) redis.set_add("user:id:#{id}:followers", user.id)
end end

def remove_follower(user) def remove_follower(user)
redis.set_delete("user:id:#{id}:followers", user.id) redis.set_delete("user:id:#{id}:followers", user.id)
end end
end end

class Post < Model class Post < Model
def self.create(user, content) def self.create(user, content)
post_id = redis.incr("post:uid") post_id = redis.incr("post:uid")
Expand All @@ -170,15 +170,15 @@ def self.create(user, content)
end end
end end
end end

property :content property :content
property :user_id property :user_id
property :created_at property :created_at

def created_at def created_at
Time.parse(_created_at) Time.parse(_created_at)
end end

def user def user
User.new(user_id) User.new(user_id)
end end
Expand Down
4 changes: 2 additions & 2 deletions login-signup.rb
@@ -1,7 +1,7 @@


before do before do
unless %w(/login /signup).include?(request.path_info) or unless %w(/login /signup).include?(request.path_info) or
request.path_info =~ /\.css$/ or request.path_info =~ /\.css$/ or
@logged_in_user = User.find_by_id(session["user_id"]) @logged_in_user = User.find_by_id(session["user_id"])
redirect '/login', 303 redirect '/login', 303
end end
Expand Down
2 changes: 1 addition & 1 deletion rubyredis.rb
Expand Up @@ -41,7 +41,7 @@ class RedisClient
"renamenx"=> ConvertToBool, "renamenx"=> ConvertToBool,
"expire"=> ConvertToBool, "expire"=> ConvertToBool,
"keys" => lambda{|r| r.split(" ")}, "keys" => lambda{|r| r.split(" ")},
"info" => lambda{|r| "info" => lambda{|r|
info = {} info = {}
r.each_line {|kv| r.each_line {|kv|
k,v = kv.split(":",2).map{|x| x.chomp} k,v = kv.split(":",2).map{|x| x.chomp}
Expand Down
2 changes: 1 addition & 1 deletion views/footer.erb
Expand Up @@ -2,7 +2,7 @@
</div> </div>
<div class="span-24 last"> <div class="span-24 last">
<hr /> <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 <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>. The code is on <a href="http://github.com/danlucraft/retwis-rb">Github</a>.
</div> </div>
Expand Down
12 changes: 6 additions & 6 deletions views/header.erb
Expand Up @@ -2,11 +2,11 @@
<head> <head>
<title>Retwis-RB</title> <title>Retwis-RB</title>
<link rel="stylesheet" href="/css/screen.css" type="text/css" media="screen, projection"> <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]> <!--[if IE]>
<link rel="stylesheet" href="/css/ie.css" type="text/css" media="screen, projection"> <link rel="stylesheet" href="/css/ie.css" type="text/css" media="screen, projection">
<![endif]--> <![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> </head>
<body> <body>
<br /> <br />
Expand All @@ -18,10 +18,10 @@
<div class="span-12 last right-align"> <div class="span-12 last right-align">
<% if @logged_in_user %> <% if @logged_in_user %>
<br /><br /> <br /><br />
<a href="/">home</a> | <a href="/">home</a> |
<a href="/<%= @logged_in_user.username %>/mentions">mentions</a> | <a href="/<%= @logged_in_user.username %>/mentions">mentions</a> |
<%= link_to_user(@logged_in_user) %> | <%= link_to_user(@logged_in_user) %> |
<a href="/timeline">timeline</a> | <a href="/timeline">timeline</a> |
<a href="/logout">logout</a> <a href="/logout">logout</a>
<% end %> <% end %>
</div> </div>
Expand Down
2 changes: 1 addition & 1 deletion views/index.erb
Expand Up @@ -15,7 +15,7 @@
</form> </form>
</div> </div>
<%= erb :_posts %> <%= erb :_posts %>

</div> </div>


<div class="span-7 last" id="rightcol"> <div class="span-7 last" id="rightcol">
Expand Down
10 changes: 5 additions & 5 deletions views/login.erb
Expand Up @@ -2,13 +2,13 @@


<div class="span-11 box"> <div class="span-11 box">
<h2>Login</h2> <h2>Login</h2>

<% if @login_error %> <% if @login_error %>
<div class="error"> <div class="error">
<%= @login_error %> <%= @login_error %>
</div> </div>
<% end %> <% end %>

<form action="login" method="post"> <form action="login" method="post">
<table> <table>
<tr> <tr>
Expand All @@ -22,17 +22,17 @@
</table> </table>
<input type="submit" value="Log in" /> <input type="submit" value="Log in" />
</form> </form>
</div> </div>


<div class="span-11 last box"> <div class="span-11 last box">
<h2>Sign up</h2> <h2>Sign up</h2>

<% if @signup_error %> <% if @signup_error %>
<div class="error"> <div class="error">
<%= @signup_error %> <%= @signup_error %>
</div> </div>
<% end %> <% end %>

<form action="signup" method="post"> <form action="signup" method="post">
<table> <table>
<tr> <tr>
Expand Down
4 changes: 2 additions & 2 deletions views/mentions.erb
Expand Up @@ -4,15 +4,15 @@
<div class="span-16" id="maincol"> <div class="span-16" id="maincol">
<h2>Mentions of: <%= @user.username %></h2> <h2>Mentions of: <%= @user.username %></h2>
<% if @logged_in_user != @user %> <% if @logged_in_user != @user %>
<div class="box"> <div class="box">
<% if @logged_in_user.following?(@user) %> <% if @logged_in_user.following?(@user) %>
<a href="/<%= @logged_in_user.username %>/stopfollow/<%= @user.username %>">Stop following</a> <a href="/<%= @logged_in_user.username %>/stopfollow/<%= @user.username %>">Stop following</a>
<% else %> <% else %>
<a href="/<%= @logged_in_user.username %>/follow/<%= @user.username %>">Follow</a> <a href="/<%= @logged_in_user.username %>/follow/<%= @user.username %>">Follow</a>
<% end %> <% end %>
</div> </div>
<% end %> <% end %>
<%= erb :_posts %> <%= erb :_posts %>
</div> </div>


Expand Down
4 changes: 2 additions & 2 deletions views/profile.erb
Expand Up @@ -3,7 +3,7 @@
<div class="span-16" id="maincol"> <div class="span-16" id="maincol">
<h2><%= @user.username %></h2> <h2><%= @user.username %></h2>
<% if @logged_in_user != @user %> <% if @logged_in_user != @user %>
<div class="box"> <div class="box">
<% if @logged_in_user.following?(@user) %> <% if @logged_in_user.following?(@user) %>
<a href="/<%= @logged_in_user.username %>/stopfollow/<%= @user.username %>">Stop following</a> <a href="/<%= @logged_in_user.username %>/stopfollow/<%= @user.username %>">Stop following</a>
<% else %> <% else %>
Expand All @@ -12,7 +12,7 @@
| <a href="/<%= @user.username %>/mentions">See mentions</a> | <a href="/<%= @user.username %>/mentions">See mentions</a>
</div> </div>
<% end %> <% end %>
<%= erb :_posts %> <%= erb :_posts %>
</div> </div>
<div class="span-7 last" id="rightcol"> <div class="span-7 last" id="rightcol">
Expand Down
2 changes: 1 addition & 1 deletion views/timeline.erb
Expand Up @@ -3,7 +3,7 @@


<div class="span-16" id="maincol"> <div class="span-16" id="maincol">
<h3>Timeline</h3> <h3>Timeline</h3>

Posts from all users. Posts from all users.
<br /> <br />
<%= erb :_posts %> <%= erb :_posts %>
Expand Down

0 comments on commit 19ea2ff

Please sign in to comment.