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 #62 from syncrou/expose_playbooks_off_of_projects
Browse files Browse the repository at this point in the history
Expose playbooks off of projects
  • Loading branch information
bdunne authored Jan 20, 2017
2 parents 203359a + fd8c6cf commit 1abb87e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/ansible_tower_client/base_models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 10 additions & 5 deletions lib/ansible_tower_client/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
@collection = body
nil
when "Hash" then
body["results"].each { |result| @collection << build_object(result) }
body["next"]
end
end

def build_object(result)
Expand Down
14 changes: 14 additions & 0 deletions spec/support/shared_examples/collection_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down

0 comments on commit 1abb87e

Please sign in to comment.