Module: Spice::Connection::Cookbooks

Included in:
Spice, Spice::Connection
Defined in:
lib/spice/connection/cookbooks.rb

Instance Method Summary collapse

Instance Method Details

#cookbook(name) ⇒ Spice::Cookbook

Retrieve a single cookbook

Parameters:

  • name (String)

    The name of the cookbook

Returns:

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/spice/connection/cookbooks.rb', line 28

def cookbook(name)
  if Gem::Version.new(Spice.chef_version) >= Gem::Version.new("0.10.0")
    cookbook = connection.get("/cookbooks/#{name}").body
    puts cookbook[name]
    versions = cookbook[name]['versions'].map{ |v| v['version'] }

    Spice::Cookbook.new(:name => name, :versions => versions)
  else
    connection.get("/cookbooks").body.keys.map do |cookbook|
      attributes = connection.get("/cookbooks/#{cookbook}").body.to_a[0]
      Spice::Cookbook.new(:name => attributes[0], :versions => attributes[1])
    end
  end
end

#cookbook_version(name, version) ⇒ Spice::CookbookVersion

Retrieve a single cookbook version

Parameters:

  • name (String)

    The cookbook name

  • version (String)

    The cookbook version

Returns:

Raises:



48
49
50
51
# File 'lib/spice/connection/cookbooks.rb', line 48

def cookbook_version(name, version)
  attributes = connection.get("/cookbooks/#{name}/#{version}").body
  Spice::CookbookVersion.new(attributes)
end

#cookbooksArray<Spice::Cookbook>

Retrieve an array of all cookbooks

Examples:

Retrieve all cookbooks with versions

Spice.cookbooks

Returns:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/spice/connection/cookbooks.rb', line 8

def cookbooks
  if Gem::Version.new(Spice.chef_version) >= Gem::Version.new("0.10.0")
    cookbooks = []
    connection.get("/cookbooks").body.each_pair do |key, value|
      versions = value['versions'].map{ |v| v['version'] }
      cookbooks << Spice::Cookbook.new(:name => key, :versions => versions)
    end
    cookbooks
  else
    connection.get("/cookbooks").body.keys.map do |cookbook|
      attributes = connection.get("/cookbooks/#{cookbook}").body.to_a[0]
      Spice::Cookbook.new(:name => attributes[0], :versions => attributes[1])
    end
  end
end