From 13be055669498cc0b14227146fad73abba1abbb4 Mon Sep 17 00:00:00 2001 From: Drew Bomhof Date: Fri, 20 Jan 2017 16:12:04 -0500 Subject: [PATCH 1/3] Exposed playbooks as a collection Playbooks are only available from a project I expose a playbooks as a collection from a project The Tower API returns playbooks as an Array, vs a Hash from POST, PUT, PATCH(able) endpoints The parse_result_set method was modified to handle each case https://www.pivotaltracker.com/story/show/137293767 --- lib/ansible_tower_client/base_models/project.rb | 4 ++++ lib/ansible_tower_client/collection.rb | 15 ++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/ansible_tower_client/base_models/project.rb b/lib/ansible_tower_client/base_models/project.rb index 7224ab8..91ec6ba 100644 --- a/lib/ansible_tower_client/base_models/project.rb +++ b/lib/ansible_tower_client/base_models/project.rb @@ -3,5 +3,9 @@ class Project < BaseModel def self.endpoint "projects".freeze end + + def playbooks + Collection.new(api).find_all_by_url(related['playbooks']) + end end end diff --git a/lib/ansible_tower_client/collection.rb b/lib/ansible_tower_client/collection.rb index 37751d5..cf47e54 100644 --- a/lib/ansible_tower_client/collection.rb +++ b/lib/ansible_tower_client/collection.rb @@ -47,17 +47,22 @@ def class_from_type(type) def fetch_more_results(next_page, get_options) return if next_page.nil? body = parse_response(api.get(next_page, get_options)) - parse_result_set(body["results"]) - - body["next"] + parse_result_set(body) end def parse_response(response) JSON.parse(response.body) end - def parse_result_set(results) - results.each { |result| @collection << build_object(result) } + def parse_result_set(body) + case body.class.name + when "Array" then + body.each { |result| @collection << result } + nil + when "Hash" then + body["results"].each { |result| @collection << build_object(result) } + body["next"] + end end def build_object(result) From 8f6369a73d18cec31d7b072a32e1cb06e8d6d13a Mon Sep 17 00:00:00 2001 From: Drew Bomhof Date: Fri, 20 Jan 2017 16:50:47 -0500 Subject: [PATCH 2/3] Added specs around an endpoint returning an Array --- spec/support/shared_examples/collection_methods.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/support/shared_examples/collection_methods.rb b/spec/support/shared_examples/collection_methods.rb index ca99196..b89c9a1 100644 --- a/spec/support/shared_examples/collection_methods.rb +++ b/spec/support/shared_examples/collection_methods.rb @@ -18,6 +18,20 @@ expect(obj_collection_array.first.url).to eq(url) end + it ".find_all_by_url returns a collection of items via a url" do + raw_collection_array = raw_url_collection.to_a.flatten + expect(api).to receive(:get).and_return(instance_double("Faraday::Result", :body => raw_collection_array.to_json)) + url = raw_url_collection['url'] + + obj_collection = collection.find_all_by_url(url) + expect(obj_collection).to be_a Enumerator + + obj_collection_array = obj_collection.to_a + expect(obj_collection_array.length).to eq(8) + expect(obj_collection_array.first).to_not eq described_class + expect(obj_collection_array.first).to be_a String + end + it ".find returns an instance" do expect(api).to receive(:get).and_return(instance_double("Faraday::Result", :body => raw_instance.to_json)) From fd8c6cfd04b83fc0e64ea481c4ceffb203442dbb Mon Sep 17 00:00:00 2001 From: Drew Bomhof Date: Fri, 20 Jan 2017 18:09:11 -0500 Subject: [PATCH 3/3] Small refactor No need to iterate over an array. --- lib/ansible_tower_client/collection.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ansible_tower_client/collection.rb b/lib/ansible_tower_client/collection.rb index cf47e54..b6c9a9f 100644 --- a/lib/ansible_tower_client/collection.rb +++ b/lib/ansible_tower_client/collection.rb @@ -57,7 +57,7 @@ def parse_response(response) def parse_result_set(body) case body.class.name when "Array" then - body.each { |result| @collection << result } + @collection = body nil when "Hash" then body["results"].each { |result| @collection << build_object(result) }