Sha256: 6f560d203ccd3d6a4155e476c6602e1e74ba449d70343ddaa004d4f32dba0a25
Contents?: true
Size: 1.77 KB
Versions: 20
Compression:
Stored size: 1.77 KB
Contents
# frozen_string_literal: true require "octokit" module Icarus module Mod # Helper methods for interacting with the Github API class Github attr_reader :client, :resources def initialize(repo = nil) self.repository = repo if repo @client = Octokit::Client.new(access_token: Config.github.token) @resources = [] end def repository raise "You must specify a repository to use" unless @repository @repository end def repository=(repo) @resources = [] # reset the resources cache @repository = repo.gsub(%r{https?://.*github\.com/}, "") end # Recursively returns all resources in the repository # path: the path to search in # cache: whether to use the cached resources # recursive: whether to recursively search subdirectories def all_files(path: nil, cache: true, recursive: false, &block) # If we've already been called for this repository, use the cached resources use_cache = @resources.any? && cache if use_cache @resources.each { |file| block.call(file) } if block_given? else @client.contents(repository, path:).each do |entry| if entry[:type] == "dir" all_files(path: entry[:path], cache: false, recursive: true, &block) if recursive next # we don't need directories in our output end block.call(entry) if block_given? @resources << entry # cache the file end end @resources unless block_given? end def find(pattern) all_files { |file| return file if file[:name] =~ /#{pattern}/i } end def get_contents(url) @client.contents(url) end end end end
Version data entries
20 entries across 20 versions & 1 rubygems