public
Last active

  • Download Gist
example_note_keeping_app.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
require 'rubygems'
require 'sinatra'
require 'redis'
 
# To use, simply start your Redis server and boot this
# example app with:
# ruby example_note_keeping_app.rb
#
# Point your browser to http://localhost:4567 and enjoy!
#
 
# Create the Redis client when the script starts
$redis = Redis.new
 
helpers do
def tag_link(t, title = nil)
arr = t.is_a?(Array) ? t : (@current_tags || []).dup.push(t)
path = arr.uniq.join('/')
%!<a href="/tags/#{path}">#{title || t}</a>!
end
end
 
# Loads all notes given by an array of IDs in a single Redis call
def load_notes(ids)
if ids.empty?
[]
else
$redis.mget(*ids.map { |id| "note-#{id}" }).map { |raw| Marshal.load(raw) }
end
end
 
get '/' do
@notes = load_notes $redis.smembers("all-notes")
erb :index
end
 
get '/tags/*' do
@current_tags = params[:splat].first.split('/')
redirect '/' if @current_tags.empty?
 
# Names of the tag-sets
sets = @current_tags.map { |t| "tag-#{t}" }
 
# Load all notes that reside in the intersection of these sets
@notes = load_notes $redis.sinter(*sets)
 
erb :index
end
 
get '/new' do
erb :new
end
 
post '/new' do
# Extract tags from request parameters
tags = params[:tags].gsub(/\s+/, '').split(',')
 
# Get ID for the new note
note_id = $redis.incr(:note_counter)
 
# Store the note
$redis.set("note-#{note_id}", Marshal.dump({ :contents => params[:contents], :tags => tags }))
$redis.sadd("all-notes", note_id)
 
# Add the ID to the tag-sets
tags.each { |t| $redis.sadd("tag-#{t}", note_id) }
 
redirect '/'
end
 
__END__
@@ layout
<html>
<head>
<title>My notes</title>
<style type="text/css" media="screen">
#main {
font-family: Helvetica;
width: 400px;
margin: 60px auto;
}
#head div {
float: right;
text-align: right;
font-size: 0.8em;
}
a {
text-decoration: none;
color: #000;
}
label {
font-size: 0.8em;
display: block;
width: 70px;
}
legend {
font-weight: bold;
}
input, textarea {
width: 100%;
margin-bottom: 1em;
}
input.submit {
width: auto;
float: right;
font-weight: bold;
}
div.note {
border-top: 2px dashed #bbb;
padding: 0.3em 1em;
background-color: #FFFFA0;
margin-bottom: 1em;
}
div.note .tags {
font-size: 0.6em;
font-style: italic;
color: #333;
}
div.note .tags > a {
color: #333;
}
h3.tags a span {
color: #f00;
font-size: 0.8em;
}
</style>
</head>
<div id="main">
<div id="head">
<div>
<a href="/new">new note</a>
</div>
<h1>My notes</h1>
</div>
<div>
<%= yield %>
</div>
</div>
</html>
 
@@ new
<fieldset>
<legend>New note</legend>
<form action="/new" method="post">
<label>Contents:</label>
<textarea name="contents"></textarea>
<label>Tags:</label>
<input type="text" name="tags" />
<input type="submit" value="Save" class="submit" />
</form>
</fieldset>
 
@@ index
<% unless @current_tags.nil? %>
<h3 class="tags">
Notes tagged with:
<%= @current_tags.map do |t|
t + tag_link(@current_tags - [t], "<span>(x)</span>")
end.join(', ') %>
</h3>
<% end %>
 
<% @notes.each do |note| %>
<div class="note">
<div class="tags">
Tags: <%= note[:tags].map { |tag| tag_link(tag) }.join(', ') %>
</div>
<pre><%= note[:contents] %></pre>
</div>
<% end %>

I get an error when I have no notes: "wrong number of arguments to mget". Adding the following to load_notes (line 24) fixes it:

return [] if ids.empty?

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.