Class: CompareLinker::GithubLinkFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/compare_linker/github_link_finder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(octokit, gem_dictionary) ⇒ GithubLinkFinder

Returns a new instance of GithubLinkFinder.



9
10
11
12
# File 'lib/compare_linker/github_link_finder.rb', line 9

def initialize(octokit, gem_dictionary)
  @octokit = octokit
  @gem_dictionary = gem_dictionary
end

Instance Attribute Details

#gem_dictionaryObject (readonly)

Returns the value of attribute gem_dictionary.



7
8
9
# File 'lib/compare_linker/github_link_finder.rb', line 7

def gem_dictionary
  @gem_dictionary
end

#homepage_uriObject (readonly)

Returns the value of attribute homepage_uri.



7
8
9
# File 'lib/compare_linker/github_link_finder.rb', line 7

def homepage_uri
  @homepage_uri
end

#octokitObject (readonly)

Returns the value of attribute octokit.



7
8
9
# File 'lib/compare_linker/github_link_finder.rb', line 7

def octokit
  @octokit
end

#repo_nameObject (readonly)

Returns the value of attribute repo_name.



7
8
9
# File 'lib/compare_linker/github_link_finder.rb', line 7

def repo_name
  @repo_name
end

#repo_ownerObject (readonly)

Returns the value of attribute repo_owner.



7
8
9
# File 'lib/compare_linker/github_link_finder.rb', line 7

def repo_owner
  @repo_owner
end

Instance Method Details

#find(gem_name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/compare_linker/github_link_finder.rb', line 14

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 = redirect_url(github_url)
    _, @repo_owner, @repo_name = github_url.match(%r!github\.com/([^/]+)/([^/#]+)!).to_a
  else
    @repo_owner, @repo_name  = gem_dictionary.lookup(gem_info["name"])
    @homepage_uri = gem_info["homepage_uri"]
  end

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

#repo_full_nameObject



35
36
37
# File 'lib/compare_linker/github_link_finder.rb', line 35

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