module Sideload::Github

Constants

GITHUB_V3

Public Instance Methods

credentials=(arr) click to toggle source
# File lib/sideload/github.rb, line 11
def credentials=(arr)
  @user, @pass = arr
end
delete(full_path, target) click to toggle source
# File lib/sideload/github.rb, line 28
def delete(full_path, target)
  raise RuntimeError.new("not implemented")
end
read(repo, path) click to toggle source
# File lib/sideload/github.rb, line 15
def read(repo, path)
  sha = navigate_to(repo, path)
  return traverse(repo, sha)
end
with(path, fname) click to toggle source
# File lib/sideload/github.rb, line 20
def with(path, fname)
  raise RuntimeError.new("not implemented")
end
write(full_path, target, content) click to toggle source
# File lib/sideload/github.rb, line 24
def write(full_path, target, content)
  raise RuntimeError.new("not implemented")
end

Private Instance Methods

cred!() click to toggle source
# File lib/sideload/github.rb, line 34
def cred!
  raise RuntimeError.new("no github credentials set") if @user.nil?
end
get_blob(url) click to toggle source
# File lib/sideload/github.rb, line 59
def get_blob(url)
  cred!
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  request = Net::HTTP::Get.new(uri.request_uri)
  request.basic_auth(@user, @pass)
  response = http.request(request)
  if response.is_a?(Net::HTTPOK)
    return Base64.decode64(JSON.parse(response.body)&.[]("content"))
  else
    puts response, url
    return nil
  end
end
get_tree(repo, folder) click to toggle source
# File lib/sideload/github.rb, line 75
def get_tree(repo, folder)
  cred!
  uri = URI.join(GITHUB_V3, "/repos/#{repo}/git/trees/#{folder}")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  request = Net::HTTP::Get.new(uri.request_uri)
  request.basic_auth(@user, @pass)
  response = http.request(request)
  if response.is_a?(Net::HTTPOK)
    return JSON.parse(response.body)&.[]("tree")
  else
    puts response, uri
    return nil
  end
end
navigate_to(repo, path) click to toggle source
traverse(repo, sha, path = []) click to toggle source
# File lib/sideload/github.rb, line 38
def traverse(repo, sha, path = [])
  return get_tree(repo, sha).reduce({}) do |acc, node|
    name = node["path"]
    case node["type"]
    when "blob"
      acc[(path + [name]).join("/")] = get_blob(node["url"])
    when "tree"
      acc.merge!(traverse(repo, node["sha"], path + [name]))
    end
    next acc
  end
end