class AnsibleTowerClient::Collection
Attributes
api[R]
klass[R]
Public Class Methods
new(api, klass = nil)
click to toggle source
# File lib/ansible_tower_client/collection.rb, line 4 def initialize(api, klass = nil) @api = api @klass = klass end
Public Instance Methods
all(get_options = nil)
click to toggle source
@param get_options [Hash] a hash of http GET params to pass to the api request
e.g. { :order_by => 'timestamp', :name__contains => 'foo' }
# File lib/ansible_tower_client/collection.rb, line 11 def all(get_options = nil) find_all_by_url("#{klass.endpoint}/", get_options) end
create(*args)
click to toggle source
# File lib/ansible_tower_client/collection.rb, line 38 def create(*args) klass.create(api, *args) end
create!(*args)
click to toggle source
# File lib/ansible_tower_client/collection.rb, line 34 def create!(*args) klass.create!(api, *args) end
find(id)
click to toggle source
# File lib/ansible_tower_client/collection.rb, line 30 def find(id) build_object(parse_response(api.get(find_uri(id)))) end
find_all_by_url(url, get_options = nil)
click to toggle source
# File lib/ansible_tower_client/collection.rb, line 15 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, options) if @collection.empty? options = nil raise StopIteration if @collection.empty? yielder.yield(@collection.shift) end end end
Private Instance Methods
build_object(result)
click to toggle source
# File lib/ansible_tower_client/collection.rb, line 73 def build_object(result) (klass || class_from_type(result['type'])).new(api, result) end
class_from_type(type)
click to toggle source
# File lib/ansible_tower_client/collection.rb, line 44 def class_from_type(type) api.send("#{type}_class") end
fetch_more_results(next_page, get_options)
click to toggle source
# File lib/ansible_tower_client/collection.rb, line 48 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) end
find_uri(id)
click to toggle source
# File lib/ansible_tower_client/collection.rb, line 54 def find_uri(id) File.join(klass.endpoint, id.to_s, "/") end
parse_response(response)
click to toggle source
# File lib/ansible_tower_client/collection.rb, line 58 def parse_response(response) JSON.parse(response.body) end
parse_result_set(body)
click to toggle source
# File lib/ansible_tower_client/collection.rb, line 62 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