Skip to content
Browse files

hurls are sets, sorted recovery

  • Loading branch information...
1 parent 2c599c9 commit d74d87a6232eeb3b306b33375ac7eb0ac9b757ae @defunkt committed Aug 22, 2009
Showing with 29 additions and 27 deletions.
  1. +21 −9 models/user.rb
  2. +8 −18 views/hurls.erb
View
30 models/user.rb
@@ -12,19 +12,31 @@ class User < Model
#
def add_hurl(hurl)
- # don't save the same hurl twice in a row
- if hurl != latest_hurl[-1]
- json = Yajl::Encoder.encode([ Time.now.to_i, hurl])
- redis.lpush(key(id, :hurls), json)
- end
+ redis.sadd(key(id, :hurls), hurl)
+ redis.set(key(id, :hurls, hurl), Time.now.to_i)
end
- def latest_hurl
- redis.lindex(key(id, :hurls), 0) || []
+ def unsorted_hurls
+ redis.members key(id, :hurls)
end
- def list_hurls(start = 0, stop = 100)
- redis.lrange(key(id, :hurls), start, stop)
+ def hurls(limit = 100)
+ hurls = redis.sort key(id, :hurls),
+ :by => "#{key(id, :hurls)}:*",
+ :order => 'DESC',
+ :get => "*",
+ :limit => [0, 100]
+
+ # convert hurls to ruby objects
+ hurls.map! { |hurl| Yajl::Parser.parse(hurl) }
+
+ # find and set the corresponding timestamps for
+ # each hurl (scoped to this user)
+ keys = hurls.map { |h| key(id, :hurls, h['id']) }
+ redis.mget(keys).each_with_index do |date, i|
+ hurls[i]['date'] = Time.at(date.to_i)
+ end
+ hurls
end
View
26 views/hurls.erb
@@ -1,21 +1,11 @@
<h2>Your hurls</h2>
<table id="archive">
- <tr>
- <td><a href="#">http://baconfile.com/leah.json</a></td>
- <td>GET</td>
- <td>no auth</td>
- <td class="date">3 days ago</td>
- </tr>
- <tr>
- <td><a href="#">http://twitter.com/status.json</a></td>
- <td>POST</td>
- <td>HTTP basic</td>
- <td class="date">3 days ago</td>
- </tr>
- <tr>
- <td><a href="#">http://baconfile.com/leah.json</a></td>
- <td>GET</td>
- <td>no auth</td>
- <td class="date">3 days ago</td>
- </tr>
+ <% for hurl in @hurls %>
+ <tr>
+ <td><a href="/hurls/<%= hurl['id'] %>"><%= hurl['url'] %></a></td>
+ <td><%= hurl['method'] %></td>
+ <td><%= hurl['auth'] == "none" ? "no auth" : "HTTP basic" %></td>
+ <td class="date relatize"><%= hurl['date'] %></td>
+ </tr>
+ <% end %>
</table>

5 comments on commit d74d87a

@sr
sr commented on d74d87a Jan 21, 2010

    
@sr
sr commented on d74d87a Jan 21, 2010

</3

@anildigital

@stammy
stammy commented on d74d87a Oct 29, 2010

<3

Please sign in to comment.
Something went wrong with that request. Please try again.