Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit fb45a669efa5717b789c425c5dd499b4b08e99f6 @ryanb ryanb committed Jul 19, 2012
Showing with 85 additions and 0 deletions.
  1. +33 −0 README.md
  2. +21 −0 feed_counter.rb
  3. +31 −0 rocket.rb
33 README.md
@@ -0,0 +1,33 @@
+# RailsCasts Episode #367: Celluloid
+
+http://railscasts.com/episodes/367-celluloid
+
+Requires Ruby 1.9.2 or higher.
+
+
+### Commands used in this episode
+
+```
+gem install celluloid
+irb -r ./rocket
+ruby feed_counter.rb http://rss.cnn.com/rss/cnn_topstories.rss http://feeds.feedburner.com/railscasts http://stackoverflow.com/feeds
+irb -r ./feed_counter
+```
+
+### Commands used in irb
+
+```ruby
+r = Rocket.new
+r.launch
+r.launch!
+r2 = Rocket.new
+Thread.list.size
+r.launch!; r2.launch!
+l = Launcher.new
+l.launch_rocket
+Rocket.supervise(true)
+f = FeedCounter.new("http://feeds.feedburner.com/railscasts")
+future = f.future(:count)
+future.value
+```
+
21 feed_counter.rb
@@ -0,0 +1,21 @@
+require 'rss'
+require 'open-uri'
+require 'celluloid'
+
+class FeedCounter
+ include Celluloid
+
+ def count(url)
+ open(url) do |f|
+ rss = RSS::Parser.parse(f.read, false)
+ count = rss.items.size
+ puts "#{count} in #{url}"
+ count
+ end
+ end
+end
+
+pool = FeedCounter.pool(size: 6)
+futures = $*.map { |url| pool.future(:count, url) }
+total = futures.map(&:value).inject(:+)
+puts "#{total} total" if total
31 rocket.rb
@@ -0,0 +1,31 @@
+require 'celluloid'
+
+class Rocket
+ include Celluloid
+
+ def initialize(autolaunch = false)
+ launch! if autolaunch
+ end
+
+ def launch
+ 3.downto(1) do |n|
+ puts "#{n}..."
+ sleep 1
+ raise "Houston, we have a problem" if [true, false].sample
+ end
+ puts "Blast off!"
+ end
+end
+
+class Launcher
+ include Celluloid
+ trap_exit :relaunch
+
+ def launch_rocket
+ Rocket.new_link.launch!
+ end
+
+ def relaunch(actor, reason)
+ launch_rocket
+ end
+end

0 comments on commit fb45a66

Please sign in to comment.