Skip to content

Commit

Permalink
added application specs
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbuddy committed Nov 19, 2009
1 parent 8c85bb8 commit 67ce4dc
Show file tree
Hide file tree
Showing 23 changed files with 276 additions and 17 deletions.
15 changes: 10 additions & 5 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ Generate your app, fill it in.

== Topology

/----------\ Worker /----------\
| | Worker | |
| Incoming |==> Queue ==> Worker ==> Queue ==> | Outgoing |
| | Worker | |
\----------/ Worker \----------/
/----------\ Worker /----------\
| | /-------\ Worker /-------\ | |
| Incoming |==> | Queue | ==> Worker ==> | Queue | ==> | Outgoing |
| | \-------/ Worker \-------/ | |
\----------/ Worker \----------/

== Sample app

Let's listen to every tweet your friend sends you, and, if its in delicious, add it, otherwise, reply with a message saying how much you enjoyed that link.

13 changes: 12 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ begin
s.add_dependency 'em-beanstalk'
s.add_dependency 'hashify'
s.add_dependency 'hwia'
s.add_dependency 'activesupport'
end
Jeweler::GemcutterTasks.new
Jeweler::RubyforgeTasks.new do |rubyforge|
Expand All @@ -29,7 +30,7 @@ end

require 'spec'
require 'spec/rake/spectask'
task :spec => 'spec:all'
task :spec => ['spec:all', 'spec:application']
namespace(:spec) do
Spec::Rake::SpecTask.new(:all) do |t|
t.spec_opts ||= []
Expand All @@ -38,6 +39,16 @@ namespace(:spec) do
t.spec_files = FileList['spec/**/*_spec.rb']
end

Spec::Rake::SpecTask.new(:application) do |t|
ENV['MESSED_HOME'] = '..'
t.ruby_opts ||= []
t.ruby_opts << '-Capplication_spec'
t.spec_opts ||= []
t.spec_opts << "-rubygems"
t.spec_opts << "--options" << "spec/spec.opts"
t.spec_files = FileList['application_spec/spec/**/*_spec.rb'].map{|f| f[/application_spec\/(.*)/, 1]}
end

end

desc "Run all examples with RCov"
Expand Down
12 changes: 12 additions & 0 deletions application_spec/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'spec'
require 'spec/rake/spectask'
task :spec => 'spec:all'
namespace(:spec) do
Spec::Rake::SpecTask.new(:all) do |t|
t.spec_opts ||= []
t.spec_opts << "-rubygems"
t.spec_opts << "--options" << "spec/spec.opts"
t.spec_files = FileList['spec/**/*_spec.rb']
end

end
3 changes: 3 additions & 0 deletions application_spec/app/runner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
with 'this is my message' do
say 'well, that was fun'
end
44 changes: 44 additions & 0 deletions application_spec/bin/runner
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env ruby

require 'rubygems'
begin
require 'messed'
rescue LoadError
if ENV['MESSED_HOME']
require File.join(ENV['MESSED_HOME'], 'lib', 'messed')
else
raise("no messed!")
end
end

require 'thor'

$root = File.expand_path(File.join(File.dirname(__FILE__), '..'))

class RunnerTask < Thor

method_options :detach => false
method_options :environment => "development"
desc "interface [NAME]", "starts an interface (#{Messed::Booter.possible_interfaces($root).join(', ')})"
def interface(name)
booter = Messed::Booter.new($root, options.environment)
interface = booter.interface_map[name]

raise("unable to find an interface with name the `#{name}'") unless interface

Messed::Interface::Runner.new(interface, :detach => options.detach?).start
end

method_options :detach => false
method_options :environment => "development"
desc "application [NAME]", "start the application"
def application
booter = Messed::Booter.new($root, options.environment)
application = booter.application
Messed::Interface::Runner.new(application, :detach => options.detach?).start
end

end

RunnerTask.start

36 changes: 36 additions & 0 deletions application_spec/bin/status
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env ruby

require 'rubygems'
begin
require 'messed'
rescue LoadError
if ENV['MESSED_HOME']
require File.join(ENV['MESSED_HOME'], 'lib', 'messed')
else
raise("no messed!")
end
end

require 'thor'

class IncomingRunnerTask < Thor

method_options :detach => false
method_options :environment => "development"
desc "start [NAME]", "start an incoming instance"
def start(name = nil)
application = Messed::Booter.new(File.expand_path(File.join(File.dirname(__FILE__), '..')), options.environment)
incoming = if name.nil? and application.incoming_map.size == 1
application.incoming_map.values.first
elsif application.incoming_map.key?(name)
application.incoming_map[name]
else
raise(name ? "unable to find incoming with name #{name}" : "You have no incoming setup.")
end

IncomingRunner.new(incoming)
end

end

IncomingRunnerTask.start
29 changes: 29 additions & 0 deletions application_spec/bin/web
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env ruby

require 'rubygems'
begin
require 'messed'
rescue LoadError
if ENV['MESSED_HOME']
require File.join(ENV['MESSED_HOME'], 'lib', 'messed')
else
raise("no messed!")
end
end

require 'thor'

class WebTask < Thor

method_options :detach => false
method_options :environment => "development"
desc "start [NAME]", "start an incoming instance"
def start(name = nil)



end

end

IncomingRunnerTask.start
Empty file.
19 changes: 19 additions & 0 deletions application_spec/config/interfaces.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
development: &default
search:
mode: incoming
adapter: twitter_search
fetch:
query:
q: '#sos09'
host: search.twitter.com
path: search.json
interval: 10

twitter_sender:
mode: outgoing
adapter: twitter_sender
username: joshbuddy
password: testing

production:
*default
12 changes: 12 additions & 0 deletions application_spec/spec/application_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'spec/spec_helper'

describe "test" do

include MessedSpecHelper

it "should have a test" do
process('this is my message')
outgoing_messages.size.should == 1
outgoing_messages.first.body.should == "well, that was fun"
end
end
7 changes: 7 additions & 0 deletions application_spec/spec/spec.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--colour
--format
specdoc
--loadby
mtime
--reverse
--backtrace
44 changes: 44 additions & 0 deletions application_spec/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'rubygems'
begin
require 'messed'
rescue LoadError
if ENV['MESSED_HOME']
require File.join(ENV['MESSED_HOME'], 'lib', 'messed')
else
raise("no messed!")
end
end

class MessedSpecHolder

attr_accessor :booter, :outgoing_messages

def initialize
@booter = Messed::Booter.new(File.join(File.dirname(__FILE__), '..'))
end

def application
@booter.application
end

def process(message)
@outgoing_messages = application.process(application.message_class.new(message))
end

end



module MessedSpecHelper

Holder = MessedSpecHolder.new

def process(message)
Holder.process(message)
end

def outgoing_messages
Holder.outgoing_messages
end

end
8 changes: 6 additions & 2 deletions lib/messed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'em-http'
require 'em-jack'
require 'hwia'
require 'activesupport'

class Messed

Expand Down Expand Up @@ -77,7 +78,7 @@ def process_incoming(continue_forever = true)
while continue_forever || incoming.jobs_available?
incoming.take { |message|
logger.debug "processing message #{message.inspect}"
process(message)
process_responses process(message)
}
end
end
Expand All @@ -88,6 +89,8 @@ def start(detach)
end

def process(message)
responses = []

self.message = message

session_store.with(message.unique_id) do |session|
Expand All @@ -98,7 +101,7 @@ def process(message)
if matcher.match?(message)
logger.debug "matched! #{matcher.inspect}"
controller = process_destination(matcher.destination)
process_responses(controller.responses)
responses.concat(controller.responses)
matcher.stop_processing?
else
false
Expand All @@ -108,6 +111,7 @@ def process(message)
controller.reset_processing! if controller.respond_to?(:reset_processing!)
reset_processing!
end
responses
end

def extract_destination(options, block)
Expand Down
2 changes: 1 addition & 1 deletion lib/messed/booter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def load_configuration
def load_interfaces
@interface_map = {}
interface_configuration.each do |name, conf|
logger.info "#{name} -> #{conf.inspect}"
logger.info "Loading interface `#{name}'"
@interface_map[name] = Interface.interface_from_configuration(self, name, conf)
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/messed/interface/adapter/twitter_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ class TwitterSender < Adapter

include Messed::Interface::EMRunner

def message_class
Messed::Message::Twitter
end

def do_work
jack = EMJack::Connection.new
jack.watch(interface.application.outgoing.tube) do
jack.use(interface.application.outgoing.tube) do
jack.each_job do |job|
process_outgoing(job, interface.application.message_class.from_json(job.body))
process_outgoing(job, message_class.from_json(job.body))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/messed/queue/beanstalk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def initialize(tube, connection = '127.0.0.1:11300')
def take(block = true)
job = beanstalk.reserve
begin
message = application.message_class.from_json(job.body)
message = application.message_class.from_json(job.body)
rescue JSON::ParserError
logger.error "malformed message #{job.body}"
job.delete
Expand Down
2 changes: 1 addition & 1 deletion lib/messed/tasks/generation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Generation < Thor

desc "generate APP_NAME", "generate the skeleton for an app called APP_NAME"
def generate(name)
Dressmaker.new(File.join(File.dirname(__FILE__), '..', '..', '..', 'patterns', 'messed'), File.join(Dir.pwd, name)).generate
Dressmaker.new(File.join(File.dirname(__FILE__), '..', '..', '..', 'patterns', 'messed'), File.join(Dir.pwd, name)).generate(:app_name => name)
end

end
Expand Down
12 changes: 8 additions & 4 deletions patterns/messed/Pattern
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
desc "make executable"
directory '/bin' do |dir|
directory.matches('/bin') do |dir|
dir.for('*') { |f|


new_exec = <<-HERE_DOC
#!/usr/bin/env ruby

Expand All @@ -19,9 +18,14 @@ end

require 'thor'

#{File.read(f)}
#{File.read(f.path)}
HERE_DOC
File.open(f, 'w') {|out| out << new_exec}
File.open(f.path, 'w') {|out| out << new_exec}
File.chmod(0755, f)
}
end

desc "set application name to #{options[:app_name]}"
files.all do |f|
f.gsub!('__APP_NAME__', options[:app_name].inspect)
end
Loading

0 comments on commit 67ce4dc

Please sign in to comment.