Class: GemsBond::Fetchers::Github

Inherits:
Fetcher
  • Object
show all
Defined in:
lib/gems_bond/fetchers/github.rb

Overview

Fetches data from GitHub

Constant Summary collapse

REPOSITORY_REGEX =

GitHub repository pattern, e.g.: “github.com/BigBigDoudou/gems_bond

%r{https?://github.com/(?<repository>.*/.*)}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Fetcher

#started?, #stop

Constructor Details

#initialize(url) ⇒ GemsBond::Fetchers::Github

Initializes an instance

Parameters:

  • url (String)

    URL of the GitHub repository



23
24
25
26
# File 'lib/gems_bond/fetchers/github.rb', line 23

def initialize(url)
  super(url)
  @url = url
end

Class Method Details

.valid_url?(url) ⇒ Boolean

Validates that ‘url` matches GitHub repository URL

Returns:

  • (Boolean)


15
16
17
# File 'lib/gems_bond/fetchers/github.rb', line 15

def valid_url?(url)
  url&.match?(REPOSITORY_REGEX)
end

Instance Method Details

#contributors_countInteger

Note:

GitHub API does not provide this number out of the box so we fetch all repository contributors and paginate with 1 per page thus the last page (from headers) should equal the number of contributors

Returns number of contributors

Returns:

  • (Integer)


58
59
60
61
62
63
64
65
66
67
# File 'lib/gems_bond/fetchers/github.rb', line 58

def contributors_count
  client = Octokit::Client.new(access_token: token, per_page: 1)
  client.contributors(@repository_path)
  response = client.last_response
  links = response.headers[:link]
  # e.g.:  "[...] <https://api.github.com/repositories/8514/contributors?per_page=1&page=377>; rel=\"last\""
  return 0 unless links

  Integer(links.match(/.*per_page=1&page=(?<last>\d+)>; rel="last"/)[:last], 10)
end

#days_since_last_commitDate

Returns number of days since last commit

Returns:

  • (Date)


86
87
88
89
90
# File 'lib/gems_bond/fetchers/github.rb', line 86

def days_since_last_commit
  return unless last_commit_date

  Integer(Date.today - last_commit_date)
end

#forks_countInteger

Returns number of forks

Returns:

  • (Integer)


43
44
45
# File 'lib/gems_bond/fetchers/github.rb', line 43

def forks_count
  @repository["forks"]
end

#last_commit_dateDate

Returns date of last commit (on main branch)

Returns:

  • (Date)


77
78
79
80
81
82
# File 'lib/gems_bond/fetchers/github.rb', line 77

def last_commit_date
  date = client.commits(@repository_path).first[:commit][:committer][:date]
  return unless date

  Date.parse(date.to_s)
end

#lib_sizeInteger

Returns size of the lib directory

Returns:

  • (Integer)


94
95
96
97
98
99
100
101
102
# File 'lib/gems_bond/fetchers/github.rb', line 94

def lib_size
  contents_size = dir_size("lib")
  return unless contents_size

  lines_count_estimation = contents_size / 25
  return lines_count_estimation if lines_count_estimation < 100

  lines_count_estimation - lines_count_estimation % 100
end

#open_issues_countInteger

Returns number of open issues

Returns:

  • (Integer)


71
72
73
# File 'lib/gems_bond/fetchers/github.rb', line 71

def open_issues_count
  @repository["open_issues"]
end

#stars_countInteger

Returns number of stars

Returns:

  • (Integer)


49
50
51
# File 'lib/gems_bond/fetchers/github.rb', line 49

def stars_count
  @repository["watchers"]
end

#startBoolean

Note:

rescue connection errors with nil

Starts the service

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
# File 'lib/gems_bond/fetchers/github.rb', line 31

def start
  super
  parse_url
  
  # ensure repository exists (otherwise it raises Octokit error)
  set_repository
rescue Octokit::Unauthorized, Octokit::InvalidRepository, Octokit::NotFound
  stop
end