Module: Merb::CookbookVersionHelper

Includes:
ControllerExceptions
Included in:
Cookbooks, Environments
Defined in:
app/helpers/cookbook_version_helper.rb

Instance Method Summary collapse

Instance Method Details

#expand_cookbook_urls(cookbook_name, versions, num_versions) ⇒ Object

takes a cookbook_name and an array of versions and returns a hash params:

  • cookbook_name: the name of the cookbook

  • versions: a sorted list of version strings

returns: {

:url => http://url,
:versions => [
  { :version => version, :url => http://url/version },
  { :version => version, :url => http://url/version }
]

}



37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/helpers/cookbook_version_helper.rb', line 37

def expand_cookbook_urls(cookbook_name, versions, num_versions)
  versions = versions[0...num_versions.to_i] unless num_versions == "all"
  version_list = versions.inject([]) do |res, version|
    res.push({ 
      :url => absolute_url(:cookbook_version, :cookbook_name => cookbook_name, :cookbook_version => version),
      :version => version
    })
    res
  end
  url = absolute_url(:cookbook, :cookbook_name => cookbook_name)
  {:url => url, :versions => version_list}
end

#num_versions!(default = "1") ⇒ Object

validate and return the number of versions requested by the user

raises an exception if an invalid number is requested

params:

  • default: the number of versions to default to

valid num_versions query parameter:

  • an integer >= 0

  • the string “all”



61
62
63
64
65
66
67
68
69
70
# File 'app/helpers/cookbook_version_helper.rb', line 61

def num_versions!(default="1")
  input = params[:num_versions]
  result = if input
             valid_input = (input == "all" || Integer(input) >= 0) rescue false
             raise BadRequest, "You have requested an invalid number of versions (x >= 0 || 'all')" unless valid_input
             input
           else
             default
           end
end