Class: Kontena::PluginManager::RubygemsClient

Inherits:
Object
  • Object
show all
Defined in:
lib/kontena/plugin_manager/rubygems_client.rb

Constant Summary collapse

JSON_MIME =
'application/json'.freeze
ACCEPT =
'Accept'.freeze
HTTPOK =
"200".freeze

Instance Method Summary collapse

Instance Method Details

#clientObject



31
32
33
34
35
36
# File 'lib/kontena/plugin_manager/rubygems_client.rb', line 31

def client
  return @client if @client
  @client = Net::HTTP.new('rubygems.org', 443)
  @client.use_ssl = true
  @client
end

#get(path, query = nil) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/kontena/plugin_manager/rubygems_client.rb', line 50

def get(path, query = nil)
  Kontena.logger.debug { "Requesting GET #{path}" }
  response = client.request(get_request(request_path(path, query)))
  Kontena.logger.debug { "Response #{response.code}" }
  raise "Server responded with #{response.code} (#{response.class.name})" unless response.code == HTTPOK
  JSON.parse(response.body)
end

#get_request(path) ⇒ Object



44
45
46
47
48
# File 'lib/kontena/plugin_manager/rubygems_client.rb', line 44

def get_request(path)
  request = Net::HTTP::Get.new(path)
  request[ACCEPT] = JSON_MIME
  request
end

#latest_version(gem_name, pre: false) ⇒ Object

Get the latest version number from rubygems

Parameters:

  • plugin_name (String)
  • pre (Boolean) (defaults to: false)

    include prerelease versions



26
27
28
29
# File 'lib/kontena/plugin_manager/rubygems_client.rb', line 26

def latest_version(gem_name, pre: false)
  return versions(gem_name).first if pre
  versions(gem_name).find { |version| !version.prerelease? }
end

#request_path(path, query = nil) ⇒ Object



38
39
40
41
42
# File 'lib/kontena/plugin_manager/rubygems_client.rb', line 38

def request_path(path, query = nil)
  uri = URI(path)
  uri.query = URI.encode_www_form(query) if query
  uri.to_s
end

#search(pattern = nil) ⇒ Object



14
15
16
# File 'lib/kontena/plugin_manager/rubygems_client.rb', line 14

def search(pattern = nil)
  get('/api/v1/search.json', query: pattern)
end

#versions(gem_name) ⇒ Object



18
19
20
21
# File 'lib/kontena/plugin_manager/rubygems_client.rb', line 18

def versions(gem_name)
  response = get("/api/v1/versions/#{gem_name}.json")
  response.map { |version| Gem::Version.new(version["number"]) }.sort.reverse
end