Sha256: 28f1088749e6b5548331da5785a5505b559c3433ca32900d5d24327fb7d35fb0

Contents?: true

Size: 1.64 KB

Versions: 53

Compression:

Stored size: 1.64 KB

Contents

require 'kontena_cli'
require 'json'
require 'uri'
require 'net/https'

module Kontena
  module PluginManager
    class RubygemsClient

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

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

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

      # Get the latest version number from rubygems
      # @param plugin_name [String]
      # @param pre [Boolean] include prerelease versions
      def latest_version(gem_name, pre: false)
        return versions(gem_name).first if pre
        versions(gem_name).find { |version| !version.prerelease? }
      end

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

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

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

      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
    end
  end
end

Version data entries

53 entries across 53 versions & 2 rubygems

Version Path
krates-1.7.11 lib/kontena/plugin_manager/rubygems_client.rb
krates-1.7.10 lib/kontena/plugin_manager/rubygems_client.rb
krates-1.7.9 lib/kontena/plugin_manager/rubygems_client.rb
krates-1.7.8 lib/kontena/plugin_manager/rubygems_client.rb
krates-1.7.7 lib/kontena/plugin_manager/rubygems_client.rb
krates-1.7.6 lib/kontena/plugin_manager/rubygems_client.rb
krates-1.7.5 lib/kontena/plugin_manager/rubygems_client.rb
krates-1.7.4 lib/kontena/plugin_manager/rubygems_client.rb
krates-1.7.3 lib/kontena/plugin_manager/rubygems_client.rb
krates-1.7.2 lib/kontena/plugin_manager/rubygems_client.rb
krates-1.7.1 lib/kontena/plugin_manager/rubygems_client.rb
krates-1.7.0 lib/kontena/plugin_manager/rubygems_client.rb
krates-1.6.9 lib/kontena/plugin_manager/rubygems_client.rb
krates-1.6.8 lib/kontena/plugin_manager/rubygems_client.rb
krates-1.6.7 lib/kontena/plugin_manager/rubygems_client.rb
krates-1.6.6 lib/kontena/plugin_manager/rubygems_client.rb
krates-1.6.5 lib/kontena/plugin_manager/rubygems_client.rb
krates-1.6.4 lib/kontena/plugin_manager/rubygems_client.rb
krates-1.6.3 lib/kontena/plugin_manager/rubygems_client.rb
krates-1.6.2 lib/kontena/plugin_manager/rubygems_client.rb