Sha256: c1442dd8e91c16d653924630585fc1ea6c77e32532a5e74b0f2342502f1e6011
Contents?: true
Size: 1.25 KB
Versions: 4
Compression:
Stored size: 1.25 KB
Contents
module AnsibleTowerClient class Collection attr_reader :api, :klass def initialize(api, klass = nil) @api = api @klass = klass end def all find_all_by_url(klass.endpoint) end def find_all_by_url(url) Enumerator.new do |yielder| @collection = [] next_page = url loop do next_page = fetch_more_results(next_page) if @collection.empty? raise StopIteration if @collection.empty? yielder.yield(@collection.shift) end end end def find(id) build_object(parse_response(api.get("#{klass.endpoint}/#{id}/"))) end private def class_from_type(type) camelized = type.split("_").collect(&:capitalize).join AnsibleTowerClient.const_get(camelized) end def fetch_more_results(next_page) return if next_page.nil? body = parse_response(api.get(next_page)) parse_result_set(body["results"]) body["next"] end def parse_response(response) JSON.parse(response.body) end def parse_result_set(results) results.each { |result| @collection << build_object(result) } end def build_object(result) class_from_type(result["type"]).new(api, result) end end end
Version data entries
4 entries across 4 versions & 1 rubygems