Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #86 from bdunne/dont_reset_get_options_on_iteration
Browse files Browse the repository at this point in the history
Allow collection enumerator to be enumerated multiple times
  • Loading branch information
gmcculloug authored Apr 13, 2017
2 parents 1a56e64 + 2f855ff commit eeaf3b9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/ansible_tower_client/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ def find_all_by_url(url, get_options = nil)
Enumerator.new do |yielder|
@collection = []
next_page = url
options = get_options

loop do
next_page = fetch_more_results(next_page, get_options) if @collection.empty?
get_options = nil
next_page = fetch_more_results(next_page, options) if @collection.empty?
options = nil
raise StopIteration if @collection.empty?
yielder.yield(@collection.shift)
end
Expand Down
23 changes: 23 additions & 0 deletions spec/collection_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
describe AnsibleTowerClient::Collection do
let(:connection) { double("connection") }
let(:mock_api) { AnsibleTowerClient::Api.new(connection) }
let(:instance) { described_class.new(mock_api) }
let(:test_url) { "/api/v1/things/1/related_things/" }
let(:get_options) { {"key" => "value"} }
let(:response_1) { double("response", :body => body_1) }
let(:body_1) { {"next_page" => nil, "results" => (1..5).collect { |i| {:id => i, :type => "host"} }}.to_json }

context "#find_all_by_url" do
it "is an Enumerator" do
expect(instance.find_all_by_url(test_url)).to be_kind_of(Enumerator)
end

it "ensures all get_options are applied to all requests" do
expect(mock_api).to receive(:get).with(anything, get_options).twice.and_return(response_1)

collection = instance.find_all_by_url(test_url, get_options)
expect(collection.count).to eq(5)
expect(collection.count).to eq(5)
end
end
end

0 comments on commit eeaf3b9

Please sign in to comment.