Class: AnsibleTowerClient::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/ansible_tower_client/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, klass = nil) ⇒ Collection

Returns a new instance of Collection.



4
5
6
7
# File 'lib/ansible_tower_client/collection.rb', line 4

def initialize(api, klass = nil)
  @api   = api
  @klass = klass
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



3
4
5
# File 'lib/ansible_tower_client/collection.rb', line 3

def api
  @api
end

#klassObject (readonly)

Returns the value of attribute klass.



3
4
5
# File 'lib/ansible_tower_client/collection.rb', line 3

def klass
  @klass
end

Instance Method Details

#all(get_options = nil) ⇒ Object

Parameters:

  • get_options (Hash) (defaults to: nil)

    a hash of http GET params to pass to the api request e.g. { :order_by => ‘timestamp’, :name__contains => ‘foo’ }



11
12
13
# 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) ⇒ Object



38
39
40
# File 'lib/ansible_tower_client/collection.rb', line 38

def create(*args)
  klass.create(api, *args)
end

#create!(*args) ⇒ Object



34
35
36
# File 'lib/ansible_tower_client/collection.rb', line 34

def create!(*args)
  klass.create!(api, *args)
end

#find(id) ⇒ Object



30
31
32
# 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) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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