Class: GitHub

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

Overview

This class implements the necessary high-level operations we need to perform on the GitHub API and caches them. "High-level", because most of them are already pre-processed to fit what is needed in gemignore, whereas "low-level" operations are those directly performed on the GitHub API

Constant Summary collapse

CACHE_LIFETIME =

seconds

60 * 30

Class Method Summary collapse

Class Method Details

.fileList(repo, tree_sha) ⇒ Object

Retrieve the full list of files available inside the given repository + tree.



35
36
37
38
39
40
41
42
# File 'lib/gemignore/github.rb', line 35

def self.fileList(repo, tree_sha)
  idArray = ["fileList", repo, tree_sha]
  cache = readCache(idArray)
  return cache if not cache.nil?

  tree = Octokit.tree(repo, tree_sha, :recursive => 1).tree
  writeCache(idArray, Hash[*tree.flat_map { |t| [t.path, t.sha] }])
end

.getFile(repo, sha) ⇒ Object

Retrieves a file's contents identified by its blob's SHA hash



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gemignore/github.rb', line 47

def self.getFile(repo, sha)
  idArray = ['getFile', repo, sha]
  cache = readCache(idArray)
  return cache if not cache.nil?

  res = Octokit.blob(repo, sha)
  data = nil
  case res.encoding
    when "base64"
      data = Base64.decode64(res.content)
    when "utf-8"
      data = res.content
    else
      raise NotImplementedError.new("Could not decode response from GitHub API")
  end
  self.writeCache(idArray, data)
end

.readCache(idArray) ⇒ Object (private)



81
82
83
84
# File 'lib/gemignore/github.rb', line 81

def self.readCache(idArray)
  id = idArray.join("$")
  @cache[id]
end

.writeCache(idArray, value) ⇒ Object (private)

Write a value to our cache object.

Raises:

  • (ArgumentError)


69
70
71
72
73
74
75
76
77
78
# File 'lib/gemignore/github.rb', line 69

def self.writeCache(idArray, value)
  raise ArgumentError.new("idArray parameter must be an array") if not idArray.is_a? Array
  id = idArray.join("$")

  if value.is_a? String
    value = value.force_encoding("UTF-8")
  end

  @cache[id] = value
end