Sha256: fc82db86065803296afb2526bed7941ec49893ee811103302a7d3b444b3ca74e

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

require 'rest-client'

module Codestatus
  class RepositoryResolver
    class NpmResolver
      GITHUB_REPOSITORY_REGEXP = %r{(https|git)://github.com/(?<owner>[^/]*)/(?<repo>[^/]*)(\.git)?/?.*}.freeze
      NPM_REGISTRY_ENDPOINT = 'https://registry.npmjs.org/'.freeze

      def resolve(registry:, package:)
        return unless registry.to_s == 'npm'
        @package = package

        github_repository
      end

      attr_reader :package

      private

      # FIXME: Copied from RubygemsResolver
      def github_repository
        result = nil
        urls.each do |url|
          matched = GITHUB_REPOSITORY_REGEXP.match(url)
          next unless matched

          repo = [matched[:owner], matched[:repo]].join('/')
          result = Codestatus::PackageRepository.new(github: repo)
          break
        end
        result
      end

      def package_info
        @package_info ||= JSON.parse(client.get("#{NPM_REGISTRY_ENDPOINT}/#{package}"))
      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 client
        RestClient
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
codestatus-0.1.1 lib/codestatus/repository_resolver/npm_resolver.rb