Class: Icarus::Mod::Github

Inherits:
Object
  • Object
show all
Defined in:
lib/icarus/mod/github.rb

Overview

Helper methods for interacting with the Github API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo = nil) ⇒ Github

Returns a new instance of Github.



11
12
13
14
15
# File 'lib/icarus/mod/github.rb', line 11

def initialize(repo = nil)
  self.repository = repo if repo
  @client = Octokit::Client.new(access_token: Config.github.token)
  @resources = []
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/icarus/mod/github.rb', line 9

def client
  @client
end

#resourcesObject (readonly)

Returns the value of attribute resources.



9
10
11
# File 'lib/icarus/mod/github.rb', line 9

def resources
  @resources
end

Instance Method Details

#all_files(path: nil, cache: true, recursive: false, &block) ⇒ Object

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


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/icarus/mod/github.rb', line 32

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
  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)
      @resources << entry # cache the file
    end
  end

  @resources unless block
end

#find(pattern) ⇒ Object



53
54
55
# File 'lib/icarus/mod/github.rb', line 53

def find(pattern)
  all_files { |file| return file if /#{pattern}/i.match?(file[:name]) }
end

#get_contents(url) ⇒ Object



57
58
59
# File 'lib/icarus/mod/github.rb', line 57

def get_contents(url)
  @client.contents(url)
end

#repositoryObject



17
18
19
20
21
# File 'lib/icarus/mod/github.rb', line 17

def repository
  raise "You must specify a repository to use" unless @repository

  @repository
end

#repository=(repo) ⇒ Object



23
24
25
26
# File 'lib/icarus/mod/github.rb', line 23

def repository=(repo)
  @resources = [] # reset the resources cache
  @repository = repo.gsub(%r{https?://.*github\.com/}, "")
end