Sha256: 985c0d628eebfedfc6afa7cac0e3a9f32ec3d730c636c25b00bf37c8688f5b42

Contents?: true

Size: 867 Bytes

Versions: 1

Compression:

Stored size: 867 Bytes

Contents

require "json"
require "httpclient"

class CompareLinker
  class GithubLinkFinder
    attr_reader :octokit, :repo_owner, :repo_name, :homepage_uri

    def initialize(octokit)
      @octokit = octokit
    end

    def find(gem_name)
      gem_info = JSON.parse(
        HTTPClient.get_content("https://rubygems.org/api/v1/gems/#{gem_name}.json")
      )

      github_url = [
        gem_info["homepage_uri"],
        gem_info["source_code_uri"]
      ].find { |uri| uri.to_s.match(/github\.com\//) }

      if github_url.nil?
        @homepage_uri = gem_info["homepage_uri"]
      else
        _, @repo_owner, @repo_name = github_url.match(%r!github\.com/([^/]+)/([^/]+)!).to_a
      end

    rescue JSON::ParserError
      @homepage_uri = "https://rubygems.org/gems/#{gem_name}"
    end

    def repo_full_name
      "#{@repo_owner}/#{repo_name}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
compare_linker-1.1.5 lib/compare_linker/github_link_finder.rb