Class: GemsBond::Fetchers::Github
- 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
-
.valid_url?(url) ⇒ Boolean
Validates that ‘url` matches GitHub repository URL.
Instance Method Summary collapse
-
#contributors_count ⇒ Integer
Returns number of contributors.
-
#days_since_last_commit ⇒ Date
Returns number of days since last commit.
-
#forks_count ⇒ Integer
Returns number of forks.
-
#initialize(url) ⇒ GemsBond::Fetchers::Github
constructor
Initializes an instance.
-
#last_commit_date ⇒ Date
Returns date of last commit (on main branch).
-
#lib_size ⇒ Integer
Returns size of the lib directory.
-
#open_issues_count ⇒ Integer
Returns number of open issues.
-
#stars_count ⇒ Integer
Returns number of stars.
-
#start ⇒ Boolean
Starts the service.
Methods inherited from Fetcher
Constructor Details
#initialize(url) ⇒ GemsBond::Fetchers::Github
Initializes an instance
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
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_count ⇒ Integer
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
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_commit ⇒ Date
Returns number of days since last commit
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_count ⇒ Integer
Returns number of forks
43 44 45 |
# File 'lib/gems_bond/fetchers/github.rb', line 43 def forks_count @repository["forks"] end |
#last_commit_date ⇒ Date
Returns date of last commit (on main branch)
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_size ⇒ Integer
Returns size of the lib directory
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_count ⇒ Integer
Returns number of open issues
71 72 73 |
# File 'lib/gems_bond/fetchers/github.rb', line 71 def open_issues_count @repository["open_issues"] end |
#stars_count ⇒ Integer
Returns number of stars
49 50 51 |
# File 'lib/gems_bond/fetchers/github.rb', line 49 def stars_count @repository["watchers"] end |
#start ⇒ Boolean
rescue connection errors with nil
Starts the service
31 32 33 34 35 36 37 38 39 |
# File 'lib/gems_bond/fetchers/github.rb', line 31 def start super parse_url login # ensure repository exists (otherwise it raises Octokit error) set_repository rescue Octokit::Unauthorized, Octokit::InvalidRepository, Octokit::NotFound stop end |