Sha256: 50ae275fc638ba5c9acb990e15540ba68a403b17ec962d67817ac149b64b5cf1

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

require 'rest-client'

module Codestatus
  module PackageResolvers
    class NpmResolver < Base
      NPM_REGISTRY_ENDPOINT = 'https://registry.npmjs.org/'.freeze

      private

      def found?
        package_info
      end

      def urls
        [
          bugs_url,
          repository_url,
          homepage_url,
        ].compact
      end

      def bugs_url
        package_info&.dig('bugs', 'url')
      end

      def homepage_url
        package_info&.dig('homepage')
      end

      def repository_url
        package_info&.dig('repository', 'url')
      end

      def package_info
        @package_info ||= request(package_uri)
      end

      def request(uri)
        response = client.get(uri)
        JSON.parse(response)
      rescue RestClient::NotFound
        nil
      end

      def client
        RestClient
      end

      def package_uri
        File.join(NPM_REGISTRY_ENDPOINT, slash_escaped_package)
      end

      # for scoped package
      #   For example, to access @atlassian/aui information,
      #   we must use https://registry.npmjs.org/@atlassian%2Faui,
      #   not https://registry.npmjs.org/%40atlassian%2Faui.
      def slash_escaped_package
        package.gsub('/', CGI.escape('/'))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
codestatus-0.1.3 lib/codestatus/package_resolvers/npm_resolver.rb