Class: Puppet::Forge

Inherits:
SemanticPuppet::Dependency::Source show all
Includes:
Errors
Defined in:
lib/puppet/forge.rb,
lib/puppet/forge/cache.rb,
lib/puppet/forge/repository.rb

Defined Under Namespace

Modules: Errors Classes: Cache, ModuleRelease, Repository

Constant Summary collapse

USER_AGENT =
"PMT/1.1.1 (v3; Net::HTTP)".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SemanticPuppet::Dependency::Source

#create_release, #priority, priority

Constructor Details

#initialize(host = Puppet[:module_repository], strict_semver = true) ⇒ Forge

Returns a new instance of Forge.



22
23
24
25
26
# File 'lib/puppet/forge.rb', line 22

def initialize(host = Puppet[:module_repository], strict_semver = true)
  @host = host
  @repository = Puppet::Forge::Repository.new(host, USER_AGENT)
  @strict_semver = strict_semver
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



20
21
22
# File 'lib/puppet/forge.rb', line 20

def host
  @host
end

#repositoryObject (readonly)

Returns the value of attribute repository.



20
21
22
# File 'lib/puppet/forge.rb', line 20

def repository
  @repository
end

Instance Method Details

#fetch(input) ⇒ Array<SemanticPuppet::Dependency::ModuleRelease>

Fetches ModuleRelease entries for each release of the named module.

Parameters:

  • input (String)

    the module name to look up

Returns:

See Also:

  • SemanticPuppet::Dependency::Source#fetch


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/puppet/forge.rb', line 91

def fetch(input)
  name = input.tr('/', '-')
  uri = "/v3/releases?module=#{name}"
  if Puppet[:module_groups]
    uri += "&module_groups=#{Puppet[:module_groups].gsub('+', ' ')}"
  end
  releases = []

  while uri
    # make_http_request URI encodes parameters
    response = make_http_request(uri)

    if response.code == '200'
      response = JSON.parse(response.body)
    else
      raise ResponseError.new(:uri => URI.parse(@host).merge(uri), :response => response)
    end

    releases.concat(process(response['results']))
    uri = response['pagination']['next']
  end

  return releases
end

#make_http_request(*args) ⇒ Object



116
117
118
# File 'lib/puppet/forge.rb', line 116

def make_http_request(*args)
  @repository.make_http_request(*args)
end

#search(term) ⇒ Array

Return a list of module metadata hashes that match the search query. This return value is used by the module_tool face install search, and displayed to on the console.

Example return value:

[

{
  "author"      => "puppetlabs",
  "name"        => "bacula",
  "tag_list"    => ["backup", "bacula"],
  "releases"    => [{"version"=>"0.0.1"}, {"version"=>"0.0.2"}],
  "full_name"   => "puppetlabs/bacula",
  "version"     => "0.0.2",
  "project_url" => "https://github.com/puppetlabs/puppetlabs-bacula",
  "desc"        => "bacula"
}

]

Parameters:

  • term (String)

    search term

Returns:

  • (Array)

    modules found

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/puppet/forge.rb', line 55

def search(term)
  matches = []
  uri = "/v3/modules?query=#{term}"
  if Puppet[:module_groups]
    uri += "&module_groups=#{Puppet[:module_groups].gsub('+', ' ')}"
  end

  while uri
    # make_http_request URI encodes parameters
    response = make_http_request(uri)

    if response.code == '200'
      result = JSON.parse(response.body)
      uri = result['pagination']['next']
      matches.concat result['results']
    else
      raise ResponseError.new(:uri => URI.parse(@host).merge(uri), :response => response)
    end
  end

  matches.each do |mod|
    mod['author'] = mod['owner']['username']
    mod['tag_list'] = mod['current_release']['tags']
    mod['full_name'] = "#{mod['author']}/#{mod['name']}"
    mod['version'] = mod['current_release']['version']
    mod['project_url'] = mod['homepage_url']
    mod['desc'] = mod['current_release']['metadata']['summary'] || ''
  end
end