Class: Berkshelf::SiteLocation

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Location
Defined in:
lib/berkshelf/locations/site_location.rb

Constant Summary collapse

SHORTNAMES =
{opscode: CommunityREST::V1_API}.freeze

Constants included from Location

Location::OPSCODE_COMMUNITY_API

Instance Attribute Summary collapse

Attributes included from Location

#name

Instance Method Summary collapse

Methods included from Location

included, init, #to_json, #validate_cached

Constructor Details

#initialize(name, version_constraint, options = {}) ⇒ SiteLocation

Returns a new instance of SiteLocation.

Parameters:

  • name (#to_s)
  • version_constraint (Solve::Constraint)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :site (String, Symbol)

    a URL pointing to a community API endpoint. Alternatively the symbol :opscode can be provided to initialize a SiteLocation pointing to the Opscode Community Site.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/berkshelf/locations/site_location.rb', line 20

def initialize(name, version_constraint, options = {})
  @name               = name
  @version_constraint = version_constraint

  api_uri = if options[:site].nil?
    SHORTNAMES[:opscode]
  elsif SHORTNAMES.has_key?(options[:site])
    SHORTNAMES[options[:site]]
  elsif options[:site].kind_of?(Symbol)
    raise InvalidSiteShortnameError.new(options[:site])
  else
    options[:site]
  end

  @conn = Berkshelf::CommunityREST.new(api_uri)
end

Instance Attribute Details

#version_constraintObject

Returns the value of attribute version_constraint.



9
10
11
# File 'lib/berkshelf/locations/site_location.rb', line 9

def version_constraint
  @version_constraint
end

Instance Method Details

#download(destination) ⇒ Berkshelf::CachedCookbook

Parameters:

  • destination (#to_s)

Returns:



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/berkshelf/locations/site_location.rb', line 40

def download(destination)
  version    = target_version
  berks_path = File.join(destination, "#{name}-#{version}")

  temp_path = conn.download(name, version)
  FileUtils.mv(File.join(temp_path, name), berks_path)

  cached = CachedCookbook.from_store_path(berks_path)
  validate_cached(cached)

  cached
end

#latest_versionString

Return the latest version that the site location has for the the cookbook

Returns:



56
57
58
# File 'lib/berkshelf/locations/site_location.rb', line 56

def latest_version
  conn.latest_version(name)
end

#target_versionString

Returns a string representing the version of the cookbook that should be downloaded for this location

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/berkshelf/locations/site_location.rb', line 64

def target_version
  version = if version_constraint
    conn.satisfy(name, version_constraint)
  else
    latest_version
  end

  if version.nil?
    msg = "Cookbook '#{name}' found at #{self}"
    msg << " that would satisfy constraint (#{version_constraint}" if version_constraint
    raise CookbookNotFound, msg
  end

  version
end

#to_hashObject



80
81
82
# File 'lib/berkshelf/locations/site_location.rb', line 80

def to_hash
  super.merge(value: self.api_uri)
end

#to_sObject



84
85
86
# File 'lib/berkshelf/locations/site_location.rb', line 84

def to_s
  "#{self.class.location_key}: '#{api_uri}'"
end