Class: LockDiff::Github::UrlDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/lock_diff/github/url_detector.rb

Constant Summary collapse

REGEXP =

xxx.github.aaa/yyyy

%r!https?://([^/]+)\.github\.[^/]+/([^/]+)!

Instance Method Summary collapse

Constructor Details

#initialize(urls) ⇒ UrlDetector

Returns a new instance of UrlDetector.



9
10
11
# File 'lib/lock_diff/github/url_detector.rb', line 9

def initialize(urls)
  @urls = Array(urls).compact
end

Instance Method Details

#callObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/lock_diff/github/url_detector.rb', line 13

def call
  url = @urls.find { |_url| _url.include?("github") }
  return unless url

  begin
    response = HTTPClient.get(url, follow_redirect: true)
    url = response.header.request_uri.to_s
  rescue
    repository = RepositoryNameDetector.new(url).call
    url = "https://github.com/#{repository}"
  end

  if url.match(REGEXP)
    _, owner, repo = url.match(REGEXP).to_a
    url = "https://github.com/#{owner}/#{repo}"
    HTTPClient.get(url).ok? ? url : nil
  else
    repository = RepositoryNameDetector.new(url).call
    "https://github.com/#{repository}"
  end
rescue => e
  LockDiff.logger.warn("Could not detect github url by #{url} because of #{e.inspect}")
  nil
end