Skip to content

Commit

Permalink
Refactor Sequential processor into tested middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
arsduo committed Sep 20, 2012
1 parent 6ad685e commit f3c4335
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 20 deletions.
28 changes: 28 additions & 0 deletions lib/batch_api/processor/sequential.rb
@@ -0,0 +1,28 @@
module BatchApi
class Processor
class Sequential

# Public: initialize with the app.
def initialize(app)
@app = app
end

# Public: execute all operations sequentially.
#
# ops - a set of BatchApi::Operations
# options - a set of options
#
# Returns an array of BatchApi::Response objects.
def call(env)
env[:ops].collect do |op|
# set the current op
env[:op] = op
# execute the individual request
# then clear out the current op afterward
@app.call(env).tap {|r| env.delete(:op) }
end
end
end
end
end

18 changes: 0 additions & 18 deletions lib/batch_api/processor/strategies/sequential.rb

This file was deleted.

33 changes: 31 additions & 2 deletions spec/lib/sequential_spec.rb
@@ -1,5 +1,34 @@
require 'spec_helper'

describe BatchApi::Processor::Strategies::Sequential do
pending "needs a test"
describe BatchApi::Processor::Sequential do

let(:app) { stub("app", call: stub) }
let(:sequential) { BatchApi::Processor::Sequential.new(app) }

describe "#call" do
let(:call_results) { 3.times.collect {|i| stub("called #{i}") } }
let(:env) { {
ops: 3.times.collect {|i| stub("op #{i}") }
} }

before :each do
app.stub(:call).and_return(*call_results)
end

it "calls the app onward, setting the env appropriately" do
env[:ops].each {|op|
app.should_receive(:call).with(hash_including(op: op)).ordered
}
sequential.call(env)
end

it "includes the rest of the env in the calls" do
app.should_receive(:call).with(hash_including(env)).exactly(3).times
sequential.call(env)
end

it "returns the results of the calls" do
sequential.call(env).should == call_results
end
end
end

0 comments on commit f3c4335

Please sign in to comment.