Class: Berkshelf::Source

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/berkshelf/source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Source

Returns a new instance of Source.

Parameters:



11
12
13
14
15
# File 'lib/berkshelf/source.rb', line 11

def initialize(uri)
  @uri        = SourceURI.parse(uri)
  @api_client = APIClient.new(uri, ssl: {verify: Berkshelf::Config.instance.ssl.verify})
  @universe   = nil
end

Instance Attribute Details

#uriBerkshelf::SourceURI (readonly)



8
9
10
# File 'lib/berkshelf/source.rb', line 8

def uri
  @uri
end

Instance Method Details

#==(other) ⇒ Object



94
95
96
97
# File 'lib/berkshelf/source.rb', line 94

def ==(other)
  return false unless other.is_a?(self.class)
  uri == other.uri
end

#build_universeArray<APIClient::RemoteCookbook>

Forcefully obtain the universe from the API endpoint and assign it to #universe. This will reload the value of #universe even if it has been loaded before.

Returns:

  • (Array<APIClient::RemoteCookbook>)


21
22
23
24
25
26
# File 'lib/berkshelf/source.rb', line 21

def build_universe
  @universe = api_client.universe
rescue => ex
  @universe = Array.new
  raise ex
end

#cookbook(name, version) ⇒ APIClient::RemoteCookbook

Parameters:

Returns:

  • (APIClient::RemoteCookbook)


43
44
45
# File 'lib/berkshelf/source.rb', line 43

def cookbook(name, version)
  universe.find { |cookbook| cookbook.name == name && cookbook.version == version }
end

#default?true, false

Determine if this source is a “default” source, as defined in the Berksfile.

Returns:

  • (true, false)

    true if this a default source, false otherwise



64
65
66
# File 'lib/berkshelf/source.rb', line 64

def default?
  @default_ ||= @uri.host == URI.parse(Berksfile::DEFAULT_API_URL).host
end

#hashObject



90
91
92
# File 'lib/berkshelf/source.rb', line 90

def hash
  @uri.host.hash
end

#inspectObject



86
87
88
# File 'lib/berkshelf/source.rb', line 86

def inspect
  "#<#{self.class.name} uri: #{@uri.to_s.inspect}>"
end

#latest(name) ⇒ APIClient::RemoteCookbook

Parameters:

Returns:

  • (APIClient::RemoteCookbook)


71
72
73
# File 'lib/berkshelf/source.rb', line 71

def latest(name)
  versions(name).sort.last
end

#search(name) ⇒ Array<APIClient::RemoteCookbook]

The list of remote cookbooks that match the given query.

Parameters:

Returns:

  • (Array<APIClient::RemoteCookbook])

    Array<APIClient::RemoteCookbook]



52
53
54
55
56
57
# File 'lib/berkshelf/source.rb', line 52

def search(name)
  universe
    .select { |cookbook| cookbook.name =~ Regexp.new(name) }
    .group_by(&:name)
    .collect { |name, versions| versions.max_by(&:version) }
end

#to_sObject



82
83
84
# File 'lib/berkshelf/source.rb', line 82

def to_s
  "#{uri}"
end

#universeArray<APIClient::RemoteCookbook>

Return the universe from the API endpoint.

This is lazily loaded so the universe will be retrieved from the API endpoint on the first call and cached for future calls. Send the #build_universe message if you want to reload the cached universe.

Returns:

  • (Array<APIClient::RemoteCookbook>)


35
36
37
# File 'lib/berkshelf/source.rb', line 35

def universe
  @universe || build_universe
end

#versions(name) ⇒ Array<APIClient::RemoteCookbook>

Parameters:

Returns:

  • (Array<APIClient::RemoteCookbook>)


78
79
80
# File 'lib/berkshelf/source.rb', line 78

def versions(name)
  universe.select { |cookbook| cookbook.name == name }
end