Permalink
Browse files

Merge pull request #1 from davelnewton/master

Pulled out a render_topics method.
  • Loading branch information...
2 parents dd46c15 + a82e3a0 commit b95a475706159df52b3b8a58435bfda41b8e5eee @nkijak committed Sep 6, 2011
Showing with 49 additions and 47 deletions.
  1. +3 −2 app/actions/home_action.rb
  2. +46 −45 app/actions/list_topics_action.rb
@@ -1,8 +1,9 @@
class HomeAction < Cramp::Action
+
def start
render "Hello World!"
render request.params
-
- finish
+ finish
end
+
end
@@ -1,54 +1,55 @@
require 'yajl'
class ListTopicsAction < Cramp::Action
- A_MAX = 1000000
-
- on_finish :cleanup
+ A_MAX = 1000000
- def respond_with
- [200, {'Content-Type' => 'application/json'}]
- end
+ on_finish :cleanup
- def render_results
- puts "RenderResults:#{@topics.length}<#{@expected}"
- return if (@topics.length < @expected && @expected != A_MAX)
- puts "Rendering..."
- render Yajl::Encoder.encode(@topics)
- finish
- end
+ def respond_with
+ [200, {'Content-Type' => 'application/json'}]
+ end
- def start
- @redis = EM::Hiredis.connect
- @topics = []
- @expected = A_MAX
- puts "Looking for keys.."
- @redis.keys("topic:??") { |keys|
- @expected = keys.length
- puts "Found #{@expected} keys"
- keys.each do |key|
- topic = {"key" => key}
- puts "Getting #{key}"
- @redis.get(key) { |topic_value|
- puts "Getting comments for #{key}"
- topic["topic"] = topic_value
- @redis.lrange("#{key}:comments", 0, -1) { |comments|
- topic["comments"] = comments || []
- @topics << topic
- puts "Added topic to results"
- render_results
- puts "finished #{key}"
- }
- }
- end
- puts "next key.."
- }
- puts "all keys being fetched.."
- end
+ def render_results
+ puts "RenderResults:#{@topics.length}<#{@expected}"
+ return if (@topics.length < @expected && @expected != A_MAX)
+ puts "Rendering..."
+ render Yajl::Encoder.encode(@topics)
+ finish
+ end
+ def start
+ @redis = EM::Hiredis.connect
+ @topics = []
+ @expected = A_MAX
+ puts "Looking for keys.."
+ @redis.keys("topic:??") do |keys|
+ @expected = keys.length
+ puts "Found #{@expected} keys"
+ keys.each { |key| render_topics key }
+ puts "next key.."
+ end
+ puts "All keys fetched and processed."
+ end
- def cleanup
- puts "closing connection"
- @redis.close_connection
- puts "done"
- end
+ def render_topics(key)
+ puts "Getting #{key}..."
+ topic = { "key" => key }
+ @redis.get(key) do |topic_value|
+ puts "Getting comments for #{key}"
+ topic["topic"] = topic_value
+ @redis.lrange("#{key}:comments", 0, -1) do |comments|
+ topic["comments"] = comments || []
+ @topics << topic
+ puts "Added topic to results"
+ render_results
+ puts "finished #{key}"
+ end
+ end
+ end
+
+ def cleanup
+ puts "closing connection"
+ @redis.close_connection
+ puts "done"
+ end
end

0 comments on commit b95a475

Please sign in to comment.