-
Notifications
You must be signed in to change notification settings - Fork 47
Refactor processing flow #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7842112
Add Processor class to handle request execution
arsduo 4edc2dd
Integrate processor
arsduo d47bb6f
Add sequential processing strategy
arsduo 1d58ed7
Get request specs passing with the new processor
arsduo 2d1803b
Merge branch 'refs/heads/config' into processor
arsduo 643a5f6
Changelog, version bump
arsduo 4491c15
Handle Basic Auth properly
arsduo f41211e
Validate inputs
arsduo f2984d9
Allow routing to custom controllers (for subclassing)
arsduo 4320233
Remove unnecessary authorization stuff
arsduo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,7 +1,7 @@ | ||
| PATH | ||
| remote: . | ||
| specs: | ||
| batch_api (0.0.1) | ||
| batch_api (0.0.3) | ||
| rails (~> 3.2) | ||
|
|
||
| GEM | ||
|
|
||
This file contains hidden or 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
This file contains hidden or 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,12 @@ | ||
| v0.0.3 | ||
| * Encapsulate processing into a Processor module | ||
| * Prepare for parallel processing in the future | ||
| * Add specific errors | ||
| * Allow controlling the routing target | ||
|
|
||
| v0.0.2 | ||
| * Add config module | ||
| * Add options for operation limit, endpoint, and verb | ||
|
|
||
| v0.0.1 | ||
| * Initial build |
This file contains hidden or 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
This file contains hidden or 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,81 @@ | ||
| require 'batch_api/processor/strategies/sequential' | ||
|
|
||
| module BatchApi | ||
| class Processor | ||
| # Raised when a user provides more Batch API requests than a service | ||
| # allows. | ||
| class OperationLimitExceeded < StandardError; end | ||
| # Raised if a provided option is invalid. | ||
| class BadOptionError < StandardError; end | ||
|
|
||
| attr_reader :ops, :options | ||
|
|
||
| # Public: create a new Processor. | ||
| # | ||
| # ops - an array of operations hashes | ||
| # options - any other options | ||
| # | ||
| # Raises OperationLimitExceeded if more operations are requested than | ||
| # allowed by the BatchApi configuration. | ||
| # Raises BadOptionError if other provided options are invalid. | ||
| # Raises ArgumentError if no operations are provided (nil or []). | ||
| # | ||
| # Returns the new Processor instance. | ||
| def initialize(ops, env, options = {}) | ||
| raise ArgumentError, "No operations provided" if ops.blank? | ||
|
|
||
| @env = env | ||
| @ops = self.process_ops(ops) | ||
| @options = self.process_options(options) | ||
| end | ||
|
|
||
| # Public: the processing strategy to use, based on the options | ||
| # provided in BatchApi setup and the request. | ||
| # Currently only Sequential is supported. | ||
| def strategy | ||
| BatchApi::Processor::Strategies::Sequential | ||
| end | ||
|
|
||
| # Public: run the batch operations according to the appropriate strategy. | ||
| # | ||
| # Returns a set of BatchResponses | ||
| def execute! | ||
| strategy.execute!(@ops, @options) | ||
| end | ||
|
|
||
| protected | ||
|
|
||
| # Internal: Validate that an allowable number of operations have been | ||
| # provided, and turn them into BatchApi::Operation objects. | ||
| # | ||
| # ops - a series of operations | ||
| # | ||
| # Raises OperationLimitExceeded if more operations are requested than | ||
| # allowed by the BatchApi configuration. | ||
| # | ||
| # Returns an array of BatchApi::Operation objects | ||
| def process_ops(ops) | ||
| if ops.length > BatchApi.config.limit | ||
| raise OperationLimitExceeded, | ||
| "Only #{BatchApi.config.limit} operations can be submitted at once, " + | ||
| "#{ops.length} were provided" | ||
| else | ||
| ops.map do |op| | ||
| BatchApi::Operation.new(op, @env) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Internal: Processes any other provided options for validity. | ||
| # Currently, the :sequential option is REQUIRED (until parallel | ||
| # implementation is created). | ||
| # | ||
| # options - an options hash | ||
| # | ||
| # Returns the valid options hash. | ||
| def process_options(options) | ||
| raise BadOptionError, "Sequential flag is currently required" unless options[:sequential] | ||
| options | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,18 @@ | ||
| module BatchApi | ||
| class Processor | ||
| module Strategies | ||
| module Sequential | ||
| # Public: execute all operations sequentially. | ||
| # | ||
| # ops - a set of BatchApi::Operations | ||
| # options - a set of options | ||
| # | ||
| # Returns an array of BatchApi::Response objects. | ||
| def self.execute!(ops, options = {}) | ||
| ops.map(&:execute) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
This file contains hidden or 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
This file contains hidden or 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,3 +1,3 @@ | ||
| module BatchApi | ||
| VERSION = "0.0.2" | ||
| VERSION = "0.0.3" | ||
| end |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,70 @@ | ||
| require 'spec_helper' | ||
|
|
||
| describe BatchApi::Processor do | ||
| it "provides a OperationLimitExceeded error" do | ||
| BatchApi::Processor::OperationLimitExceeded.superclass.should == StandardError | ||
| end | ||
|
|
||
| it "provides a OperationLimitExceeded error" do | ||
| BatchApi::Processor::BadOptionError.superclass.should == StandardError | ||
| end | ||
|
|
||
| let(:ops) { [ {url: "/endpoint", method: "get"} ] } | ||
| let(:options) { { sequential: true } } | ||
| let(:env) { {} } | ||
| let(:processor) { BatchApi::Processor.new(ops, env, options) } | ||
|
|
||
| describe "#initialize" do | ||
| # this may be brittle...consider refactoring? | ||
| it "turns the ops provided into BatchApi::Operations stored at #ops" do | ||
| # simulate receiving several operations | ||
| operation_objects = 3.times.collect { stub("operation object") } | ||
| operation_params = 3.times.collect do |i| | ||
| stub("raw operation").tap do |o| | ||
| BatchApi::Operation.should_receive(:new).with(o, anything).and_return(operation_objects[i]) | ||
| end | ||
| end | ||
|
|
||
| BatchApi::Processor.new(operation_params, env, options).ops.should == operation_objects | ||
| end | ||
|
|
||
| it "makes the options available" do | ||
| BatchApi::Processor.new(ops, env, options).options.should == options | ||
| end | ||
|
|
||
| context "error conditions" do | ||
| it "(currently) throws an error if sequential is not true" do | ||
| expect { BatchApi::Processor.new(ops, env, {}) }.to raise_exception(BatchApi::Processor::BadOptionError) | ||
| end | ||
|
|
||
| it "raise a OperationLimitExceeded error if too many ops provided" do | ||
| ops = (BatchApi.config.limit + 1).times.collect {|i| i} | ||
| expect { BatchApi::Processor.new(ops, env, options) }.to raise_exception(BatchApi::Processor::OperationLimitExceeded) | ||
| end | ||
|
|
||
| it "raises an ArgumentError if operations.blank?" do | ||
| expect { BatchApi::Processor.new(nil, env, options) }.to raise_exception(ArgumentError) | ||
| expect { BatchApi::Processor.new(nil, env, []) }.to raise_exception(ArgumentError) | ||
| end | ||
| end | ||
|
|
||
| describe "#strategy" do | ||
| it "returns BatchApi::Processor::Strategies::Sequential" do | ||
| processor.strategy.should == BatchApi::Processor::Strategies::Sequential | ||
| end | ||
| end | ||
|
|
||
| describe "#execute!" do | ||
| it "executes on the provided strategy" do | ||
| processor.strategy.should_receive(:execute!).with(processor.ops, processor.options) | ||
| processor.execute! | ||
| end | ||
|
|
||
| it "returns the result of the strategy" do | ||
| stubby = stub | ||
| processor.strategy.stub(:execute!).and_return(stubby) | ||
| processor.execute!.should == stubby | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,5 @@ | ||
| require 'spec_helper' | ||
|
|
||
| describe BatchApi::Processor::Strategies::Sequential do | ||
| pending "needs a test" | ||
| end |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no_method = op_params.except(:method)