Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Sequential processor into tested middleware
- Loading branch information
Showing
3 changed files
with
59 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |